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
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@375
     8
// BEGIN: openfixed.bean
jtulach@374
     9
public final class Calculator {
jtulach@374
    10
    private final EventSupport listeners;
jtulach@374
    11
    private int sum;
jtulach@374
    12
jtulach@374
    13
    private Calculator(EventSupport listeners) {
jtulach@374
    14
        this.listeners = listeners;
jtulach@374
    15
    }
jtulach@373
    16
    
jtulach@374
    17
    public static Calculator create() {
jtulach@374
    18
        return new Calculator(new TrivialEventSupport());
jtulach@374
    19
    }
jtulach@374
    20
jtulach@374
    21
    public static Calculator createAsynch() {
jtulach@374
    22
        return new Calculator(new AsyncEventSupport());
jtulach@374
    23
    }
jtulach@374
    24
jtulach@374
    25
    /** @since 2.0 */
jtulach@374
    26
    public static Calculator createPending() {
jtulach@374
    27
        return new Calculator(new PendingEventSupport());
jtulach@374
    28
    }
jtulach@374
    29
jtulach@374
    30
    /** @since 3.0 */
jtulach@374
    31
    public static Calculator createBatch() {
jtulach@374
    32
        return new Calculator(new PostEventSupport());
jtulach@374
    33
    }
jtulach@374
    34
jtulach@374
    35
    public synchronized void add(int add) {
jtulach@374
    36
        sum += add;
jtulach@374
    37
        listeners.fireModificationEvent(new ModificationEvent(this, add));
jtulach@374
    38
    }
jtulach@374
    39
    
jtulach@374
    40
    public synchronized int getSum() {
jtulach@374
    41
        return sum;
jtulach@374
    42
    }
jtulach@374
    43
    
jtulach@374
    44
    public void addModificationListener(ModificationListener l) {
jtulach@373
    45
        listeners.add(l);
jtulach@373
    46
    }
jtulach@374
    47
    public void removeModificationListener(ModificationListener l) {
jtulach@373
    48
        listeners.remove(l);
jtulach@373
    49
    }
jtulach@373
    50
}
jtulach@375
    51
// END: openfixed.bean