# HG changeset patch # User Jaroslav Tulach # Date 1213430079 -7200 # Node ID c479228609ea5957cdb07e287d94d753c0a50dee # Parent f2b6057a33765bf0d86f3d36b3b28d2770f60886 Part II: Practical Design is now ready for review by our reviewers diff -r f2b6057a3376 -r c479228609ea samples/aserverinfo/test/org/apidesign/aserverinfo/AServerInfoTest.java --- a/samples/aserverinfo/test/org/apidesign/aserverinfo/AServerInfoTest.java Sat Jun 14 09:54:38 2008 +0200 +++ b/samples/aserverinfo/test/org/apidesign/aserverinfo/AServerInfoTest.java Sat Jun 14 09:54:39 2008 +0200 @@ -23,28 +23,6 @@ @Test public void showUseOfCumulativeFactory() throws Exception { - class Prov implements AServerInfo.NameProvider, AServerInfo.URLProvider, AServerInfo.ResetHandler { - int resets; - - public String getName() { - return "API Design Server"; - } - - public URL getURL() { - try { - return new URL("http://www.apidesign.org"); - } catch (MalformedURLException ex) { - Exceptions.printStackTrace(ex); - return null; - } - } - - public void reset() { - resets++; - } - - } - Prov prov = new Prov(); AServerInfo info; @@ -58,4 +36,47 @@ assertEquals("Once reset", 1, prov.resets); } + + @Test + public void showVerboseUseOfCumulativeFactory() throws Exception { + Prov prov = new Prov(); + AServerInfo info; + + // BEGIN: aserverinfo.cumulative.creation.verbose + AServerInfo empty = AServerInfo.empty(); + AServerInfo name = empty.nameProvider(prov); + AServerInfo urlAndName = name.urlProvider(prov); + info = urlAndName.reset(prov); + // END: aserverinfo.cumulative.creation.verbose + + assertEquals("API Design Server", info.getName()); + assertEquals("http://www.apidesign.org", info.getURL().toExternalForm()); + info.reset(); + assertEquals("Once reset", 1, prov.resets); + + } + + + private static class Prov implements AServerInfo.NameProvider, AServerInfo.URLProvider, AServerInfo.ResetHandler { + int resets; + + public String getName() { + return "API Design Server"; + } + + public URL getURL() { + try { + return new URL("http://www.apidesign.org"); + } catch (MalformedURLException ex) { + Exceptions.printStackTrace(ex); + return null; + } + } + + public void reset() { + resets++; + } + + } + } \ No newline at end of file diff -r f2b6057a3376 -r c479228609ea samples/reentrant/src/org/apidesign/reentrant/CriticalSectionReentrantImmutable.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/reentrant/src/org/apidesign/reentrant/CriticalSectionReentrantImmutable.java Sat Jun 14 09:54:39 2008 +0200 @@ -0,0 +1,66 @@ +package org.apidesign.reentrant; + +import java.util.Collection; +import java.util.concurrent.atomic.AtomicInteger; + +public class CriticalSectionReentrantImmutable> implements CriticalSection { + private ImmutableData data = new ImmutableData(); + + public void assignPilot(T pilot) { + data = data.newPilot(pilot); + } + + // BEGIN: reentrant.merge.immutable + public int sumBigger(Collection args) { + for (;;) { + ImmutableData previous = this.data; + int[] ret = { 0 }; + ImmutableData now = doCriticalSection(args, previous, ret); + + synchronized (this) { + // if there was no parallel or reentrant change, + // apply and return. Otherwise try once more. + if (previous == this.data) { + this.data = now; + return ret[0]; + } + } + } + } + // END: reentrant.merge.immutable + + private ImmutableData doCriticalSection(Collection args, ImmutableData data, int[] result) { + result[0] = 0; + for (T cmp : args) { + if (data.pilot.compareTo(cmp) < 0) { + result[0]++; + } + } + return data.newCnt(data.cnt + result[0]); + } + + public int getCount() { + return data.cnt; + } + + private static final class ImmutableData { + final T pilot; + final int cnt; + + public ImmutableData() { + pilot = null; + cnt = 0; + } + private ImmutableData(T pilot, int cnt) { + this.pilot = pilot; + this.cnt = cnt; + } + + public ImmutableData newPilot(T pilot) { + return new ImmutableData(pilot, this.cnt); + } + public ImmutableData newCnt(int cnt) { + return new ImmutableData(this.pilot, cnt); + } + } +} diff -r f2b6057a3376 -r c479228609ea samples/reentrant/test/org/apidesign/reentrant/CriticalSectionReentrantImmutableTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/reentrant/test/org/apidesign/reentrant/CriticalSectionReentrantImmutableTest.java Sat Jun 14 09:54:39 2008 +0200 @@ -0,0 +1,13 @@ +package org.apidesign.reentrant; + +public class CriticalSectionReentrantImmutableTest extends CriticalSectionBase { + @Override + protected boolean reentrantJustOnce() { + return true; + } + + @Override + protected CriticalSection create() { + return new CriticalSectionReentrantImmutable(); + } +} \ No newline at end of file