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
     1 package org.apidesign.openfixed;
     2 
     3 /** Sample bean using the {@link ModificationListener}
     4  * to <b>add</b> numbers.
     5  *
     6  * @author Jaroslav Tulach <jtulach@netbeans.org>
     7  */
     8 public final class Calculator {
     9     private final EventSupport listeners;
    10     private int sum;
    11 
    12     private Calculator(EventSupport listeners) {
    13         this.listeners = listeners;
    14     }
    15     
    16     public static Calculator create() {
    17         return new Calculator(new TrivialEventSupport());
    18     }
    19 
    20     public static Calculator createAsynch() {
    21         return new Calculator(new AsyncEventSupport());
    22     }
    23 
    24     /** @since 2.0 */
    25     public static Calculator createPending() {
    26         return new Calculator(new PendingEventSupport());
    27     }
    28 
    29     /** @since 3.0 */
    30     public static Calculator createBatch() {
    31         return new Calculator(new PostEventSupport());
    32     }
    33 
    34     public synchronized void add(int add) {
    35         sum += add;
    36         listeners.fireModificationEvent(new ModificationEvent(this, add));
    37     }
    38     
    39     public synchronized int getSum() {
    40         return sum;
    41     }
    42     
    43     public void addModificationListener(ModificationListener l) {
    44         listeners.add(l);
    45     }
    46     public void removeModificationListener(ModificationListener l) {
    47         listeners.remove(l);
    48     }
    49 }