FileDocCategorySizeDatePackage
CircularList.javaAPI DocGlassfish v2 API6238Fri May 04 22:31:06 BST 2007com.sun.appserv.management.util.misc

CircularList

public final class CircularList extends AbstractList
A circular list implementation

Fields Summary
private final T[]
mObjects
private int
mNumItems
private int
mFirst
private OverflowHandler
mOverflowHandler
Constructors Summary
public CircularList(Class theClass, int size)

		if ( size == 0 )
		{
			throw new IllegalArgumentException( "list must have at least one item" );
		}

		mObjects	= ArrayUtil.newArray( theClass, size );
		mFirst		= 0;
		mNumItems	= 0;
		
		mOverflowHandler	= null;
	
Methods Summary
public final booleanadd(T item)

		final int	capacity	= capacity();
		
		assert( mFirst < capacity );
		
		// if we're full before adding, then we'll be overwriting
		// the first item.
		if ( size() == capacity )
		{
			final T	overwrittenObject	= get( 0 );
			
			// first item will be overwritten; next one is first (oldest)
			mFirst	= (mFirst + 1) % capacity;
			store( capacity - 1, item );
			
			discardedObject( overwrittenObject );
			
		}
		else
		{
			store( mNumItems, item );
			++mNumItems;
		}
		
		++modCount;
		return( true );
	
public final voidadd(int index, T item)
May be added to the end only.

		if ( index == mNumItems )
		{
			add( item );
		}
		else
		{
			throw new UnsupportedOperationException( "add not at end" );
		}
	
public final intcapacity()

		return( mObjects.length );
	
private final voidcheckInBounds(int i)

		if ( i < 0 || i >= mNumItems )
		{
			throw new IndexOutOfBoundsException( "" + i );
		}
	
public final voidclear()

		for( int i = 0; i < size(); ++i )
		{
			set( i, null );
		}
		mNumItems	= 0;
		++modCount;
	
protected voiddiscardedObject(T o)

		if ( mOverflowHandler != null )
		{
			mOverflowHandler.handleBufferOverflow( o );
		}
	
public booleanequals(java.lang.Object rhsIn)

		boolean	equal	= false;
		
		if ( rhsIn == this )
		{
			equal	= true;
		}
		else if ( ! (rhsIn instanceof CircularList) )
		{
			equal	= false;
		}
		else
		{
			final CircularList	rhs	= (CircularList)rhsIn;
			
			equal =  capacity() == rhs.capacity() &&
				size() == rhs.size();
			if ( equal )
			{
				final int	size	= size();
				for( int i = 0; i < size(); ++i )
				{
					if ( ! CompareUtil.objectsEqual( get( i ), rhs.get( i ) ) )
					{
						equal	= false;
						break;
					}
				}
			}
		}
		
		return( equal );
	
public final Tget(int i)

		checkInBounds( i );
		
		return( mObjects[ getPhysicalIndex( i ) ] );
	
public com.sun.appserv.management.util.misc.CircularList$OverflowHandlergetOverflowHandler()

		return( mOverflowHandler );
	
private final intgetPhysicalIndex(int logicalIndex)

		return( (mFirst + logicalIndex) % capacity() );
	
public final Tremove(int i)

		T	result	= null;
		
		if ( i == 0 )
		{
			result	= removeFirst();
		}
		else if ( i == mNumItems - 1 )
		{
			result	= removeLast();
		}
		else
		{
			throw new UnsupportedOperationException();
		}
		
		++modCount;
		return( result );
	
public final TremoveFirst()

		checkInBounds( 0 );
		
		final T	result	= get( 0 );
		--mNumItems;
		mFirst	= (mFirst + 1) % capacity();
		
		return( result );
	
public final TremoveLast()

		checkInBounds( 0 );
		
		final T	result	= get( mNumItems - 1 );
		--mNumItems;
		
		return( result );
	
public final Tset(int i, T item)

		checkInBounds( i );
		
		final int	physicalIndex	= getPhysicalIndex( i );
		final T	oldItem	= mObjects[ physicalIndex ];
		mObjects[ physicalIndex ] = item;
		
		return( oldItem );
	
public voidsetOverflowHandler(com.sun.appserv.management.util.misc.CircularList$OverflowHandler handler)

		mOverflowHandler	= handler;
	
public final intsize()

		return( mNumItems );
	
private final voidstore(int logicalIndex, T item)

		mObjects[ getPhysicalIndex( logicalIndex ) ]	= item;
	
public java.lang.StringtoString()

		return( ArrayStringifier.stringify( toArray( ), ", " ) );