summaryrefslogtreecommitdiff
path: root/RTJava/examples/tank/Tank.java
blob: 41483d8f26661057b40685350f865c16714a42c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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;
}