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