001         package com.croftsoft.core.util.queue;
002    
003         import java.util.*;
004    
005         import com.croftsoft.core.lang.NullArgumentException;
006    
007         /*********************************************************************
008         * An implementation of Queue that relies upon a Vector backbone.
009         *
010         * @author
011         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
012         * @version
013         *   2003-05-22
014         * @since
015         *   1999-02-07
016         *********************************************************************/
017    
018         public final class  VectorQueue
019           implements Queue
020         //////////////////////////////////////////////////////////////////////
021         //////////////////////////////////////////////////////////////////////
022         {
023    
024         private final Vector  vector;
025    
026         //////////////////////////////////////////////////////////////////////
027         //////////////////////////////////////////////////////////////////////
028    
029         public  VectorQueue ( Vector  vector )
030         //////////////////////////////////////////////////////////////////////
031         {
032           NullArgumentException.check ( this.vector = vector );
033         }
034    
035         public  VectorQueue ( )
036         //////////////////////////////////////////////////////////////////////
037         {
038           this ( new Vector ( ) );
039         }
040    
041         //////////////////////////////////////////////////////////////////////
042         //////////////////////////////////////////////////////////////////////
043    
044         public boolean  append ( Object  o )
045         //////////////////////////////////////////////////////////////////////
046         {
047           NullArgumentException.check ( o );
048    
049           synchronized ( vector )
050           {
051             vector.addElement ( o );
052    
053             vector.notifyAll ( );
054           }
055    
056           return true;
057         }
058    
059         public Object  poll ( )
060         //////////////////////////////////////////////////////////////////////
061         {
062           synchronized ( vector )
063           {
064             if ( vector.size ( ) > 0 )
065             {
066               Object  o = vector.elementAt ( 0 );
067    
068               vector.removeElementAt ( 0 );
069    
070               return o;
071             }
072           }
073    
074           return null;
075         }
076    
077         public Object  pull ( )
078           throws InterruptedException
079         //////////////////////////////////////////////////////////////////////
080         {
081           return pull ( 0 );
082         }
083    
084         public Object  pull ( long  timeout )
085           throws InterruptedException
086         //////////////////////////////////////////////////////////////////////
087         {
088           if ( timeout < 0 )
089           {
090             throw new IllegalArgumentException ( "timeout < 0" );
091           }
092    
093           long  stopTime = System.currentTimeMillis ( ) + timeout;
094    
095           Object  o = null;
096    
097           while ( ( o = poll ( ) ) == null )
098           {
099             if ( timeout == 0 )
100             {
101               synchronized ( vector )
102               {
103                 vector.wait ( );
104               }
105             }
106             else
107             {
108               long  now = System.currentTimeMillis ( );
109    
110               if ( stopTime > now )
111               {
112                 synchronized ( vector )
113                 {
114                   vector.wait ( stopTime - now );
115                 }
116               }
117               else
118               {
119                 break;
120               }
121             }
122           }
123    
124           return o;
125         }
126    
127         public Object  replace ( Object  o )
128         //////////////////////////////////////////////////////////////////////
129         {
130           synchronized ( vector )
131           {
132             int  index = vector.indexOf ( o );
133    
134             if ( index < 0 )
135             {
136               append ( o );
137    
138               return null;
139             }
140             else
141             {
142               Object  oldObject = vector.elementAt ( index );
143    
144               vector.setElementAt ( o, index );
145    
146               return oldObject;
147             }
148           }
149         }
150    
151         //////////////////////////////////////////////////////////////////////
152         //////////////////////////////////////////////////////////////////////
153         }