User: user57
Date: 02/02/14 22:14:22
Added: src/main/org/jboss/util/collection AbstractQueue.java
ArrayIterator.java CachedCollection.java
CachedList.java CollectionException.java
CompoundIterator.java CompoundKey.java
EmptyCollectionException.java
FullCollectionException.java Iterators.java
ListQueue.java ListSet.java Queue.java
ReverseListIterator.java WeakSet.java package.html
Log:
o importing the useful bits from the bliss cl.
o plus some bits from server/*/util/* have been moved to util.jmx
Revision Changes Path
1.1
jboss-common/src/main/org/jboss/util/collection/AbstractQueue.java
Index: AbstractQueue.java
===================================================================
/***************************************
* *
* JBoss: The OpenSource J2EE WebOS *
* *
* Distributable under LGPL license. *
* See terms of license at gnu.org. *
* *
***************************************/
package org.jboss.util.collection;
import java.util.AbstractCollection;
/**
* An abstract implementation of a Queue. Sub-classes must provide methods
* for <code>addLast(Object)</code> and <code>removeFirst()</code>.
*
* @version <tt>$Revision: 1.1 $</tt>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
*/
public abstract class AbstractQueue
extends AbstractCollection
implements Queue
{
/** Default maximum queue size */
public static int DEFAULT_MAXIMUM_SIZE = UNLIMITED_MAXIMUM_SIZE;
/** Maximum queue size */
protected int maximumSize = DEFAULT_MAXIMUM_SIZE;
/**
* Initializes the AbstractQueue.
*/
protected AbstractQueue() {}
/**
* Initializes the AbstractQueue.
*
* @param maxSize Maximum queue size.
*
* @exception IllegalArgumentException Illegal size.
*/
protected AbstractQueue(final int maxSize) {
setMaximumSize(maxSize);
}
/**
* Get the maximum size of the queue.
*
* @return Maximum queue size or {@link #UNLIMITED_MAXIMUM_SIZE}.
*/
public int getMaximumSize() {
return maximumSize;
}
/**
* Set the maximum size of the queue
*
* @param size New maximim queue size or {@link #UNLIMITED_MAXIMUM_SIZE}.
*
* @exception IllegalArgumentException Illegal size.
*/
public void setMaximumSize(final int size) {
if (size < 0 && size != UNLIMITED_MAXIMUM_SIZE)
throw new IllegalArgumentException("illegal size: " + size);
maximumSize = size;
}
/**
* Check if the queue is full.
*
* @return True if the queue is full.
*/
public boolean isFull() {
if (maximumSize != UNLIMITED_MAXIMUM_SIZE && size() >= maximumSize)
return true;
return false;
}
/**
* Check if the queue is empty.
*
* @return True if the queue is empty.
*/
public boolean isEmpty() {
if (size() <= 0)
return true;
return false;
}
/**
* Append and object to the underling list.
*
* @param obj Object to enqueue.
* @return True if collection was modified.
*
* @exception FullCollectionException The queue is full.
*/
public boolean add(Object obj) throws FullCollectionException {
if (isFull())
throw new FullCollectionException();
return addLast(obj);
}
/**
* Remove and return the first object in the queue.
*
* @return Dequeued object.
*
* @exception EmptyCollectionException The queue is empty.
*/
public Object remove() throws EmptyCollectionException {
if (isEmpty())
throw new EmptyCollectionException();
return removeFirst();
}
/**
* Removes all of the elements from this queue
*/
public void clear() {
while (!isEmpty()) {
remove();
}
}
/**
* Appends the given element to the end of the queue
*
* @param obj Object to append
* @return Per Collection.add(), we return a boolean to indicate if
* the object modified the collection.
*/
protected abstract boolean addLast(Object obj);
/**
* Remove the first object in the queue
*
* @return First object in the queue
*/
protected abstract Object removeFirst();
}
1.1
jboss-common/src/main/org/jboss/util/collection/ArrayIterator.java
Index: ArrayIterator.java
===================================================================
/***************************************
* *
* JBoss: The OpenSource J2EE WebOS *
* *
* Distributable under LGPL license. *
* See terms of license at gnu.org. *
* *
***************************************/
package org.jboss.util.collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.io.Serializable;
import org.jboss.util.NullArgumentException;
/**
* An array iterator.
*
* @version <tt>$Revision: 1.1 $</tt>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
*/
public class ArrayIterator
implements Iterator, Serializable, Cloneable
{
/** Array to iterate over. */
protected final Object[] array;
/** The current position in the array. */
protected int index;
/**
* Construct an ArrayIterator.
*
* @param array The array to iterate over.
*/
public ArrayIterator(final Object[] array) {
if (array == null)
throw new NullArgumentException("array");
this.array = array;
}
/**
* Returns true if there are more elements in the iteration.
*
* @return True if there are more elements in the iteration.
*/
public boolean hasNext() {
return index < array.length;
}
/**
* Returns the next element in the iteration.
*
* @return The next element in the iteration.
*
* @throws NoSuchElementException The are no more elements available.
*/
public Object next() {
if (! hasNext())
throw new NoSuchElementException();
return array[index++];
}
/**
* Unsupported.
*
* @throws UnsupportedOperationException
*/
public void remove() {
throw new UnsupportedOperationException();
}
/**
* Returns a shallow cloned copy of this object.
*
* @return A shallow cloned copy of this object.
*/
public Object clone() {
try {
return super.clone();
}
catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
}
1.1
jboss-common/src/main/org/jboss/util/collection/CachedCollection.java
Index: CachedCollection.java
===================================================================
/***************************************
* *
* JBoss: The OpenSource J2EE WebOS *
* *
* Distributable under LGPL license. *
* See terms of license at gnu.org. *
* *
***************************************/
package org.jboss.util.collection;
import java.util.Collection;
import java.util.AbstractCollection;
import java.util.Iterator;
import java.lang.ref.ReferenceQueue;
import org.jboss.util.SoftObject;
import org.jboss.util.Objects;
/**
* A wrapper around a <code>Collection</code> which translates added objects
* into {@link SoftObject} references, allowing the VM to garbage collect
* objects in the collection when memory is low.
*
* @version <tt>$Revision: 1.1 $</tt>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
*/
public class CachedCollection
extends AbstractCollection
{
/** Reference queue */
protected final ReferenceQueue queue = new ReferenceQueue();
/** Wrapped collection */
protected final Collection collection;
/**
* Construct a CachedCollection.
*
* @param collection Collection to wrap.
*/
public CachedCollection(final Collection collection) {
this.collection = collection;
}
/**
* Returns an iterator over the elements contained in this collection.
*
* @return An iterator over the elements contained in this collection.
*/
public Iterator iterator() {
maintain();
return new MyIterator(collection.iterator());
}
/**
* Returns the size of the collection.
*
* @return The number of elements in the collection.
*/
public int size() {
maintain();
return collection.size();
}
/**
* Add an object to the collection.
*
* @param obj Object (or <i>null</i> to add to the collection.
* @return True if object was added.
*/
public boolean add(final Object obj) {
maintain();
SoftObject soft = SoftObject.create(obj, queue);
return collection.add(soft);
}
/**
* Maintains the collection by removing garbage collected objects.
*/
private void maintain() {
SoftObject obj;
int count = 0;
while ((obj = (SoftObject)queue.poll()) != null) {
count++;
collection.remove(obj);
}
if (count != 0) {
// some temporary debugging fluff
System.err.println("vm reclaimed " + count + " objects");
}
}
/////////////////////////////////////////////////////////////////////////
// De-Referencing Iterator //
/////////////////////////////////////////////////////////////////////////
/**
* A dereferencing iterator.
*/
private final class MyIterator
implements Iterator
{
private final Iterator iter;
public MyIterator(final Iterator iter) {
this.iter = iter;
}
public boolean hasNext() {
maintain();
return iter.hasNext();
}
private Object nextObject() {
Object obj = iter.next();
return Objects.deref(obj);
}
public Object next() {
maintain();
return nextObject();
}
public void remove() {
maintain();
iter.remove();
}
}
}
1.1 jboss-common/src/main/org/jboss/util/collection/CachedList.java
Index: CachedList.java
===================================================================
/***************************************
* *
* JBoss: The OpenSource J2EE WebOS *
* *
* Distributable under LGPL license. *
* See terms of license at gnu.org. *
* *
***************************************/
package org.jboss.util.collection;
import java.util.List;
import java.util.LinkedList;
import java.util.AbstractList;
import java.util.Iterator;
import java.lang.ref.ReferenceQueue;
import org.jboss.util.SoftObject;
import org.jboss.util.Objects;
/**
* A wrapper around a <code>List</code> which translates added objects
* into {@link SoftObject} references, allowing the VM to garbage collect
* objects in the collection when memory is low.
*
* @version <tt>$Revision: 1.1 $</tt>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
*/
public class CachedList
extends AbstractList
{
/** Reference queue. */
protected final ReferenceQueue queue = new ReferenceQueue();
/** Wrapped list. */
protected final List list;
/**
* Construct a <tt>CachedList</tt>.
*
* @param list List to wrap.
*/
public CachedList(final List list) {
this.list = list;
}
/**
* Construct a <tt>CachedList</tt> using a <tt>LinkedList</tt> for
* storage.
*/
public CachedList() {
this(new LinkedList());
}
/**
* Dereference the object at the given index.
*/
private Object getObject(final int index) {
Object obj = list.get(index);
return Objects.deref(obj);
}
/**
* Returns the element at the specified position in this list.
*
* @param index Index of element to return.
* @return The element at the specified position.
*/
public Object get(final int index) {
maintain();
return getObject(index);
}
/**
* Return the size of the list.
*
* @return The number of elements in the list.
*/
public int size() {
maintain();
return list.size();
}
/**
* Replaces the element at the specified position in this list with the
* specified element.
*
* @param index Index of element to replace.
* @param obj Element to be stored at the specified postion.
* @return The previous element at the given index.
*/
public Object set(final int index, final Object obj) {
maintain();
SoftObject soft = SoftObject.create(obj, queue);
soft = (SoftObject)list.set(index, soft);
return Objects.deref(soft);
}
/**
* Inserts the specified element at the specified position in this list
* (optional operation). Shifts the element currently at that position
* (if any) and any subsequent elements to the right (adds one to their
* indices).
*
* @param index Index at which the specified element is to be inserted.
* @param obj Element to be inserted.
*/
public void add(final int index, final Object obj) {
maintain();
SoftObject soft = SoftObject.create(obj, queue);
list.add(index, soft);
}
/**
* Removes the element at the specified position in this list (optional
* operation). Shifts any subsequent elements to the left (subtracts one
* from their indices). Returns the element that was removed from the list.
*
* @param index The index of the element to remove.
* @return The element previously at the specified position.
*/
public Object remove(final int index) {
maintain();
Object obj = list.remove(index);
return Objects.deref(obj);
}
/**
* Maintains the collection by removing garbage collected objects.
*/
private void maintain() {
SoftObject obj;
int count = 0;
while ((obj = (SoftObject)queue.poll()) != null) {
count++;
list.remove(obj);
}
if (count != 0) {
// some temporary debugging fluff
System.err.println("vm reclaimed " + count + " objects");
}
}
}
1.1
jboss-common/src/main/org/jboss/util/collection/CollectionException.java
Index: CollectionException.java
===================================================================
/***************************************
* *
* JBoss: The OpenSource J2EE WebOS *
* *
* Distributable under LGPL license. *
* See terms of license at gnu.org. *
* *
***************************************/
package org.jboss.util.collection;
/**
* A generic collection exception.
*
* @version <tt>$Revision: 1.1 $</tt>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
*/
public class CollectionException
extends RuntimeException
{
/**
* Construct a <code>CollectionException</code> with the specified
* detail message.
*
* @param msg Detail message.
*/
public CollectionException(String msg) {
super(msg);
}
/**
* Construct a <code>CollectionException</code> with no detail.
*/
public CollectionException() {
super();
}
}
1.1
jboss-common/src/main/org/jboss/util/collection/CompoundIterator.java
Index: CompoundIterator.java
===================================================================
/***************************************
* *
* JBoss: The OpenSource J2EE WebOS *
* *
* Distributable under LGPL license. *
* See terms of license at gnu.org. *
* *
***************************************/
package org.jboss.util.collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* A compound iterator, which iterates over all of the elements in the
* given iterators.
*
* @version <tt>$Revision: 1.1 $</tt>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
*/
public class CompoundIterator
implements Iterator
{
/** The array of iterators to iterate over. */
protected final Iterator iters[];
/** The index of the current iterator. */
protected int index;
/**
* Construct a CompoundIterator over the given array of iterators.
*
* @param iters Array of iterators to iterate over.
*
* @throws IllegalArgumentException Array is <kk>null</kk> or empty.
*/
public CompoundIterator(final Iterator iters[]) {
if (iters == null || iters.length == 0)
throw new IllegalArgumentException("array is null or empty");
this.iters = iters;
}
/**
* Check if there are more elements.
*
* @return True if there are more elements.
*/
public boolean hasNext() {
for (; index < iters.length; index++) {
if (iters[index] != null && iters[index].hasNext()) {
return true;
}
}
return false;
}
/**
* Return the next element from the current iterator.
*
* @return The next element from the current iterator.
*
* @throws NoSuchElementException There are no more elements.
*/
public Object next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return iters[index].next();
}
/**
* Remove the current element from the current iterator.
*
* @throws IllegalStateException
* @throws UnsupportedOperationException
*/
public void remove() {
iters[index].remove();
}
}
1.1 jboss-common/src/main/org/jboss/util/collection/CompoundKey.java
Index: CompoundKey.java
===================================================================
/***************************************
* *
* JBoss: The OpenSource J2EE WebOS *
* *
* Distributable under LGPL license. *
* See terms of license at gnu.org. *
* *
***************************************/
package org.jboss.util.collection;
import java.io.Serializable;
import org.jboss.util.NullArgumentException;
import org.jboss.util.Objects;
import org.jboss.util.HashCode;
import org.jboss.util.Strings;
/**
* An immutable compound key class.
*
* @version <tt>$Revision: 1.1 $</tt>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
*/
public class CompoundKey
implements Serializable, Cloneable
{
/** The elements of the key */
private final Object elements[];
/**
* Construct a CompoundKey.
*
* @param elements Elements of the key.
*/
public CompoundKey(final Object elements[]) {
if (elements == null)
throw new NullArgumentException("elements");
this.elements = elements;
}
/**
* Construct a CompoundKey.
*
* @param a Element.
* @param b Element.
*/
public CompoundKey(final Object a, final Object b) {
this(new Object[] { a, b });
}
/**
* Construct a CompoundKey.
*
* @param a Element.
* @param b Element.
* @param c Element.
*/
public CompoundKey(final Object a, final Object b, final Object c) {
this(new Object[] { a, b, c });
}
/**
* Test the equality of an object with this.
*
* @param obj Object to test equality with.
* @return True if object is equal.
*/
public boolean equals(final Object obj) {
if (obj == this) return true;
if (obj != null && obj.getClass() == getClass()) {
CompoundKey key = (CompoundKey)obj;
return Objects.equals(key.elements, elements);
}
return false;
}
/**
* Get the hash code of this object.
*
* @return Hash code.
*/
public int hashCode() {
return HashCode.generate(elements);
}
/**
* Return a string representation of this object.
*
* @return A string representation of this object.
*/
public String toString() {
return super.toString() + Strings.join(elements, "[", ",", "]");
}
/**
* Return a shallow cloned copy of this object.
*
* @return Shallow cloned copy of this object.
*/
public Object clone() {
try {
return super.clone();
}
catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
}
1.1
jboss-common/src/main/org/jboss/util/collection/EmptyCollectionException.java
Index: EmptyCollectionException.java
===================================================================
/***************************************
* *
* JBoss: The OpenSource J2EE WebOS *
* *
* Distributable under LGPL license. *
* See terms of license at gnu.org. *
* *
***************************************/
package org.jboss.util.collection;
/**
* Thrown to indicate that an operation can not be performed on an empty
* collection.
*
* @version <tt>$Revision: 1.1 $</tt>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
*/
public class EmptyCollectionException
extends CollectionException
{
/**
* Construct a <code>EmptyCollectionException</code> with the specified
* detail message.
*
* @param msg Detail message.
*/
public EmptyCollectionException(String msg) {
super(msg);
}
/**
* Construct a <code>EmptyCollectionException</code> with no detail.
*/
public EmptyCollectionException() {
super();
}
}
1.1
jboss-common/src/main/org/jboss/util/collection/FullCollectionException.java
Index: FullCollectionException.java
===================================================================
/***************************************
* *
* JBoss: The OpenSource J2EE WebOS *
* *
* Distributable under LGPL license. *
* See terms of license at gnu.org. *
* *
***************************************/
package org.jboss.util.collection;
/**
* Thrown to indicate that an operation can not be performed on a full
* collection.
*
* @version <tt>$Revision: 1.1 $</tt>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
*/
public class FullCollectionException
extends CollectionException
{
/**
* Construct a <code>FullCollectionException</code> with the specified
* detail message.
*
* @param msg Detail message.
*/
public FullCollectionException(String msg) {
super(msg);
}
/**
* Construct a <code>FullCollectionException</code> with no detail.
*/
public FullCollectionException() {
super();
}
}
1.1 jboss-common/src/main/org/jboss/util/collection/Iterators.java
Index: Iterators.java
===================================================================
/***************************************
* *
* JBoss: The OpenSource J2EE WebOS *
* *
* Distributable under LGPL license. *
* See terms of license at gnu.org. *
* *
***************************************/
package org.jboss.util.collection;
import java.util.Iterator;
import java.util.Enumeration;
import java.util.NoSuchElementException;
import java.util.Map;
import java.util.HashMap;
import org.jboss.util.Null;
/**
* A collection of <code>Iterator</code> and <code>Enumeration</code>
* utilities.
*
* @version <tt>$Revision: 1.1 $</tt>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
*/
public final class Iterators
{
/////////////////////////////////////////////////////////////////////////
// Enumeration/Iterator Conversion //
/////////////////////////////////////////////////////////////////////////
/**
* An Enumeration to Iterator wrapper.
*/
private static final class Enum2Iterator
implements Iterator
{
private final Enumeration enum;
public Enum2Iterator(final Enumeration enum) {
this.enum = enum;
}
public boolean hasNext() {
return enum.hasMoreElements();
}
public Object next() {
return enum.nextElement();
}
public void remove() {
throw new UnsupportedOperationException("Enumerations are immutable");
}
}
/**
* Return an Iterator wrapper for the given Enumeration
*
* @param enum Enumeration to wrap
* @return Enumeration wrapped as an Iterator
*/
public static Iterator forEnumeration(final Enumeration enum) {
return new Enum2Iterator(enum);
}
/**
* An Iterator to Enumeration wrapper class.
*/
private static final class Iter2Enumeration
implements Enumeration
{
private final Iterator iter;
public Iter2Enumeration(final Iterator iter) {
this.iter = iter;
}
public boolean hasMoreElements() {
return iter.hasNext();
}
public Object nextElement() {
return iter.next();
}
}
/**
* Return an Enumeration for the given Iterator.
*
* @param iter Iterator to wrap.
* @return Enumeration wrapper.
*/
public static Enumeration toEnumeration(final Iterator iter) {
return new Iter2Enumeration(iter);
}
/////////////////////////////////////////////////////////////////////////
// Iterator Wrappers //
/////////////////////////////////////////////////////////////////////////
/**
* Wraps an Iterator making it immutable, by disabling calls to
* <code>remove()</code>
*/
private static final class ImmutableIterator
implements Iterator
{
private final Iterator iter;
public ImmutableIterator(final Iterator iter) {
this.iter = iter;
}
public boolean hasNext() {
return iter.hasNext();
}
public Object next() {
return iter.next();
}
public void remove() {
throw new UnsupportedOperationException("iterator is immutable");
}
}
/**
* Make an Iterator immutable
*
* @param iter Iterator to make immutable
* @return Imutable iterator
*/
public static Iterator makeImmutable(final Iterator iter) {
return new ImmutableIterator(iter);
}
/**
* Wraps an Iterator making it synchronized.
*/
private static final class SyncIterator
implements Iterator
{
private final Iterator iter;
public SyncIterator(final Iterator iter) {
this.iter = iter;
}
public synchronized boolean hasNext() {
return iter.hasNext();
}
public synchronized Object next() {
return iter.next();
}
public synchronized void remove() {
iter.remove();
}
}
/**
* Returns a synchronized version of the given Iterator.
*
* @param iter Iterator to synchronize.
* @return Synchronized Iterator.
*/
public static Iterator makeSynchronized(final Iterator iter) {
return new SyncIterator(iter);
}
/**
* Wraps an Enumeration making it synchronized.
*/
private static final class SyncEnumeration
implements Enumeration
{
private final Enumeration enum;
public SyncEnumeration(final Enumeration enum) {
this.enum = enum;
}
public synchronized boolean hasMoreElements() {
return enum.hasMoreElements();
}
public synchronized Object nextElement() {
return enum.nextElement();
}
}
/**
* Returns a synchronized version of the given Enumeration.
*
* @param enum Enumeration to synchronize.
* @return Synchronized Enumeration.
*/
public static Enumeration makeSynchronized(final Enumeration enum) {
return new SyncEnumeration(enum);
}
/////////////////////////////////////////////////////////////////////////
// Empty Iterator //
/////////////////////////////////////////////////////////////////////////
/** An empty Iterator */
public static final Iterator EMPTY_ITERATOR = new EmptyIterator();
/**
* An empty Iterator
*/
private static final class EmptyIterator
implements Iterator
{
public boolean hasNext() {
return false;
}
public Object next() {
throw new NoSuchElementException("no more elements");
}
public void remove() {
throw new IllegalStateException("no more elements");
}
}
/////////////////////////////////////////////////////////////////////////
// Misc Methods //
/////////////////////////////////////////////////////////////////////////
/**
* Returns an Iterator containing the <i>union</i> of all of the elements
* in the given iterator array.
*
* @param iters Array of iterators.
* @return Iterator containing the <i>union</i>.
*/
public static Iterator union(final Iterator iters[]) {
Map map = new HashMap();
for (int i=0; i < iters.length; i++) {
if (iters[i] != null) {
while (iters[i].hasNext()) {
Object obj = iters[i].next();
if (!map.containsKey(obj)) {
map.put(obj, Null.VALUE);
}
}
}
}
return map.keySet().iterator();
}
/**
* Return a delimited string representation of all of the elements
* in the given Iterator.
*
* @param iter Iterator to convert to string.
* @param delim Elemement delimiter.
* @return Delimited string value.
*/
public static String toString(final Iterator iter, final String delim) {
StringBuffer buff = new StringBuffer();
while (iter.hasNext()) {
buff.append(iter.next());
if (iter.hasNext()) {
buff.append(delim);
}
}
return buff.toString();
}
/**
* Return a comma delimited string representation of all of the elements
* in the given Iterator.
*
* @param iter Iterator to convert to string.
* @return Delimited string value.
*/
public static String toString(final Iterator iter) {
return toString(iter, ",");
}
}
1.1 jboss-common/src/main/org/jboss/util/collection/ListQueue.java
Index: ListQueue.java
===================================================================
/***************************************
* *
* JBoss: The OpenSource J2EE WebOS *
* *
* Distributable under LGPL license. *
* See terms of license at gnu.org. *
* *
***************************************/
package org.jboss.util.collection;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
import org.jboss.util.NullArgumentException;
/**
* A ListQueue implements a first-in, first-out container using a List as
* a data structure.
*
* @version <tt>$Revision: 1.1 $</tt>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
*/
public class ListQueue
extends AbstractQueue
{
/** List container */
protected final List list;
/**
* Construct a new <i>constrained</i> ListQueue.
*
* @param list The list which will be used to store queued objects.
* @param maxSize The maximum size of the queue.
*
* @exception IllegalArgumentException List is <i>null</i>.
*/
public ListQueue(final List list, final int maxSize) {
super(maxSize);
if (list == null)
throw new NullArgumentException("list");
this.list = list;
}
/**
* Construct a new <i>constrained</i> ListQueue using a
* <code>LinkedList</code> for a data-structure.
*
* @param maxSize The maximum size of the queue.
*/
public ListQueue(final int maxSize) {
super(maxSize);
this.list = new LinkedList();
}
/**
* Construct a new <i>unconstrained</i> ListQueue.
*
* @param list The list which will be used to store queued objects.
*
* @exception IllegalArgumentException List is <i>null</i>
*/
public ListQueue(final List list) {
this(list, UNLIMITED_MAXIMUM_SIZE);
}
/**
* Construct a new <i>unconstrained</i> ListQueue using a
* <code>LinkedList</code> for a data-structure.
*/
public ListQueue() {
this(new LinkedList(), UNLIMITED_MAXIMUM_SIZE);
}
/**
* Appends the given element to the end of this list.
*
* @param obj Object to append.
*/
protected boolean addLast(final Object obj) {
return list.add(obj);
}
/**
* Remove the first object in the queue.
*
* @return First object in the queue.
*/
protected Object removeFirst() {
return list.remove(0);
}
/**
* Get the size of the queue.
*
* @return The number of elements in the queue.
*/
public int size() {
return list.size();
}
/**
* Returns an iterator over the elements in this list in proper sequence.
*
* @return An iterator over the elements in this list in proper sequence.
*/
public Iterator iterator() {
return list.iterator();
}
/**
* Get the object at the front of the queue.
*
* @return Object at the front of the queue.
*
* @exception EmptyCollectionException The queue is empty.
*/
public Object getFront() throws EmptyCollectionException {
if (isEmpty())
throw new EmptyCollectionException();
return list.get(0);
}
/**
* Get the object at the back of the queue.
*
* @return Object at the back of the queue.
*
* @exception EmptyCollectionException The queue is empty.
*/
public Object getBack() throws EmptyCollectionException {
if (isEmpty())
throw new EmptyCollectionException();
return list.get(list.size() - 1);
}
/**
* Returns an iterator over the elements in this list in reverse sequence.
*
* @return An iterator over the elements in this list in reverse sequence.
*/
public Iterator reverseIterator() {
return new ReverseListIterator(list);
}
/**
* Return a String representation of this queue.
*
* @return String
*/
public String toString() {
return list.toString();
}
}
1.1 jboss-common/src/main/org/jboss/util/collection/ListSet.java
Index: ListSet.java
===================================================================
/***************************************
* *
* JBoss: The OpenSource J2EE WebOS *
* *
* Distributable under LGPL license. *
* See terms of license at gnu.org. *
* *
***************************************/
package org.jboss.util.collection;
import java.util.List;
import java.util.Set;
import java.util.AbstractSet;
import java.util.Iterator;
import org.jboss.util.NullArgumentException;
/**
* A thin wrapper around a <code>List</code> transforming it into a
* modifiable <code>Set</code>.
*
* @version <tt>$Revision: 1.1 $</tt>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
*/
public class ListSet
extends AbstractSet
implements Set
{
/** The <tt>List</tt> which will be used for element storage. */
protected final List list;
/**
* Construct a <tt>ListSet</tt>.
*
* @param list The <tt>List</tt> which will be used for element storage.
*
* @throws IllegalArgumentException List is <tt>null</tt> or contains
* duplicate entries.
*/
public ListSet(final List list) {
if (list == null)
throw new NullArgumentException("list");
// make sure there are no duplicates
int size = list.size();
for (int i=0; i<size; i++) {
Object obj = list.get(i);
if (list.indexOf(obj) != list.lastIndexOf(obj)) {
throw new IllegalArgumentException
("list contains duplicate entries");
}
}
this.list = list;
}
/**
* Return the size of the set.
*
* @return The size of the set.
*/
public int size() {
return list.size();
}
/**
* Return an iteration over the elements in the set.
*
* @return An iteration over the elements in the set.
*/
public Iterator iterator() {
return list.iterator();
}
/**
* Add an element to the set.
*
* @param obj Element to add to the set.
* @return True if the element was added.
*/
public boolean add(final Object obj) {
boolean added = false;
if (!list.contains(obj)) {
added = list.add(obj);
}
return added;
}
/**
* Returns <tt>true</tt> if this set contains no elements.
*
* @return <tt>true</tt> if this set contains no elements.
*/
public boolean isEmpty() {
return list.isEmpty();
}
/**
* Returns <tt>true</tt> if this set contains the specified element.
*
* @param obj Element whose presence in this set is to be tested.
* @return <tt>true</tt> if this set contains the specified element.
*/
public boolean contains(final Object obj) {
return list.contains(obj);
}
/**
* Removes the given element from this set if it is present.
*
* @param obj Object to be removed from this set, if present.
* @return <tt>true</tt> if the set contained the specified element.
*/
public boolean remove(final Object obj) {
return list.remove(obj);
}
/**
* Removes all of the elements from this set.
*/
public void clear() {
list.clear();
}
/**
* Returns a shallow copy of this <tt>ListSet</tt> instance: the elements
* themselves are not cloned.
*
* @return A shallow copy of this set.
*/
public Object clone() {
try {
return super.clone();
}
catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
}
1.1 jboss-common/src/main/org/jboss/util/collection/Queue.java
Index: Queue.java
===================================================================
/***************************************
* *
* JBoss: The OpenSource J2EE WebOS *
* *
* Distributable under LGPL license. *
* See terms of license at gnu.org. *
* *
***************************************/
package org.jboss.util.collection;
import java.util.Collection;
/**
* An iterface used to implement a first-in, first-out container.
*
* @version <tt>$Revision: 1.1 $</tt>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
*/
public interface Queue
extends Collection
{
/** Unlimited maximum queue size identifier. */
int UNLIMITED_MAXIMUM_SIZE = -1;
/**
* Get the maximum size of the queue.
*
* @return Maximum pool size or {@link #UNLIMITED_MAXIMUM_SIZE}.
*/
int getMaximumSize();
/**
* Set the maximum size of the queue.
*
* @param size New maximim pool size or {@link #UNLIMITED_MAXIMUM_SIZE}.
*
* @exception IllegalArgumentException Illegal size.
*/
void setMaximumSize(int size) throws IllegalArgumentException;
/**
* Check if the queue is full.
*
* @return True if the queue is full.
*/
boolean isFull();
/**
* Check if the queue is empty.
*
* @return True if the queue is empty.
*/
boolean isEmpty();
/**
* Enqueue an object onto the queue.
*
* @param obj Object to enqueue.
* @return True if collection was modified.
*
* @exception FullCollectionException The queue is full.
*/
boolean add(Object obj) throws FullCollectionException;
/**
* Dequeue an object from the queue.
*
* @return Dequeued object.
*
* @exception EmptyCollectionException The queue is empty.
*/
Object remove() throws EmptyCollectionException;
/**
* Get the object at the front of the queue.
*
* @return Object at the front of the queue.
*
* @exception EmptyCollectionException The queue is empty.
*/
Object getFront() throws EmptyCollectionException;
/**
* Get the object at the back of the queue.
*
* @return Object at the back of the queue.
*
* @exception EmptyCollectionException The queue is empty.
*/
Object getBack() throws EmptyCollectionException;
}
1.1
jboss-common/src/main/org/jboss/util/collection/ReverseListIterator.java
Index: ReverseListIterator.java
===================================================================
/***************************************
* *
* JBoss: The OpenSource J2EE WebOS *
* *
* Distributable under LGPL license. *
* See terms of license at gnu.org. *
* *
***************************************/
package org.jboss.util.collection;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
/**
* An iterator that returns elements in reverse order from a list.
*
* @version <tt>$Revision: 1.1 $</tt>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
*/
public class ReverseListIterator
implements Iterator
{
/** The list to get elements from */
protected final List list;
/** The current index of the list */
protected int current;
/**
* Construct a ReverseListIterator for the given list.
*
* @param list List to iterate over.
*/
public ReverseListIterator(final List list) {
this.list = list;
current = list.size() - 1;
}
/**
* Check if there are more elements.
*
* @return True if there are more elements.
*/
public boolean hasNext() {
return current > 0;
}
/**
* Get the next element.
*
* @return The next element.
*
* @throws NoSuchElementException
*/
public Object next() {
if (current <= 0) {
throw new NoSuchElementException();
}
return list.get(current--);
}
/**
* Remove the current element.
*/
public void remove() {
list.remove(current);
}
}
1.1 jboss-common/src/main/org/jboss/util/collection/WeakSet.java
Index: WeakSet.java
===================================================================
/***************************************
* *
* JBoss: The OpenSource J2EE WebOS *
* *
* Distributable under LGPL license. *
* See terms of license at gnu.org. *
* *
***************************************/
package org.jboss.util.collection;
import java.lang.ref.ReferenceQueue;
import java.util.Set;
import java.util.HashSet;
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.jboss.util.NullArgumentException;
import org.jboss.util.WeakObject;
/**
* A <tt>Set</tt> implementation with <em>weak elements</em>. An entry in
* a <tt>WeakSet</tt> will automatically be removed when the element is no
* longer in ordinary use. More precisely, the presence of an given element
* will not prevent the element from being discarded by the garbage collector,
* that is, made finalizable, finalized, and then reclaimed.
*
* @version <tt>$Revision: 1.1 $</tt>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
*/
public class WeakSet
extends AbstractSet
implements Set
{
/** The reference queue used to get object removal notifications. */
protected final ReferenceQueue queue = new ReferenceQueue();
/** The <tt>Set</tt> which will be used for element storage. */
protected final Set set;
/**
* Construct a <tt>WeakSet</tt>. Any elements in the given set will be
* wrapped in {@link WeakObject} references.
*
* @param set The <tt>Set</tt> which will be used for element storage.
*
* @throws NullArgumentException Set is <tt>null</tt>.
*/
public WeakSet(final Set set) {
if (set == null)
throw new NullArgumentException("set");
// reset any elements to weak objects
if (set.size() != 0) {
Object elements[] = set.toArray();
set.clear();
for (int i=0; i<elements.length; i++) {
add(elements[i]);
}
}
this.set = set;
}
/**
* Construct a <tt>WeakSet</tt> based on a <tt>HashSet</tt>.
*/
public WeakSet() {
this(new HashSet());
}
/**
* Maintain the elements in the set. Removes objects from the set that
* have been reclaimed due to GC.
*/
protected final void maintain() {
WeakObject weak;
while ((weak = (WeakObject)queue.poll()) != null) {
set.remove(weak);
}
}
/**
* Return the size of the set.
*
* @return The size of the set.
*/
public int size() {
maintain();
return set.size();
}
/**
* Return an iteration over the elements in the set.
*
* @return An iteration over the elements in the set.
*/
public Iterator iterator() {
return new Iterator() {
/** The set's iterator */
Iterator iter = set.iterator();
/** The next available object. */
Object next = null;
public boolean hasNext() {
while (iter.hasNext()) {
WeakObject weak = (WeakObject)iter.next();
Object obj = null;
if (weak != null && (obj = weak.get()) == null) {
// object has been reclaimed by the GC
continue;
}
next = obj;
return true;
}
return false;
}
public Object next() {
if ((next == null) && !hasNext()) {
throw new NoSuchElementException();
}
Object obj = next;
next = null;
return obj;
}
public void remove() {
iter.remove();
}
};
}
/**
* Add an element to the set.
*
* @param obj Element to add to the set.
* @return True if the element was added.
*/
public boolean add(final Object obj) {
maintain();
return set.add(WeakObject.create(obj, queue));
}
/**
* Returns <tt>true</tt> if this set contains no elements.
*
* @return <tt>true</tt> if this set contains no elements.
*/
public boolean isEmpty() {
maintain();
return set.isEmpty();
}
/**
* Returns <tt>true</tt> if this set contains the specified element.
*
* @param obj Element whose presence in this set is to be tested.
* @return <tt>true</tt> if this set contains the specified element.
*/
public boolean contains(final Object obj) {
maintain();
return set.contains(WeakObject.create(obj));
}
/**
* Removes the given element from this set if it is present.
*
* @param obj Object to be removed from this set, if present.
* @return <tt>true</tt> if the set contained the specified element.
*/
public boolean remove(final Object obj) {
maintain();
return set.remove(WeakObject.create(obj));
}
/**
* Removes all of the elements from this set.
*/
public void clear() {
set.clear();
}
/**
* Returns a shallow copy of this <tt>WeakSet</tt> instance: the elements
* themselves are not cloned.
*
* @return A shallow copy of this set.
*/
public Object clone() {
maintain();
try {
return super.clone();
}
catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
}
1.1 jboss-common/src/main/org/jboss/util/collection/package.html
Index: package.html
===================================================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<!-- $Id: package.html,v 1.1 2002/02/15 06:14:22 user57 Exp $ -->
<!--
JBoss: The OpenSource J2EE WebOS
Distributable under LGPL license.
See terms of license at gnu.org.
-->
</head>
<body bgcolor="white">
<p>Extentions to the <i>Java Collections framework</i>.<p>
<h2>Package Specification</h2>
<ul>
<li><a href="javascript: alert('not available')">Not Available</a>
</ul>
<h2>Related Documentation</h2>
<ul>
<li><a href="javascript: alert('not available')">Not Available</a>
</ul>
<h2>Package Status</h2>
<ul>
<li><font color="green"><b>STABLE</b></font>
</ul>
<h2>Todo</h2>
<ul>
<li>???
</ul>
<!-- Put @see and @since tags down here. -->
</body>
</html>
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development