1.1 --- a/samples/aserverinfo/test/org/apidesign/aserverinfo/AServerInfoTest.java Sat Jun 14 09:54:38 2008 +0200
1.2 +++ b/samples/aserverinfo/test/org/apidesign/aserverinfo/AServerInfoTest.java Sat Jun 14 09:54:39 2008 +0200
1.3 @@ -23,28 +23,6 @@
1.4
1.5 @Test
1.6 public void showUseOfCumulativeFactory() throws Exception {
1.7 - class Prov implements AServerInfo.NameProvider, AServerInfo.URLProvider, AServerInfo.ResetHandler {
1.8 - int resets;
1.9 -
1.10 - public String getName() {
1.11 - return "API Design Server";
1.12 - }
1.13 -
1.14 - public URL getURL() {
1.15 - try {
1.16 - return new URL("http://www.apidesign.org");
1.17 - } catch (MalformedURLException ex) {
1.18 - Exceptions.printStackTrace(ex);
1.19 - return null;
1.20 - }
1.21 - }
1.22 -
1.23 - public void reset() {
1.24 - resets++;
1.25 - }
1.26 -
1.27 - }
1.28 -
1.29 Prov prov = new Prov();
1.30 AServerInfo info;
1.31
1.32 @@ -58,4 +36,47 @@
1.33 assertEquals("Once reset", 1, prov.resets);
1.34
1.35 }
1.36 +
1.37 + @Test
1.38 + public void showVerboseUseOfCumulativeFactory() throws Exception {
1.39 + Prov prov = new Prov();
1.40 + AServerInfo info;
1.41 +
1.42 + // BEGIN: aserverinfo.cumulative.creation.verbose
1.43 + AServerInfo empty = AServerInfo.empty();
1.44 + AServerInfo name = empty.nameProvider(prov);
1.45 + AServerInfo urlAndName = name.urlProvider(prov);
1.46 + info = urlAndName.reset(prov);
1.47 + // END: aserverinfo.cumulative.creation.verbose
1.48 +
1.49 + assertEquals("API Design Server", info.getName());
1.50 + assertEquals("http://www.apidesign.org", info.getURL().toExternalForm());
1.51 + info.reset();
1.52 + assertEquals("Once reset", 1, prov.resets);
1.53 +
1.54 + }
1.55 +
1.56 +
1.57 + private static class Prov implements AServerInfo.NameProvider, AServerInfo.URLProvider, AServerInfo.ResetHandler {
1.58 + int resets;
1.59 +
1.60 + public String getName() {
1.61 + return "API Design Server";
1.62 + }
1.63 +
1.64 + public URL getURL() {
1.65 + try {
1.66 + return new URL("http://www.apidesign.org");
1.67 + } catch (MalformedURLException ex) {
1.68 + Exceptions.printStackTrace(ex);
1.69 + return null;
1.70 + }
1.71 + }
1.72 +
1.73 + public void reset() {
1.74 + resets++;
1.75 + }
1.76 +
1.77 + }
1.78 +
1.79 }
1.80 \ No newline at end of file
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/samples/reentrant/src/org/apidesign/reentrant/CriticalSectionReentrantImmutable.java Sat Jun 14 09:54:39 2008 +0200
2.3 @@ -0,0 +1,66 @@
2.4 +package org.apidesign.reentrant;
2.5 +
2.6 +import java.util.Collection;
2.7 +import java.util.concurrent.atomic.AtomicInteger;
2.8 +
2.9 +public class CriticalSectionReentrantImmutable<T extends Comparable<T>> implements CriticalSection<T> {
2.10 + private ImmutableData<T> data = new ImmutableData<T>();
2.11 +
2.12 + public void assignPilot(T pilot) {
2.13 + data = data.newPilot(pilot);
2.14 + }
2.15 +
2.16 + // BEGIN: reentrant.merge.immutable
2.17 + public int sumBigger(Collection<T> args) {
2.18 + for (;;) {
2.19 + ImmutableData<T> previous = this.data;
2.20 + int[] ret = { 0 };
2.21 + ImmutableData<T> now = doCriticalSection(args, previous, ret);
2.22 +
2.23 + synchronized (this) {
2.24 + // if there was no parallel or reentrant change,
2.25 + // apply and return. Otherwise try once more.
2.26 + if (previous == this.data) {
2.27 + this.data = now;
2.28 + return ret[0];
2.29 + }
2.30 + }
2.31 + }
2.32 + }
2.33 + // END: reentrant.merge.immutable
2.34 +
2.35 + private ImmutableData<T> doCriticalSection(Collection<T> args, ImmutableData<T> data, int[] result) {
2.36 + result[0] = 0;
2.37 + for (T cmp : args) {
2.38 + if (data.pilot.compareTo(cmp) < 0) {
2.39 + result[0]++;
2.40 + }
2.41 + }
2.42 + return data.newCnt(data.cnt + result[0]);
2.43 + }
2.44 +
2.45 + public int getCount() {
2.46 + return data.cnt;
2.47 + }
2.48 +
2.49 + private static final class ImmutableData<T> {
2.50 + final T pilot;
2.51 + final int cnt;
2.52 +
2.53 + public ImmutableData() {
2.54 + pilot = null;
2.55 + cnt = 0;
2.56 + }
2.57 + private ImmutableData(T pilot, int cnt) {
2.58 + this.pilot = pilot;
2.59 + this.cnt = cnt;
2.60 + }
2.61 +
2.62 + public ImmutableData<T> newPilot(T pilot) {
2.63 + return new ImmutableData(pilot, this.cnt);
2.64 + }
2.65 + public ImmutableData<T> newCnt(int cnt) {
2.66 + return new ImmutableData(this.pilot, cnt);
2.67 + }
2.68 + }
2.69 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/samples/reentrant/test/org/apidesign/reentrant/CriticalSectionReentrantImmutableTest.java Sat Jun 14 09:54:39 2008 +0200
3.3 @@ -0,0 +1,13 @@
3.4 +package org.apidesign.reentrant;
3.5 +
3.6 +public class CriticalSectionReentrantImmutableTest extends CriticalSectionBase {
3.7 + @Override
3.8 + protected boolean reentrantJustOnce() {
3.9 + return true;
3.10 + }
3.11 +
3.12 + @Override
3.13 + protected CriticalSection<Integer> create() {
3.14 + return new CriticalSectionReentrantImmutable<Integer>();
3.15 + }
3.16 +}
3.17 \ No newline at end of file