KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > event > impl > AbstractQueue


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.event.impl;
18
19 import org.apache.excalibur.event.DequeueInterceptor;
20 import org.apache.excalibur.event.EnqueuePredicate;
21 import org.apache.excalibur.event.Queue;
22
23 /**
24  * Provides the base functionality for the other <code>Queue</code> types.
25  *
26  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
27  */

28 public abstract class AbstractQueue implements Queue
29 {
30     /** An empty array used as a return value when the Queue is empty */
31     protected final static Object JavaDoc[] EMPTY_ARRAY = new Object JavaDoc[ 0 ];
32     /** The number of milliseconds to wait */
33     protected long m_timeout = 0;
34     protected EnqueuePredicate m_predicate = new NullEnqueuePredicate();
35     protected DequeueInterceptor m_interceptor = new NullDequeueInterceptor();
36
37     /**
38      * Default for canAccept()
39      *
40      * @return how many elements we can enqueue
41      */

42     public int canAccept()
43     {
44         return ( maxSize() > 0 ) ? maxSize() - size() : maxSize();
45     }
46
47     /**
48      * Default maxSize to -1 which is unbounded
49      *
50      * @return the maximum number of elements
51      */

52     public int maxSize()
53     {
54         return -1;
55     }
56
57     /**
58      * Check to see if the <code>Queue</code> is full. The method uses the
59      * <code>maxSize</code> and <code>size</code> methods to determine
60      * whether the queue is full.
61      *
62      * @return true if there is no room in the Queue
63      */

64     public boolean isFull()
65     {
66         return maxSize() != -1 /* There exists an upper bound... */
67             && maxSize() - size() <= 0; /* ...and it is reached. */
68     }
69
70     /**
71      * Set the timeout for the <code>Queue</code> in milliseconds. The
72      * default timeout is 0, which means that we don't wait at all.
73      *
74      * @param millis The number of milliseconds to block waiting for events to be enqueued
75      */

76     public void setTimeout( final long millis )
77     {
78         if( millis > 0 )
79         {
80             m_timeout = millis;
81         }
82         else
83         {
84             m_timeout = 0;
85         }
86     }
87
88     /**
89      * Encapsulates the logic to block the <code>Queue</code> for the amount
90      * of time specified by the timeout.
91      *
92      * @param lock The object used as the mutex.
93      */

94     protected void block( Object JavaDoc lock )
95     {
96         if( m_timeout > 0 )
97         {
98             long start = System.currentTimeMillis();
99             long end = start + m_timeout;
100
101             while( start < end || size() > 0 )
102             {
103                 try
104                 {
105                     lock.wait( m_timeout );
106                 }
107                 catch( InterruptedException JavaDoc ie )
108                 {
109                     // ignore
110
}
111             }
112         }
113     }
114
115     /**
116      * Set the EnqueuePredicate to limit entries into this Queue.
117      */

118     public void setEnqueuePredicate( EnqueuePredicate predicate )
119     {
120         if ( null == predicate ) throw new NullPointerException JavaDoc( "predicate" );
121
122         m_predicate = predicate;
123     }
124
125     /**
126      * Return the EnqueuePredicate that is already set for this Queue.
127      */

128     public EnqueuePredicate getEnqueuePredicate()
129     {
130         return m_predicate;
131     }
132
133     /**
134      * Set the dequeue executable for this sink. This mechanism
135      * allows users to define a methods that will be executed
136      * before or after dequeuing elements from a source
137      * @since Sep 23, 2002
138      *
139      * @param executable
140      * The dequeue executable for this sink.
141      */

142     public void setDequeueInterceptor(DequeueInterceptor executable)
143     {
144         if ( null == executable ) throw new NullPointerException JavaDoc( "executable" );
145
146         m_interceptor = executable;
147     }
148
149     /**
150      * Return the dequeue executable for this sink.
151      * @since Sep 23, 2002
152      *
153      * @return {@link DequeueInterceptor}
154      * The dequeue executable for this sink.
155      */

156     public DequeueInterceptor getDequeueInterceptor()
157     {
158         return m_interceptor;
159     }
160 }
161
Popular Tags