# HG changeset patch # User Jaroslav Tulach # Date 1300650753 -3600 # Node ID 3abae898011d52e499f6ab21571209dc9c44a86a # Parent 35da2d439e3d90c890384b0c0126281f97775f32 More code snippets for openfixed project diff -r 35da2d439e3d -r 3abae898011d samples/openfixed/src/org/apidesign/openfixed/AsyncEventSupport.java --- a/samples/openfixed/src/org/apidesign/openfixed/AsyncEventSupport.java Sun Mar 20 18:52:47 2011 +0100 +++ b/samples/openfixed/src/org/apidesign/openfixed/AsyncEventSupport.java Sun Mar 20 20:52:33 2011 +0100 @@ -11,16 +11,9 @@ */ final class AsyncEventSupport implements EventSupport { private final List listeners = new CopyOnWriteArrayList(); - private static final Executor EXEC = Executors.newSingleThreadExecutor(); AsyncEventSupport() { } - - @Override - public void fireModificationEvent(ModificationEvent ev) { - EXEC.execute(new Deliverable(ev, listeners.toArray(new ModificationListener[0]))); - } - @Override public void add(ModificationListener l) { listeners.add(l); @@ -30,12 +23,23 @@ public void remove(ModificationListener l) { listeners.remove(l); } + + // BEGIN: openfixed.asynch + private static final Executor EXEC = Executors.newSingleThreadExecutor(); + @Override + public void fireModificationEvent(ModificationEvent ev) { + EXEC.execute(new Deliverable( + ev, listeners.toArray(new ModificationListener[0]) + )); + } private static class Deliverable implements Runnable { final ModificationEvent ev; final ModificationListener[] listeners; - public Deliverable(ModificationEvent ev, ModificationListener[] listeners) { + public Deliverable( + ModificationEvent ev, ModificationListener[] listeners + ) { this.ev = ev; this.listeners = listeners; } @@ -47,4 +51,5 @@ } } } + // END: openfixed.asynch } diff -r 35da2d439e3d -r 3abae898011d samples/openfixed/src/org/apidesign/openfixed/Calculator.java --- a/samples/openfixed/src/org/apidesign/openfixed/Calculator.java Sun Mar 20 18:52:47 2011 +0100 +++ b/samples/openfixed/src/org/apidesign/openfixed/Calculator.java Sun Mar 20 20:52:33 2011 +0100 @@ -5,6 +5,7 @@ * * @author Jaroslav Tulach */ +// BEGIN: openfixed.bean public final class Calculator { private final EventSupport listeners; private int sum; @@ -47,3 +48,4 @@ listeners.remove(l); } } +// END: openfixed.bean diff -r 35da2d439e3d -r 3abae898011d samples/openfixed/src/org/apidesign/openfixed/PendingEventSupport.java --- a/samples/openfixed/src/org/apidesign/openfixed/PendingEventSupport.java Sun Mar 20 18:52:47 2011 +0100 +++ b/samples/openfixed/src/org/apidesign/openfixed/PendingEventSupport.java Sun Mar 20 20:52:33 2011 +0100 @@ -46,6 +46,7 @@ pending = deliverables.toArray(new Deliverable[0]); deliverables.clear(); } + // BEGIN: openfixed.pendingCount int pendingCount = pending.length; for (Deliverable d : pending) { d.ev.pending = --pendingCount; @@ -53,6 +54,7 @@ l.modification(d.ev); } } + // END: openfixed.pendingCount } private static class Deliverable { diff -r 35da2d439e3d -r 3abae898011d samples/openfixed/src/org/apidesign/openfixed/PostEventSupport.java --- a/samples/openfixed/src/org/apidesign/openfixed/PostEventSupport.java Sun Mar 20 18:52:47 2011 +0100 +++ b/samples/openfixed/src/org/apidesign/openfixed/PostEventSupport.java Sun Mar 20 20:52:33 2011 +0100 @@ -48,8 +48,10 @@ pending = deliverables.toArray(new Deliverable[0]); deliverables.clear(); } + // BEGIN: openfixed.postimpl Calculator calc = null; - Set notify = new HashSet(); + Set notify; + notify = new HashSet(); int pendingCount = pending.length; for (Deliverable d : pending) { calc = (Calculator)d.ev.getSource(); @@ -60,10 +62,10 @@ } d.ev.posts = null; } - for (PostModificationListener pml : notify) { pml.postProcess(new PostModificationEvent(calc)); } + // END: openfixed.postimpl } private static class Deliverable { diff -r 35da2d439e3d -r 3abae898011d samples/openfixed/src/org/apidesign/openfixed/TrivialEventSupport.java --- a/samples/openfixed/src/org/apidesign/openfixed/TrivialEventSupport.java Sun Mar 20 18:52:47 2011 +0100 +++ b/samples/openfixed/src/org/apidesign/openfixed/TrivialEventSupport.java Sun Mar 20 20:52:33 2011 +0100 @@ -17,12 +17,14 @@ TrivialEventSupport() { } + // BEGIN: openfixed.trivial @Override public void fireModificationEvent(ModificationEvent ev) { for (ModificationListener l : listener) { l.modification(ev); } } + // END: openfixed.trivial @Override public void add(ModificationListener l) { diff -r 35da2d439e3d -r 3abae898011d samples/openfixed/test/org/apidesign/openfixed/CalculatorBase.java --- a/samples/openfixed/test/org/apidesign/openfixed/CalculatorBase.java Sun Mar 20 18:52:47 2011 +0100 +++ b/samples/openfixed/test/org/apidesign/openfixed/CalculatorBase.java Sun Mar 20 20:52:33 2011 +0100 @@ -12,6 +12,7 @@ protected abstract Calculator create(); + // BEGIN: openfixed.commontest public void testSumAndListeners() throws Exception { Calculator a = create(); MockListener l = new MockListener(); @@ -47,8 +48,9 @@ events.add(ev); } - public synchronized List assertEvents(String msg, int cnt) - throws InterruptedException { + public synchronized List assertEvents( + String msg, int cnt + ) throws InterruptedException { for (int i = 0; i < 10; i++) { if (events != null && events.size() >= cnt) { break; @@ -61,4 +63,5 @@ return res; } } // end of ModificationListener + // END: openfixed.commontest } diff -r 35da2d439e3d -r 3abae898011d samples/openfixed/test/org/apidesign/openfixed/PostTest.java --- a/samples/openfixed/test/org/apidesign/openfixed/PostTest.java Sun Mar 20 18:52:47 2011 +0100 +++ b/samples/openfixed/test/org/apidesign/openfixed/PostTest.java Sun Mar 20 20:52:33 2011 +0100 @@ -18,7 +18,9 @@ } public void testPostModificationEvents() throws Exception { - class PostListener extends BlockingListener implements PostModificationListener { + // BEGIN: openfixed.usemount + class PostListener extends BlockingListener + implements PostModificationListener { int cnt; @Override @@ -30,8 +32,10 @@ @Override public synchronized void postProcess(PostModificationEvent ev) { + // called when batch processing is over cnt++; } + // FINISH: openfixed.usemount public synchronized void assertPostProcess(String msg, int expected) throws InterruptedException { for (int i = 0; i < 10; i++) {