# HG changeset patch # User Jaroslav Tulach # Date 1213430037 -7200 # Node ID 3280450405a0171b05f5735b3b54ed988b0d9c4c # Parent 30a0c70c6579d3a865f4c54c1cdd8fb01b5481f4 review2: Note about Tune and DVB Central. Finished example with AServerInfo diff -r 30a0c70c6579 -r 3280450405a0 samples/aserverinfo/src/org/apidesign/aserverinfo/AServerInfo.java --- a/samples/aserverinfo/src/org/apidesign/aserverinfo/AServerInfo.java Sat Jun 14 09:53:51 2008 +0200 +++ b/samples/aserverinfo/src/org/apidesign/aserverinfo/AServerInfo.java Sat Jun 14 09:53:57 2008 +0200 @@ -2,7 +2,6 @@ import java.net.URL; import java.util.concurrent.Callable; -import org.openide.util.Exceptions; import org.openide.util.Lookup; public final class AServerInfo { @@ -11,13 +10,17 @@ private final Callable reset; private final Callable shutdown; - public AServerInfo(Callable name, Callable url, Callable reset, Callable s) { + private AServerInfo(Callable name, Callable url, Callable reset, Callable s) { this.name = name; this.url = url; this.reset = reset; this.shutdown = s; } + // + // API for clients + // + public String getName() { return call(name, "noname"); } @@ -27,23 +30,21 @@ } public void reset() throws Exception { - reset.call(); + if (reset != null) { + reset.call(); + } } public void shutdown() throws Exception { - shutdown.call(); - } - - - - private static T call(Callable name, T defValue) { - try { - return name.call(); - } catch (Exception ex) { - return defValue; + if (shutdown != null) { + shutdown.call(); } } + // + // factories + // + // BEGIN: aserverinfo.create public interface NameProvider { public String getName(); @@ -134,7 +135,68 @@ return new AServerInfo(nameP, urlP, resetP, shutP); } + + // + // cumulative factory methods + // + // BEGIN: aserverinfo.cumulative.factory + public static AServerInfo empty() { + return new AServerInfo(null, null, null, null); + } + public final AServerInfo nameProvider(final NameProvider np) { + Callable nameP = new Callable() { + public String call() throws Exception { + return np == null ? "noname" : np.getName(); + } + }; + return new AServerInfo(nameP, this.url, this.reset, this.shutdown); + } + // END: aserverinfo.cumulative.empty + + public final AServerInfo urlProvider(final URLProvider up) { + Callable urlP = new Callable() { + public URL call() throws Exception { + return up == null ? null : up.getURL(); + } + }; + return new AServerInfo(this.name, urlP, this.reset, this.shutdown); + } + public final AServerInfo reset(final ResetHandler h) { + Callable resetP = new Callable() { + public Void call() throws Exception { + if (h != null) { + h.reset(); + } + return null; + } + }; + return new AServerInfo(this.name, this.url, resetP, this.shutdown); + } + public final AServerInfo shutdown(final ShutdownHandler s) { + Callable shutP = new Callable() { + public Void call() throws Exception { + if (s != null) { + s.shutdown(); + } + return null; + } + }; + return new AServerInfo(this.name, this.url, this.reset, shutP); + } + + // + // Support implementations + // + + private static T call(Callable name, T defValue) { + try { + return name.call(); + } catch (Exception ex) { + return defValue; + } + } + } diff -r 30a0c70c6579 -r 3280450405a0 samples/aserverinfo/test/org/apidesign/aserverinfo/AServerInfoTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/aserverinfo/test/org/apidesign/aserverinfo/AServerInfoTest.java Sat Jun 14 09:53:57 2008 +0200 @@ -0,0 +1,61 @@ +package org.apidesign.aserverinfo; + +import java.net.MalformedURLException; +import java.net.URL; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; +import org.openide.util.Exceptions; + +public class AServerInfoTest { + + public AServerInfoTest() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + @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; + + // BEGIN: aserverinfo.cumulative.creation + info = AServerInfo.empty().nameProvider(prov).urlProvider(prov).reset(prov); + // END: aserverinfo.cumulative.creation + + assertEquals("API Design Server", info.getName()); + assertEquals("http://www.apidesign.org", info.getURL().toExternalForm()); + info.reset(); + assertEquals("Once reset", 1, prov.resets); + + } +} \ No newline at end of file