/** * 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++; }
/** * 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; }
/** * 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; }
/** * 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 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++; } }
/** 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; }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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++; } }
/** * 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++; } }
/** 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; }