samples/openfixed/src/org/apidesign/openfixed/Calculator.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 20 Mar 2011 18:52:47 +0100
changeset 374 35da2d439e3d
parent 373 samples/openfixed/src/org/apidesign/openfixed/Growable.java@c20d1d8ef2ca
child 375 3abae898011d
permissions -rw-r--r--
Calculator and various ways to deliver changes in its counter to listeners
jtulach@373
     1
package org.apidesign.openfixed;
jtulach@373
     2
jtulach@374
     3
/** Sample bean using the {@link ModificationListener}
jtulach@374
     4
 * to <b>add</b> numbers.
jtulach@373
     5
 *
jtulach@373
     6
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@373
     7
 */
jtulach@374
     8
public final class Calculator {
jtulach@374
     9
    private final EventSupport listeners;
jtulach@374
    10
    private int sum;
jtulach@374
    11
jtulach@374
    12
    private Calculator(EventSupport listeners) {
jtulach@374
    13
        this.listeners = listeners;
jtulach@374
    14
    }
jtulach@373
    15
    
jtulach@374
    16
    public static Calculator create() {
jtulach@374
    17
        return new Calculator(new TrivialEventSupport());
jtulach@374
    18
    }
jtulach@374
    19
jtulach@374
    20
    public static Calculator createAsynch() {
jtulach@374
    21
        return new Calculator(new AsyncEventSupport());
jtulach@374
    22
    }
jtulach@374
    23
jtulach@374
    24
    /** @since 2.0 */
jtulach@374
    25
    public static Calculator createPending() {
jtulach@374
    26
        return new Calculator(new PendingEventSupport());
jtulach@374
    27
    }
jtulach@374
    28
jtulach@374
    29
    /** @since 3.0 */
jtulach@374
    30
    public static Calculator createBatch() {
jtulach@374
    31
        return new Calculator(new PostEventSupport());
jtulach@374
    32
    }
jtulach@374
    33
jtulach@374
    34
    public synchronized void add(int add) {
jtulach@374
    35
        sum += add;
jtulach@374
    36
        listeners.fireModificationEvent(new ModificationEvent(this, add));
jtulach@374
    37
    }
jtulach@374
    38
    
jtulach@374
    39
    public synchronized int getSum() {
jtulach@374
    40
        return sum;
jtulach@374
    41
    }
jtulach@374
    42
    
jtulach@374
    43
    public void addModificationListener(ModificationListener l) {
jtulach@373
    44
        listeners.add(l);
jtulach@373
    45
    }
jtulach@374
    46
    public void removeModificationListener(ModificationListener l) {
jtulach@373
    47
        listeners.remove(l);
jtulach@373
    48
    }
jtulach@373
    49
}