--- projects/cms/source/util/uk/org/iscream/cms/util/Queue.java 2001/03/14 23:25:29 1.16 +++ projects/cms/source/util/uk/org/iscream/cms/util/Queue.java 2001/03/26 17:59:47 1.20 @@ -14,7 +14,7 @@ import uk.org.iscream.util.*; * actually contains some elements. * * @author $Author: tdb $ - * @version $Id: Queue.java,v 1.16 2001/03/14 23:25:29 tdb Exp $ + * @version $Id: Queue.java,v 1.20 2001/03/26 17:59:47 tdb Exp $ */ public class Queue { @@ -23,7 +23,7 @@ public class Queue { /** * The current CVS revision of this class */ - public static final String REVISION = "$Revision: 1.16 $"; + public static final String REVISION = "$Revision: 1.20 $"; /** * Pass to constructor to remove a RANDOM item from @@ -49,6 +49,11 @@ public class Queue { */ public static final int DROP = 3; + /** + * To allow opposite lookups. + */ + public static final String[] algorithms = {"RANDOM", "FIRST", "LAST", "DROP"}; + //---STATIC METHODS--- //---CONSTRUCTORS--- @@ -92,12 +97,12 @@ public class Queue { // get size before adding to the Queue int s = ((LinkedList) _lists.get(i)).size(); // check whether we need to remove an item from the current Queue - if(_maxSize!=-1 && s==_maxSize && _removeAlgorithm!=DROP) { + if(_maxSize!=-1 && s>=_maxSize && _removeAlgorithm!=DROP) { // we need to remove an item removeQueueItem((LinkedList) _lists.get(i)); } // check if we should add (not if Queue full, and using DROP algorithm) - if(!(s==_maxSize && _removeAlgorithm==DROP)) { + if(!(s>=_maxSize && _removeAlgorithm==DROP)) { // add the next item, ensuring we lock synchronized(this) { // LinkedList.add() does the same thing, but this ensures behaviour @@ -106,10 +111,8 @@ public class Queue { } // if the queue was empty before the add it is possible // that a consumer is waiting... so we notify them - if (s == 0) { - synchronized(((LinkedList) _lists.get(i))) { - ((LinkedList) _lists.get(i)).notifyAll(); - } + synchronized(((LinkedList) _lists.get(i))) { + ((LinkedList) _lists.get(i)).notifyAll(); } } } @@ -131,8 +134,8 @@ public class Queue { throw new InvalidQueueException("Requested queue "+queue+" does not exist"); } // block if the queue is empty - if (((LinkedList) _lists.get(queue)).size() == 0) { - synchronized(((LinkedList) _lists.get(queue))) { + synchronized(((LinkedList) _lists.get(queue))) { + if (((LinkedList) _lists.get(queue)).size() == 0) { try { ((LinkedList) _lists.get(queue)).wait(); } catch(Exception e) {} } } @@ -242,7 +245,7 @@ public class Queue { * * @return An integer to be passed to the get() method. */ - public int getQueue() { + public synchronized int getQueue() { int pos = -1; for(int i=0; i < _lists.size(); i++) { if(_lists.get(i) == null) { @@ -266,7 +269,7 @@ public class Queue { * * @param queue The integer identifier for the queue, given by getQueue(). */ - public void removeQueue(int queue) { + public synchronized void removeQueue(int queue) { _lists.set(queue, null); }