public final BehindRef insert ( final AbstractItemLink insertAil ) { final long insertPosition = insertAil . getPosition ( ) ; boolean inserted = false ; BehindRef addref = null ; // Loop backwards through the list in order of decreasing sequence number // (addition usually near the end) until we have inserted the entry BehindRef lookat = _lastLinkBehind ; while ( ! inserted && ( lookat != null ) ) { AbstractItemLink lookatAil = lookat . getAIL ( ) ; if ( lookatAil != null ) { long lookatPosition = lookatAil . getPosition ( ) ; if ( insertPosition > lookatPosition ) { // This is where it goes then addref = new BehindRef ( insertAil ) ; if ( lookat . _next == null ) { // It's now the last one in the list _lastLinkBehind = addref ; } else { // It's going in the middle of the list addref . _next = lookat . _next ; lookat . _next . _prev = addref ; } addref . _prev = lookat ; lookat . _next = addref ; inserted = true ; } else if ( insertPosition == lookatPosition ) { // A duplicate. OK, it's been made reavailable more than once addref = lookat ; inserted = true ; } else { // Need to move backwards lookat = lookat . _prev ; } } else { BehindRef newlookat = lookat . _prev ; _remove ( lookat ) ; lookat = newlookat ; } } // If we have not inserted it yet, it's going to be the first in the list if ( ! inserted ) { addref = new BehindRef ( insertAil ) ; if ( _firstLinkBehind != null ) { addref . _next = _firstLinkBehind ; _firstLinkBehind . _prev = addref ; _firstLinkBehind = addref ; } else { _firstLinkBehind = addref ; _lastLinkBehind = addref ; } } return addref ; }
Inserts a reference to an AIL in the list of AILs behind the curent position of the cursor . The insertion is performed in position order . The list is composed of weak references and any which are discovered to refer to objects no longer on the heap are removed as we go .