samples/openfixed/src/org/apidesign/openfixed/Calculator.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 20 Mar 2011 20:52:33 +0100
changeset 375 3abae898011d
parent 374 35da2d439e3d
child 395 837ae5b09036
permissions -rw-r--r--
More code snippets for openfixed project
     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 // BEGIN: openfixed.bean
     9 public final class Calculator {
    10     private final EventSupport listeners;
    11     private int sum;
    12 
    13     private Calculator(EventSupport listeners) {
    14         this.listeners = listeners;
    15     }
    16     
    17     public static Calculator create() {
    18         return new Calculator(new TrivialEventSupport());
    19     }
    20 
    21     public static Calculator createAsynch() {
    22         return new Calculator(new AsyncEventSupport());
    23     }
    24 
    25     /** @since 2.0 */
    26     public static Calculator createPending() {
    27         return new Calculator(new PendingEventSupport());
    28     }
    29 
    30     /** @since 3.0 */
    31     public static Calculator createBatch() {
    32         return new Calculator(new PostEventSupport());
    33     }
    34 
    35     public synchronized void add(int add) {
    36         sum += add;
    37         listeners.fireModificationEvent(new ModificationEvent(this, add));
    38     }
    39     
    40     public synchronized int getSum() {
    41         return sum;
    42     }
    43     
    44     public void addModificationListener(ModificationListener l) {
    45         listeners.add(l);
    46     }
    47     public void removeModificationListener(ModificationListener l) {
    48         listeners.remove(l);
    49     }
    50 }
    51 // END: openfixed.bean