samples/openfixed/src/org/apidesign/openfixed/Calculator.java
changeset 374 35da2d439e3d
parent 373 c20d1d8ef2ca
child 375 3abae898011d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/openfixed/src/org/apidesign/openfixed/Calculator.java	Sun Mar 20 18:52:47 2011 +0100
     1.3 @@ -0,0 +1,49 @@
     1.4 +package org.apidesign.openfixed;
     1.5 +
     1.6 +/** Sample bean using the {@link ModificationListener}
     1.7 + * to <b>add</b> numbers.
     1.8 + *
     1.9 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.10 + */
    1.11 +public final class Calculator {
    1.12 +    private final EventSupport listeners;
    1.13 +    private int sum;
    1.14 +
    1.15 +    private Calculator(EventSupport listeners) {
    1.16 +        this.listeners = listeners;
    1.17 +    }
    1.18 +    
    1.19 +    public static Calculator create() {
    1.20 +        return new Calculator(new TrivialEventSupport());
    1.21 +    }
    1.22 +
    1.23 +    public static Calculator createAsynch() {
    1.24 +        return new Calculator(new AsyncEventSupport());
    1.25 +    }
    1.26 +
    1.27 +    /** @since 2.0 */
    1.28 +    public static Calculator createPending() {
    1.29 +        return new Calculator(new PendingEventSupport());
    1.30 +    }
    1.31 +
    1.32 +    /** @since 3.0 */
    1.33 +    public static Calculator createBatch() {
    1.34 +        return new Calculator(new PostEventSupport());
    1.35 +    }
    1.36 +
    1.37 +    public synchronized void add(int add) {
    1.38 +        sum += add;
    1.39 +        listeners.fireModificationEvent(new ModificationEvent(this, add));
    1.40 +    }
    1.41 +    
    1.42 +    public synchronized int getSum() {
    1.43 +        return sum;
    1.44 +    }
    1.45 +    
    1.46 +    public void addModificationListener(ModificationListener l) {
    1.47 +        listeners.add(l);
    1.48 +    }
    1.49 +    public void removeModificationListener(ModificationListener l) {
    1.50 +        listeners.remove(l);
    1.51 +    }
    1.52 +}