review2: Note about Tune and DVB Central. Finished example with AServerInfo
1.1 --- a/samples/aserverinfo/src/org/apidesign/aserverinfo/AServerInfo.java Sat Jun 14 09:53:51 2008 +0200
1.2 +++ b/samples/aserverinfo/src/org/apidesign/aserverinfo/AServerInfo.java Sat Jun 14 09:53:57 2008 +0200
1.3 @@ -2,7 +2,6 @@
1.4
1.5 import java.net.URL;
1.6 import java.util.concurrent.Callable;
1.7 -import org.openide.util.Exceptions;
1.8 import org.openide.util.Lookup;
1.9
1.10 public final class AServerInfo {
1.11 @@ -11,13 +10,17 @@
1.12 private final Callable<Void> reset;
1.13 private final Callable<Void> shutdown;
1.14
1.15 - public AServerInfo(Callable<String> name, Callable<URL> url, Callable<Void> reset, Callable<Void> s) {
1.16 + private AServerInfo(Callable<String> name, Callable<URL> url, Callable<Void> reset, Callable<Void> s) {
1.17 this.name = name;
1.18 this.url = url;
1.19 this.reset = reset;
1.20 this.shutdown = s;
1.21 }
1.22
1.23 + //
1.24 + // API for clients
1.25 + //
1.26 +
1.27 public String getName() {
1.28 return call(name, "noname");
1.29 }
1.30 @@ -27,23 +30,21 @@
1.31 }
1.32
1.33 public void reset() throws Exception {
1.34 - reset.call();
1.35 + if (reset != null) {
1.36 + reset.call();
1.37 + }
1.38 }
1.39
1.40 public void shutdown() throws Exception {
1.41 - shutdown.call();
1.42 - }
1.43 -
1.44 -
1.45 -
1.46 - private static <T> T call(Callable<T> name, T defValue) {
1.47 - try {
1.48 - return name.call();
1.49 - } catch (Exception ex) {
1.50 - return defValue;
1.51 + if (shutdown != null) {
1.52 + shutdown.call();
1.53 }
1.54 }
1.55
1.56 + //
1.57 + // factories
1.58 + //
1.59 +
1.60 // BEGIN: aserverinfo.create
1.61 public interface NameProvider {
1.62 public String getName();
1.63 @@ -134,7 +135,68 @@
1.64
1.65 return new AServerInfo(nameP, urlP, resetP, shutP);
1.66 }
1.67 +
1.68 + //
1.69 + // cumulative factory methods
1.70 + //
1.71
1.72 + // BEGIN: aserverinfo.cumulative.factory
1.73 + public static AServerInfo empty() {
1.74 + return new AServerInfo(null, null, null, null);
1.75 + }
1.76
1.77 + public final AServerInfo nameProvider(final NameProvider np) {
1.78 + Callable<String> nameP = new Callable<String>() {
1.79 + public String call() throws Exception {
1.80 + return np == null ? "noname" : np.getName();
1.81 + }
1.82 + };
1.83 + return new AServerInfo(nameP, this.url, this.reset, this.shutdown);
1.84 + }
1.85 + // END: aserverinfo.cumulative.empty
1.86 +
1.87 + public final AServerInfo urlProvider(final URLProvider up) {
1.88 + Callable<URL> urlP = new Callable<URL>() {
1.89 + public URL call() throws Exception {
1.90 + return up == null ? null : up.getURL();
1.91 + }
1.92 + };
1.93 + return new AServerInfo(this.name, urlP, this.reset, this.shutdown);
1.94 + }
1.95 + public final AServerInfo reset(final ResetHandler h) {
1.96 + Callable<Void> resetP = new Callable<Void>() {
1.97 + public Void call() throws Exception {
1.98 + if (h != null) {
1.99 + h.reset();
1.100 + }
1.101 + return null;
1.102 + }
1.103 + };
1.104 + return new AServerInfo(this.name, this.url, resetP, this.shutdown);
1.105 + }
1.106 + public final AServerInfo shutdown(final ShutdownHandler s) {
1.107 + Callable<Void> shutP = new Callable<Void>() {
1.108 + public Void call() throws Exception {
1.109 + if (s != null) {
1.110 + s.shutdown();
1.111 + }
1.112 + return null;
1.113 + }
1.114 + };
1.115 + return new AServerInfo(this.name, this.url, this.reset, shutP);
1.116 + }
1.117 +
1.118 + //
1.119 + // Support implementations
1.120 + //
1.121 +
1.122 + private static <T> T call(Callable<T> name, T defValue) {
1.123 + try {
1.124 + return name.call();
1.125 + } catch (Exception ex) {
1.126 + return defValue;
1.127 + }
1.128 + }
1.129 +
1.130
1.131 }
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/samples/aserverinfo/test/org/apidesign/aserverinfo/AServerInfoTest.java Sat Jun 14 09:53:57 2008 +0200
2.3 @@ -0,0 +1,61 @@
2.4 +package org.apidesign.aserverinfo;
2.5 +
2.6 +import java.net.MalformedURLException;
2.7 +import java.net.URL;
2.8 +import org.junit.After;
2.9 +import org.junit.Before;
2.10 +import org.junit.Test;
2.11 +import static org.junit.Assert.*;
2.12 +import org.openide.util.Exceptions;
2.13 +
2.14 +public class AServerInfoTest {
2.15 +
2.16 + public AServerInfoTest() {
2.17 + }
2.18 +
2.19 + @Before
2.20 + public void setUp() {
2.21 + }
2.22 +
2.23 + @After
2.24 + public void tearDown() {
2.25 + }
2.26 +
2.27 + @Test
2.28 + public void showUseOfCumulativeFactory() throws Exception {
2.29 + class Prov implements AServerInfo.NameProvider, AServerInfo.URLProvider, AServerInfo.ResetHandler {
2.30 + int resets;
2.31 +
2.32 + public String getName() {
2.33 + return "API Design Server";
2.34 + }
2.35 +
2.36 + public URL getURL() {
2.37 + try {
2.38 + return new URL("http://www.apidesign.org");
2.39 + } catch (MalformedURLException ex) {
2.40 + Exceptions.printStackTrace(ex);
2.41 + return null;
2.42 + }
2.43 + }
2.44 +
2.45 + public void reset() {
2.46 + resets++;
2.47 + }
2.48 +
2.49 + }
2.50 +
2.51 + Prov prov = new Prov();
2.52 + AServerInfo info;
2.53 +
2.54 + // BEGIN: aserverinfo.cumulative.creation
2.55 + info = AServerInfo.empty().nameProvider(prov).urlProvider(prov).reset(prov);
2.56 + // END: aserverinfo.cumulative.creation
2.57 +
2.58 + assertEquals("API Design Server", info.getName());
2.59 + assertEquals("http://www.apidesign.org", info.getURL().toExternalForm());
2.60 + info.reset();
2.61 + assertEquals("Once reset", 1, prov.resets);
2.62 +
2.63 + }
2.64 +}
2.65 \ No newline at end of file