/** * Return the node following the given node. This method exists for two reasons: * <ol> * <li>It's really not recommended that the methods implemented by TLinkable be * called directly since they're used internally by this class.</li> * <li>This solves problems arising from generics when working with the linked * objects directly.</li> * </ol> * <p/> * NOTE: this should only be used with nodes contained in the list. The results are * undefined with anything else. * * @param current The current node * @return the node after the current node */ @SuppressWarnings({"unchecked"}) public T getNext( T current ) { return current.getNext(); }
/** * Copies the list's contents into a native array. This will be a * shallow copy: the Tlinkable instances in the Object[] array * have links to one another: changing those will put this list * into an unpredictable state. Holding a reference to one * element in the list will prevent the others from being garbage * collected unless you clear the next/previous links. <b>Caveat * programmer!</b> * * @return an <code>Object[]</code> value */ public Object[] toArray() { Object[] o = new Object[_size]; int i = 0; for ( TLinkable link = _head; link != null; link = link.getNext() ) { o[i++] = link; } return o; }
/** * A linear search for <tt>o</tt> in the list. * * @param o an <code>Object</code> value * @return a <code>boolean</code> value */ public boolean contains( Object o ) { for ( TLinkable<T> link = _head; link != null; link = link.getNext() ) { if ( o.equals( link ) ) { return true; } } return false; }
/** * Executes <tt>procedure</tt> for each entry in the list. * * @param procedure a <code>TObjectProcedure</code> value * @return false if the loop over the values terminated because * the procedure returned false for some value. */ @SuppressWarnings({"unchecked"}) public boolean forEachValue( TObjectProcedure<T> procedure ) { T node = _head; while ( node != null ) { boolean keep_going = procedure.execute( node ); if ( !keep_going ) { return false; } node = node.getNext(); } return true; }
/** {@inheritDoc} */ @Override @SuppressWarnings({"unchecked"}) public T get( int index ) { // Blow out for bogus values if ( index < 0 || index >= _size ) { throw new IndexOutOfBoundsException( "Index: " + index + ", Size: " + _size ); } // Determine if it's better to get there from the front or the back if ( index > ( _size >> 1 ) ) { int position = _size - 1; T node = _tail; while ( position > index ) { node = node.getPrevious(); position--; } return node; } else { int position = 0; T node = _head; while ( position < index ) { node = node.getNext(); position++; } return node; } }
/** * Remove and return the first element in the list. * * @return an <code>Object</code> value */ @SuppressWarnings({"unchecked"}) public T removeFirst() { T o = _head; if ( o == null ) { return null; } T n = o.getNext(); o.setNext( null ); if ( null != n ) { n.setPrevious( null ); } _head = n; if ( --_size == 0 ) { _tail = null; } return o; }
/** * Returns a typed array of the objects in the set. * * @param a an <code>Object[]</code> value * @return an <code>Object[]</code> value */ @SuppressWarnings({"unchecked"}) public T[] toUnlinkedArray( T[] a ) { int size = size(); if ( a.length < size ) { a = (T[]) Array.newInstance( a.getClass().getComponentType(), size ); } int i = 0; for ( T link = _head, tmp; link != null; i++ ) { a[i] = link; tmp = link; link = link.getNext(); tmp.setNext( null ); // clear the links tmp.setPrevious( null ); } _size = 0; // clear the list _head = _tail = null; return a; }
/** * Copies the list to a native array, destroying the next/previous * links as the copy is made. This list will be emptied after the * copy (as if clear() had been invoked). The Object[] array * returned will contain TLinkables that do <b>not</b> hold * references to one another and so are less likely to be the * cause of memory leaks. * * @return an <code>Object[]</code> value */ public Object[] toUnlinkedArray() { Object[] o = new Object[_size]; int i = 0; for ( TLinkable<T> link = _head, tmp; link != null; i++ ) { o[i] = link; tmp = link; link = link.getNext(); tmp.setNext( null ); // clear the links tmp.setPrevious( null ); } _size = 0; // clear the list _head = _tail = null; return o; }
/** * Inserts newElement into the list immediately after current. * All elements to the left of and including current are shifted * over. * * @param current a <code>TLinkable</code> value currently in the list. * @param newElement a <code>TLinkable</code> value to be added to * the list. */ public void addAfter( T current, T newElement ) { if ( current == _tail ) { addLast( newElement ); } else if ( current == null ) { addFirst( newElement ); } else { T n = current.getNext(); newElement.setPrevious( current ); newElement.setNext( n ); current.setNext( newElement ); n.setPrevious( newElement ); _size++; } }
n = link.getNext();
/** * Return the node following the given node. This method exists for two reasons: * <ol> * <li>It's really not recommended that the methods implemented by TLinkable be * called directly since they're used internally by this class.</li> * <li>This solves problems arising from generics when working with the linked * objects directly.</li> * </ol> * <p/> * NOTE: this should only be used with nodes contained in the list. The results are * undefined with anything else. * * @param current The current node * @return the node after the current node */ @SuppressWarnings({"unchecked"}) public T getNext( T current ) { return current.getNext(); }
/** * A linear search for <tt>o</tt> in the list. * * @param o an <code>Object</code> value * @return a <code>boolean</code> value */ public boolean contains( Object o ) { for ( TLinkable<T> link = _head; link != null; link = link.getNext() ) { if ( o.equals( link ) ) { return true; } } return false; }
/** * A linear search for <tt>o</tt> in the list. * * @param o an <code>Object</code> value * @return a <code>boolean</code> value */ public boolean contains( Object o ) { for ( TLinkable<T> link = _head; link != null; link = link.getNext() ) { if ( o.equals( link ) ) { return true; } } return false; }
/** * A linear search for <tt>o</tt> in the list. * * @param o an <code>Object</code> value * @return a <code>boolean</code> value */ public boolean contains( Object o ) { for ( TLinkable<T> link = _head; link != null; link = link.getNext() ) { if ( o.equals( link ) ) { return true; } } return false; }
/** * A linear search for <tt>o</tt> in the list. * * @param o an <code>Object</code> value * @return a <code>boolean</code> value */ @Override public boolean contains( Object o ) { for ( TLinkable<T> link = _head; link != null; link = link.getNext() ) { if ( o.equals( link ) ) { return true; } } return false; }