View Javadoc

1   /*
2    * Copyright (c) 2005, The K-Wf Grid Consortium
3    * Fraunhofer Institute for Computer Architecture and Software Technology
4    * See http://www.kwfgrid.eu and http://www.first.fraunhofer.de for more details.
5    */
6   package net.kwfgrid.gwes.uiproxy;
7   
8   import java.util.*;
9   
10  /**
11     Default implementation of buffer. Just a simple FIFO mechanism.
12     This buffer supports the following properties:
13     <ul>
14     <li>Buffer.size</li>
15     </ul>
16   */
17  public class DefaultBuffer extends AbstractBuffer {
18      protected List _content;
19      protected boolean _overflow;
20  
21      public DefaultBuffer() {
22  	super();
23  	_content = new ArrayList(getSize());
24  	_overflow = false;
25      }
26           
27      public synchronized void handle(Object message) {
28  	if (shouldHandle(message)) {
29  	    if (_content.size()>=getSize()) _overflow = true;
30  	    else _content.add(message);
31  	}
32      }
33      
34      public synchronized Object[] read() throws BufferException {
35  	if (_overflow) throw new BufferOverflowException(this);
36  	Object[] messages = _content.toArray();
37  	_content.clear();
38  	return messages;
39      }
40      
41      /**
42         Determine if this buffer should handle the specified message.
43         The default implementation always returns <code>true</code>. 
44         Subclasses must override this method in order to sort out messages of interest.
45       */
46      protected boolean shouldHandle(Object message) {
47  	return true;
48      }
49  }