samples/openfixed/src/org/apidesign/openfixed/PostEventSupport.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
     1 package org.apidesign.openfixed;
     2 
     3 import java.util.HashSet;
     4 import java.util.List;
     5 import java.util.Set;
     6 import java.util.concurrent.CopyOnWriteArrayList;
     7 import java.util.concurrent.Executor;
     8 import java.util.concurrent.Executors;
     9 import org.apidesign.openfixed.Calculator.EventSupport;
    10 
    11 /**
    12  *
    13  * @author Jaroslav Tulach <jtulach@netbeans.org>
    14  */
    15 final class PostEventSupport implements EventSupport, Runnable {
    16     private final List<ModificationListener> listeners = new CopyOnWriteArrayList<ModificationListener>();
    17     private final List<Deliverable> deliverables = new CopyOnWriteArrayList<Deliverable>();
    18     private static final Executor EXEC = Executors.newSingleThreadExecutor();
    19     
    20     PostEventSupport() {
    21     }
    22 
    23     @Override
    24     public void fireModificationEvent(ModificationEvent ev) {
    25         synchronized (deliverables) {
    26             final Deliverable d = new Deliverable(ev, listeners.toArray(new ModificationListener[0]));
    27             deliverables.add(d);
    28             EXEC.execute(this);
    29         }
    30     }
    31 
    32     @Override
    33     public void add(ModificationListener l) {
    34         listeners.add(l);
    35     }
    36 
    37     @Override
    38     public void remove(ModificationListener l) {
    39         listeners.remove(l);
    40     }
    41 
    42     @Override
    43     public void run() {
    44         Deliverable[] pending;
    45         synchronized (deliverables) {
    46             if (deliverables.isEmpty()) {
    47                 return;
    48             }
    49             pending = deliverables.toArray(new Deliverable[0]);
    50             deliverables.clear();
    51         }
    52         // BEGIN: openfixed.postimpl
    53         Calculator calc = null;
    54         Set<PostModificationListener> notify;
    55         notify = new HashSet<PostModificationListener>();
    56         int pendingCount = pending.length;
    57         for (Deliverable d : pending) {
    58             calc = (Calculator)d.ev.getSource();
    59             d.ev.pending = --pendingCount;
    60             d.ev.posts = notify;
    61             for (ModificationListener l : d.listeners) {
    62                 l.modification(d.ev);
    63             }
    64             d.ev.posts = null;
    65         }
    66         for (PostModificationListener pml : notify) {
    67             pml.postProcess(new PostModificationEvent(calc));
    68         }
    69         // END: openfixed.postimpl
    70     }
    71     
    72     private static class Deliverable {
    73         final ModificationEvent ev;
    74         final ModificationListener[] listeners;
    75 
    76         public Deliverable(ModificationEvent ev, ModificationListener[] listeners) {
    77             this.ev = ev;
    78             this.listeners = listeners;
    79         }
    80     }
    81 }