01: /*
02: * Copyright (c) 2001 by Matt Welsh and The Regents of the University of
03: * California. All rights reserved.
04: *
05: * Permission to use, copy, modify, and distribute this software and its
06: * documentation for any purpose, without fee, and without written agreement is
07: * hereby granted, provided that the above copyright notice and the following
08: * two paragraphs appear in all copies of this software.
09: *
10: * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
11: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
12: * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
13: * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14: *
15: * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
18: * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
19: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
20: *
21: * Author: Matt Welsh <mdw@cs.berkeley.edu>
22: *
23: */
24:
25: package seda.sandStorm.core;
26:
27: import seda.sandStorm.api.*;
28:
29: /**
30: * This enqueue predicate implements a simple threshold for the
31: * size of the queue.
32: */
33: public class QueueThresholdPredicate implements EnqueuePredicateIF {
34:
35: private static final boolean DEBUG = false;
36:
37: private SinkIF thesink;
38: private int threshold;
39:
40: /**
41: * Create a new QueueThresholdPredicate for the given sink and
42: * threshold. A threshold of -1 indicates no threshold.
43: */
44: public QueueThresholdPredicate(SinkIF sink, int threshold) {
45: this .thesink = sink;
46: this .threshold = threshold;
47: }
48:
49: /**
50: * Returns true if the given element can be accepted into the queue.
51: */
52: public boolean accept(QueueElementIF qel) {
53: if (DEBUG)
54: System.err.println("QueueThresholdPredicate.accept ["
55: + thesink + "]: size " + thesink.size()
56: + ", thresh " + threshold);
57: if (threshold == -1)
58: return true;
59: if ((thesink.size() + 1) > threshold)
60: return false;
61: return true;
62: }
63:
64: /**
65: * Return the current queue threshold.
66: */
67: public int getThreshold() {
68: return threshold;
69: }
70:
71: /**
72: * Set the current queue threshold. A queue threshold of -1 indicates
73: * an infinite threshold.
74: */
75: public void setThreshold(int threshold) {
76: this.threshold = threshold;
77: }
78:
79: }
|