KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.event;
9
10 import java.util.ArrayList JavaDoc;
11
12 /**
13  * The default queue implementation is a variable size queue.
14  *
15  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
16  */

17 public abstract class AbstractQueue implements Queue
18 {
19     protected long m_timeout = 0;
20
21     /**
22      * Default for canAccept()
23      */

24     public int canAccept()
25     {
26         return ( maxSize() > 0 ) ? maxSize() - size() : maxSize();
27     }
28
29     /**
30      * Default maxSize to -1 which is unbounded
31      */

32     public int maxSize()
33     {
34         return -1;
35     }
36
37     /**
38      * Default for isFull()
39      */

40     public boolean isFull()
41     {
42         return maxSize() - size() > 0;
43     }
44
45     /**
46      * Set the timeout
47      */

48     public void setTimeout( final long millis )
49     {
50         if ( millis > 0 )
51         {
52             m_timeout = millis;
53         }
54         else
55         {
56             m_timeout = 0;
57         }
58     }
59
60     protected void block( Object JavaDoc lock )
61     {
62         if ( m_timeout > 0 )
63         {
64             long start = System.currentTimeMillis();
65             long end = start + m_timeout;
66
67             while ( start < end || size() > 0 )
68             {
69                 try
70                 {
71                     lock.wait( m_timeout );
72                 }
73                 catch ( InterruptedException JavaDoc ie )
74                 {
75                     // ignore
76
}
77             }
78         }
79     }
80 }
81
Popular Tags