diff options
author | Angelo Corsaro <angelo@icorsaro.net> | 2001-02-03 01:49:55 +0000 |
---|---|---|
committer | Angelo Corsaro <angelo@icorsaro.net> | 2001-02-03 01:49:55 +0000 |
commit | a42e0462455b2aa96d0b415c96828702e0efee7b (patch) | |
tree | a81f36fb1155daf38d746016e78545b33f06834e /RTJava/examples | |
parent | fd6285fe574cbe269b767b18d088c21617485786 (diff) | |
download | ATCD-a42e0462455b2aa96d0b415c96828702e0efee7b.tar.gz |
Fri Feb 02 19:49:28 2001 Angelo Corsaro <corsaro@cs.wustl.edu>
Diffstat (limited to 'RTJava/examples')
-rw-r--r-- | RTJava/examples/tank/Refiller.java | 26 | ||||
-rw-r--r-- | RTJava/examples/tank/Tank.java | 72 |
2 files changed, 98 insertions, 0 deletions
diff --git a/RTJava/examples/tank/Refiller.java b/RTJava/examples/tank/Refiller.java new file mode 100644 index 00000000000..1ceea495854 --- /dev/null +++ b/RTJava/examples/tank/Refiller.java @@ -0,0 +1,26 @@ +package tank; + +/** + * This class represent an activity that refills periodically + * the bucket. In this application it is assumed that this activity + * should be carried periodically. + */ +public class Refiller implements java.lang.Runnable { + + /** + * Constructs a refiller that refills the tank every + * given amount of time. + * + * @param period The period of time at which the refill should + * occur. + */ + public Refiller(int period) { + this.period = period; + } + + public Refiller(int period) { + this.period = period; + } + + protected long period; +} diff --git a/RTJava/examples/tank/Tank.java b/RTJava/examples/tank/Tank.java new file mode 100644 index 00000000000..41483d8f266 --- /dev/null +++ b/RTJava/examples/tank/Tank.java @@ -0,0 +1,72 @@ +package tank; + +/** + * This class represent a bucket from which some kind of + * liquid ca be refilled or spilled. + */ +public class Tank { + + /** + * Creates a bucket that can contains at most a given + * amount of liquid and that has initially available a + * given quantity. + * + * @param capacity The capacity of the Tank in liters. + * @param available The quantity of liquid initially available in + * the bucket. + */ + public Tank(long capacity, long available) { + this.capacity = capacity; + this.available = abailable; + } + + /** + * Refills the bucket with a given amount of liquid. + * + * @param amount The amount of liquid added to the bucket. + */ + public void refill(long amount) { + this.available = java.lang.Math.min(this.capacity, amount+this.available); + + // Here we should check for an overflow. + if (this.available > capacity) + this.overflowEvent.fire(); + } + + /** + * Spills from the bucket a given amount of liquid. + * + * @param amount The amount of liquid added to the bucket. + */ + public void spill(long amount) { + this.available = java.lang.Math.max(0, this.available-amount); + if (this.available < amount) + this.underflowEven.fire(); + } + + /** + * Adds an AsyncEventHandler that will be called by the scheduler + * each time an overflow occurs. + * + * @param handler Ther overflow handler. + */ + public void addOverflowHandler(AsyncEventHandler handler) { + this.overflowEvent.addHandler(handler); + } + + /** + * Adds an AsyncEventHandler that will be called by the scheduler + * each time an underflow occurs. + * + * @param handler Ther underflow handler. + */ + public void addUnderflowHandler(AsyncEventHandler handler) { + this.underflowEvent.addHandler(handler); + } + + protected long capacity; + protected long available; + + protected AsynchEvent overflowEvent; + protected AsynchEven underflowEvent; +} |