More code snippets for openfixed project
authorJaroslav Tulach <jtulach@netbeans.org>
Sun, 20 Mar 2011 20:52:33 +0100
changeset 3753abae898011d
parent 374 35da2d439e3d
child 376 bb34a70d36ba
More code snippets for openfixed project
samples/openfixed/src/org/apidesign/openfixed/AsyncEventSupport.java
samples/openfixed/src/org/apidesign/openfixed/Calculator.java
samples/openfixed/src/org/apidesign/openfixed/PendingEventSupport.java
samples/openfixed/src/org/apidesign/openfixed/PostEventSupport.java
samples/openfixed/src/org/apidesign/openfixed/TrivialEventSupport.java
samples/openfixed/test/org/apidesign/openfixed/CalculatorBase.java
samples/openfixed/test/org/apidesign/openfixed/PostTest.java
     1.1 --- a/samples/openfixed/src/org/apidesign/openfixed/AsyncEventSupport.java	Sun Mar 20 18:52:47 2011 +0100
     1.2 +++ b/samples/openfixed/src/org/apidesign/openfixed/AsyncEventSupport.java	Sun Mar 20 20:52:33 2011 +0100
     1.3 @@ -11,16 +11,9 @@
     1.4   */
     1.5  final class AsyncEventSupport implements EventSupport {
     1.6      private final List<ModificationListener> listeners = new CopyOnWriteArrayList<ModificationListener>();
     1.7 -    private static final Executor EXEC = Executors.newSingleThreadExecutor();
     1.8      
     1.9      AsyncEventSupport() {
    1.10      }
    1.11 -
    1.12 -    @Override
    1.13 -    public void fireModificationEvent(ModificationEvent ev) {
    1.14 -        EXEC.execute(new Deliverable(ev, listeners.toArray(new ModificationListener[0])));
    1.15 -    }
    1.16 -
    1.17      @Override
    1.18      public void add(ModificationListener l) {
    1.19          listeners.add(l);
    1.20 @@ -30,12 +23,23 @@
    1.21      public void remove(ModificationListener l) {
    1.22          listeners.remove(l);
    1.23      }
    1.24 +
    1.25 +    // BEGIN: openfixed.asynch
    1.26 +    private static final Executor EXEC = Executors.newSingleThreadExecutor();
    1.27 +    @Override
    1.28 +    public void fireModificationEvent(ModificationEvent ev) {
    1.29 +        EXEC.execute(new Deliverable(
    1.30 +            ev, listeners.toArray(new ModificationListener[0])
    1.31 +        ));
    1.32 +    }
    1.33      
    1.34      private static class Deliverable implements Runnable {
    1.35          final ModificationEvent ev;
    1.36          final ModificationListener[] listeners;
    1.37  
    1.38 -        public Deliverable(ModificationEvent ev, ModificationListener[] listeners) {
    1.39 +        public Deliverable(
    1.40 +            ModificationEvent ev, ModificationListener[] listeners
    1.41 +        ) {
    1.42              this.ev = ev;
    1.43              this.listeners = listeners;
    1.44          }
    1.45 @@ -47,4 +51,5 @@
    1.46              }
    1.47          }
    1.48      }
    1.49 +    // END: openfixed.asynch
    1.50  }
     2.1 --- a/samples/openfixed/src/org/apidesign/openfixed/Calculator.java	Sun Mar 20 18:52:47 2011 +0100
     2.2 +++ b/samples/openfixed/src/org/apidesign/openfixed/Calculator.java	Sun Mar 20 20:52:33 2011 +0100
     2.3 @@ -5,6 +5,7 @@
     2.4   *
     2.5   * @author Jaroslav Tulach <jtulach@netbeans.org>
     2.6   */
     2.7 +// BEGIN: openfixed.bean
     2.8  public final class Calculator {
     2.9      private final EventSupport listeners;
    2.10      private int sum;
    2.11 @@ -47,3 +48,4 @@
    2.12          listeners.remove(l);
    2.13      }
    2.14  }
    2.15 +// END: openfixed.bean
     3.1 --- a/samples/openfixed/src/org/apidesign/openfixed/PendingEventSupport.java	Sun Mar 20 18:52:47 2011 +0100
     3.2 +++ b/samples/openfixed/src/org/apidesign/openfixed/PendingEventSupport.java	Sun Mar 20 20:52:33 2011 +0100
     3.3 @@ -46,6 +46,7 @@
     3.4              pending = deliverables.toArray(new Deliverable[0]);
     3.5              deliverables.clear();
     3.6          }
     3.7 +        // BEGIN: openfixed.pendingCount
     3.8          int pendingCount = pending.length;
     3.9          for (Deliverable d : pending) {
    3.10              d.ev.pending = --pendingCount;
    3.11 @@ -53,6 +54,7 @@
    3.12                  l.modification(d.ev);
    3.13              }
    3.14          }
    3.15 +        // END: openfixed.pendingCount
    3.16      }
    3.17      
    3.18      private static class Deliverable {
     4.1 --- a/samples/openfixed/src/org/apidesign/openfixed/PostEventSupport.java	Sun Mar 20 18:52:47 2011 +0100
     4.2 +++ b/samples/openfixed/src/org/apidesign/openfixed/PostEventSupport.java	Sun Mar 20 20:52:33 2011 +0100
     4.3 @@ -48,8 +48,10 @@
     4.4              pending = deliverables.toArray(new Deliverable[0]);
     4.5              deliverables.clear();
     4.6          }
     4.7 +        // BEGIN: openfixed.postimpl
     4.8          Calculator calc = null;
     4.9 -        Set<PostModificationListener> notify = new HashSet<PostModificationListener>();
    4.10 +        Set<PostModificationListener> notify;
    4.11 +        notify = new HashSet<PostModificationListener>();
    4.12          int pendingCount = pending.length;
    4.13          for (Deliverable d : pending) {
    4.14              calc = (Calculator)d.ev.getSource();
    4.15 @@ -60,10 +62,10 @@
    4.16              }
    4.17              d.ev.posts = null;
    4.18          }
    4.19 -        
    4.20          for (PostModificationListener pml : notify) {
    4.21              pml.postProcess(new PostModificationEvent(calc));
    4.22          }
    4.23 +        // END: openfixed.postimpl
    4.24      }
    4.25      
    4.26      private static class Deliverable {
     5.1 --- a/samples/openfixed/src/org/apidesign/openfixed/TrivialEventSupport.java	Sun Mar 20 18:52:47 2011 +0100
     5.2 +++ b/samples/openfixed/src/org/apidesign/openfixed/TrivialEventSupport.java	Sun Mar 20 20:52:33 2011 +0100
     5.3 @@ -17,12 +17,14 @@
     5.4      TrivialEventSupport() {
     5.5      }
     5.6  
     5.7 +    // BEGIN: openfixed.trivial
     5.8      @Override
     5.9      public void fireModificationEvent(ModificationEvent ev) {
    5.10          for (ModificationListener l : listener) {
    5.11              l.modification(ev);
    5.12          }
    5.13      }
    5.14 +    // END: openfixed.trivial
    5.15  
    5.16      @Override
    5.17      public void add(ModificationListener l) {
     6.1 --- a/samples/openfixed/test/org/apidesign/openfixed/CalculatorBase.java	Sun Mar 20 18:52:47 2011 +0100
     6.2 +++ b/samples/openfixed/test/org/apidesign/openfixed/CalculatorBase.java	Sun Mar 20 20:52:33 2011 +0100
     6.3 @@ -12,6 +12,7 @@
     6.4      
     6.5      protected abstract Calculator create();
     6.6  
     6.7 +    // BEGIN: openfixed.commontest
     6.8      public void testSumAndListeners() throws Exception {
     6.9          Calculator a = create();
    6.10          MockListener l = new MockListener();
    6.11 @@ -47,8 +48,9 @@
    6.12              events.add(ev);
    6.13          }
    6.14          
    6.15 -        public synchronized List<ModificationEvent> assertEvents(String msg, int cnt) 
    6.16 -        throws InterruptedException {
    6.17 +        public synchronized List<ModificationEvent> assertEvents(
    6.18 +            String msg, int cnt
    6.19 +        ) throws InterruptedException {
    6.20              for (int i = 0; i < 10; i++) {
    6.21                  if (events != null && events.size() >= cnt) {
    6.22                      break;
    6.23 @@ -61,4 +63,5 @@
    6.24              return res;
    6.25          }
    6.26      } // end of ModificationListener
    6.27 +    // END: openfixed.commontest
    6.28  }
     7.1 --- a/samples/openfixed/test/org/apidesign/openfixed/PostTest.java	Sun Mar 20 18:52:47 2011 +0100
     7.2 +++ b/samples/openfixed/test/org/apidesign/openfixed/PostTest.java	Sun Mar 20 20:52:33 2011 +0100
     7.3 @@ -18,7 +18,9 @@
     7.4      }
     7.5  
     7.6      public void testPostModificationEvents() throws Exception {
     7.7 -        class PostListener extends BlockingListener implements PostModificationListener {
     7.8 +        // BEGIN: openfixed.usemount
     7.9 +        class PostListener extends BlockingListener 
    7.10 +        implements PostModificationListener {
    7.11              int cnt;
    7.12  
    7.13              @Override
    7.14 @@ -30,8 +32,10 @@
    7.15  
    7.16              @Override
    7.17              public synchronized void postProcess(PostModificationEvent ev) {
    7.18 +                // called when batch processing is over
    7.19                  cnt++;
    7.20              }
    7.21 +        // FINISH: openfixed.usemount
    7.22              
    7.23              public synchronized void assertPostProcess(String msg, int expected) throws InterruptedException {
    7.24                  for (int i = 0; i < 10; i++) {