/** * Return the node preceding 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 getPrevious( T current ) { return current.getPrevious(); }
/** {@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 last element in the list. * * @return an <code>Object</code> value */ @SuppressWarnings({"unchecked"}) public T removeLast() { T o = _tail; if ( o == null ) { return null; } T prev = o.getPrevious(); o.setPrevious( null ); if ( null != prev ) { prev.setNext( null ); } _tail = prev; if ( --_size == 0 ) { _head = null; } return o; }
/** Empties the list. */ public void clear() { if ( null != _head ) { for ( TLinkable<T> link = _head.getNext(); link != null; link = link.getNext() ) { TLinkable<T> prev = link.getPrevious(); prev.setNext( null ); link.setPrevious( null ); } _head = _tail = null; } _size = 0; }
/** * Inserts newElement into the list immediately before current. * All elements to the right 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 addBefore( T current, T newElement ) { if ( current == _head ) { addFirst( newElement ); } else if ( current == null ) { addLast( newElement ); } else { T p = current.getPrevious(); newElement.setNext( current ); p.setNext( newElement ); newElement.setPrevious( p ); current.setPrevious( newElement ); _size++; } }
/** * Implementation of index-based list insertions. * * @param index an <code>int</code> value * @param linkable an object of type TLinkable */ @SuppressWarnings({"unchecked"}) protected void insert( int index, T linkable ) { if ( _size == 0 ) { _head = _tail = linkable; // first insertion } else if ( index == 0 ) { linkable.setNext( _head ); // insert at front _head.setPrevious( linkable ); _head = linkable; } else if ( index == _size ) { // insert at back _tail.setNext( linkable ); linkable.setPrevious( _tail ); _tail = linkable; } else { T node = get( index ); T before = node.getPrevious(); if ( before != null ) { before.setNext( linkable ); } linkable.setPrevious( before ); linkable.setNext( node ); node.setPrevious( linkable ); } _size++; }
TLinkable<T> link = (TLinkable<T>) o; p = link.getPrevious(); n = link.getNext();
/** * Return the node preceding 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 getPrevious( T current ) { return current.getPrevious(); }
/** * Return the node preceding 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 getPrevious( T current ) { return current.getPrevious(); }
/** * Return the node preceding 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 */ public T getPrevious( T current ) { return current.getPrevious(); }
/** * Return the node preceding 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> * 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 getPrevious( T current ) { return current.getPrevious(); }
/** * Remove and return the last element in the list. * * @return an <code>Object</code> value */ @SuppressWarnings({"unchecked"}) public T removeLast() { T o = _tail; if ( o == null ) { return null; } T prev = o.getPrevious(); o.setPrevious( null ); if ( null != prev ) { prev.setNext( null ); } _tail = prev; if ( --_size == 0 ) { _head = null; } return o; }
/** * Remove and return the last element in the list. * * @return an <code>Object</code> value */ @SuppressWarnings({"unchecked"}) public T removeLast() { T o = _tail; if ( o == null ) { return null; } T prev = o.getPrevious(); o.setPrevious( null ); if ( null != prev ) { prev.setNext( null ); } _tail = prev; if ( --_size == 0 ) { _head = null; } return o; }
/** * Remove and return the last element in the list. * * @return an <code>Object</code> value */ public T removeLast() { T o = _tail; if ( o == null ) { return null; } T prev = o.getPrevious(); o.setPrevious( null ); if ( null != prev ) { prev.setNext( null ); } _tail = prev; if ( --_size == 0 ) { _head = null; } return o; }
/** Empties the list. */ public void clear() { if ( null != _head ) { for ( TLinkable<T> link = _head.getNext(); link != null; link = link.getNext() ) { TLinkable<T> prev = link.getPrevious(); prev.setNext( null ); link.setPrevious( null ); } _head = _tail = null; } _size = 0; }
/** Empties the list. */ public void clear() { if ( null != _head ) { for ( TLinkable<T> link = _head.getNext(); link != null; link = link.getNext() ) { TLinkable<T> prev = link.getPrevious(); prev.setNext( null ); link.setPrevious( null ); } _head = _tail = null; } _size = 0; }
/** Empties the list. */ @Override public void clear() { if ( null != _head ) { for ( TLinkable<T> link = _head.getNext(); link != null; link = link.getNext() ) { TLinkable<T> prev = link.getPrevious(); prev.setNext( null ); link.setPrevious( null ); } _head = _tail = null; } _size = 0; }
/** Empties the list. */ public void clear() { if ( null != _head ) { for ( TLinkable<T> link = _head.getNext(); link != null; link = link.getNext() ) { TLinkable<T> prev = link.getPrevious(); prev.setNext( null ); link.setPrevious( null ); } _head = _tail = null; } _size = 0; }
/** * Inserts newElement into the list immediately before current. * All elements to the right 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 addBefore( T current, T newElement ) { if ( current == _head ) { addFirst( newElement ); } else if ( current == null ) { addLast( newElement ); } else { T p = current.getPrevious(); newElement.setNext( current ); p.setNext( newElement ); newElement.setPrevious( p ); current.setPrevious( newElement ); _size++; } }
/** * Inserts newElement into the list immediately before current. * All elements to the right 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 addBefore( T current, T newElement ) { if ( current == _head ) { addFirst( newElement ); } else if ( current == null ) { addLast( newElement ); } else { T p = current.getPrevious(); newElement.setNext( current ); p.setNext( newElement ); newElement.setPrevious( p ); current.setPrevious( newElement ); _size++; } }