samples/openfixed/src/org/apidesign/openfixed/Calculator.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 30 Oct 2014 21:30:10 +0100
changeset 409 40cabcdcd2be
parent 375 3abae898011d
permissions -rw-r--r--
Updating to NBMs from NetBeans 8.0.1 as some of them are required to run on JDK8
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@395
    16
jtulach@395
    17
    /** An abstraction over various types of event delivery
jtulach@395
    18
     * to listeners. Comes with four different implementations.
jtulach@395
    19
     * A trivial one, asynchronous one, one with support for
jtulach@395
    20
     * pending events and one for a batch events delivery.
jtulach@395
    21
     */
jtulach@395
    22
    interface EventSupport {
jtulach@395
    23
        public void fireModificationEvent(ModificationEvent ev);
jtulach@395
    24
        public void add(ModificationListener l);
jtulach@395
    25
        public void remove(ModificationListener l);
jtulach@395
    26
    }
jtulach@373
    27
    
jtulach@374
    28
    public static Calculator create() {
jtulach@374
    29
        return new Calculator(new TrivialEventSupport());
jtulach@374
    30
    }
jtulach@374
    31
jtulach@374
    32
    public static Calculator createAsynch() {
jtulach@374
    33
        return new Calculator(new AsyncEventSupport());
jtulach@374
    34
    }
jtulach@374
    35
jtulach@374
    36
    /** @since 2.0 */
jtulach@374
    37
    public static Calculator createPending() {
jtulach@374
    38
        return new Calculator(new PendingEventSupport());
jtulach@374
    39
    }
jtulach@374
    40
jtulach@374
    41
    /** @since 3.0 */
jtulach@374
    42
    public static Calculator createBatch() {
jtulach@374
    43
        return new Calculator(new PostEventSupport());
jtulach@374
    44
    }
jtulach@374
    45
jtulach@374
    46
    public synchronized void add(int add) {
jtulach@374
    47
        sum += add;
jtulach@374
    48
        listeners.fireModificationEvent(new ModificationEvent(this, add));
jtulach@374
    49
    }
jtulach@374
    50
    
jtulach@374
    51
    public synchronized int getSum() {
jtulach@374
    52
        return sum;
jtulach@374
    53
    }
jtulach@374
    54
    
jtulach@374
    55
    public void addModificationListener(ModificationListener l) {
jtulach@373
    56
        listeners.add(l);
jtulach@373
    57
    }
jtulach@374
    58
    public void removeModificationListener(ModificationListener l) {
jtulach@373
    59
        listeners.remove(l);
jtulach@373
    60
    }
jtulach@373
    61
}
jtulach@375
    62
// END: openfixed.bean