private TemplateSubPatternAssociation insertAssociationIntoList ( TemplateSubPatternAssociation head , TemplateSubPatternAssociation item , boolean isWildCardInsert ) { // Sort first by import level (higher level is at front), // then by priority (highest priority is at front), // then by document order (later in document is at front). double priority = getPriorityOrScore ( item ) ; double workPriority ; int importLevel = item . getImportLevel ( ) ; int docOrder = item . getDocOrderPos ( ) ; TemplateSubPatternAssociation insertPoint = head ; TemplateSubPatternAssociation next ; boolean insertBefore ; // true means insert before insertPoint; otherwise after // This can only be true if insertPoint is pointing to // the first or last template. // Spin down so that insertPoint points to: // (a) the template immediately _before_ the first template on the chain with // a precedence that is either (i) less than ours or (ii) the same as ours but // the template document position is less than ours // -or- // (b) the last template on the chain if no such template described in (a) exists. // If we are pointing to the first template or the last template (that is, case b), // we need to determine whether to insert before or after the template. Otherwise, // we always insert after the insertPoint. while ( true ) { next = insertPoint . getNext ( ) ; if ( null == next ) break ; else { workPriority = getPriorityOrScore ( next ) ; if ( importLevel > next . getImportLevel ( ) ) break ; else if ( importLevel < next . getImportLevel ( ) ) insertPoint = next ; else if ( priority > workPriority ) // import precedence is equal break ; else if ( priority < workPriority ) insertPoint = next ; else if ( docOrder >= next . getDocOrderPos ( ) ) // priorities, import are equal break ; else insertPoint = next ; } } if ( ( null == next ) || ( insertPoint == head ) ) // insert point is first or last { workPriority = getPriorityOrScore ( insertPoint ) ; if ( importLevel > insertPoint . getImportLevel ( ) ) insertBefore = true ; else if ( importLevel < insertPoint . getImportLevel ( ) ) insertBefore = false ; else if ( priority > workPriority ) insertBefore = true ; else if ( priority < workPriority ) insertBefore = false ; else if ( docOrder >= insertPoint . getDocOrderPos ( ) ) insertBefore = true ; else insertBefore = false ; } else insertBefore = false ; // System.out.println("appending: "+target+" to "+matchPat.getPattern()); if ( isWildCardInsert ) { if ( insertBefore ) { item . setNext ( insertPoint ) ; String key = insertPoint . getTargetString ( ) ; item . setTargetString ( key ) ; putHead ( key , item ) ; return item ; } else { item . setNext ( next ) ; insertPoint . setNext ( item ) ; return head ; } } else { if ( insertBefore ) { item . setNext ( insertPoint ) ; if ( insertPoint . isWild ( ) || item . isWild ( ) ) m_wildCardPatterns = item ; else putHead ( item . getTargetString ( ) , item ) ; return item ; } else { item . setNext ( next ) ; insertPoint . setNext ( item ) ; return head ; } } }
Insert the given TemplateSubPatternAssociation into the the linked list . Sort by import precedence then priority then by document order .