1 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 28 public abstract class AbstractQueue implements Queue 29 { 30 31 protected final static Object [] EMPTY_ARRAY = new Object [ 0 ]; 32 33 protected long m_timeout = 0; 34 protected EnqueuePredicate m_predicate = new NullEnqueuePredicate(); 35 protected DequeueInterceptor m_interceptor = new NullDequeueInterceptor(); 36 37 42 public int canAccept() 43 { 44 return ( maxSize() > 0 ) ? maxSize() - size() : maxSize(); 45 } 46 47 52 public int maxSize() 53 { 54 return -1; 55 } 56 57 64 public boolean isFull() 65 { 66 return maxSize() != -1 67 && maxSize() - size() <= 0; 68 } 69 70 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 94 protected void block( Object 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 ie ) 108 { 109 } 111 } 112 } 113 } 114 115 118 public void setEnqueuePredicate( EnqueuePredicate predicate ) 119 { 120 if ( null == predicate ) throw new NullPointerException ( "predicate" ); 121 122 m_predicate = predicate; 123 } 124 125 128 public EnqueuePredicate getEnqueuePredicate() 129 { 130 return m_predicate; 131 } 132 133 142 public void setDequeueInterceptor(DequeueInterceptor executable) 143 { 144 if ( null == executable ) throw new NullPointerException ( "executable" ); 145 146 m_interceptor = executable; 147 } 148 149 156 public DequeueInterceptor getDequeueInterceptor() 157 { 158 return m_interceptor; 159 } 160 } 161 | Popular Tags |