# HG changeset patch # User Jaroslav Tulach # Date 1213430037 -7200 # Node ID f6e64df4f96238b8b8cd996dab5971a3523af865 # Parent 3280450405a0171b05f5735b3b54ed988b0d9c4c Way simplified internals of the AServerInfo class diff -r 3280450405a0 -r f6e64df4f962 samples/aserverinfo/src/org/apidesign/aserverinfo/AServerInfo.java --- a/samples/aserverinfo/src/org/apidesign/aserverinfo/AServerInfo.java Sat Jun 14 09:53:57 2008 +0200 +++ b/samples/aserverinfo/src/org/apidesign/aserverinfo/AServerInfo.java Sat Jun 14 09:53:57 2008 +0200 @@ -1,43 +1,43 @@ package org.apidesign.aserverinfo; import java.net.URL; -import java.util.concurrent.Callable; import org.openide.util.Lookup; public final class AServerInfo { - private final Callable name; - private final Callable url; - private final Callable reset; - private final Callable shutdown; + private final NameProvider name; + private final URLProvider url; + private final ResetHandler reset; + private final ShutdownHandler shutdown; - private AServerInfo(Callable name, Callable url, Callable reset, Callable s) { + private AServerInfo(NameProvider name, URLProvider url, ResetHandler reset, ShutdownHandler shutdown) { this.name = name; this.url = url; this.reset = reset; - this.shutdown = s; + this.shutdown = shutdown; } + // // API for clients // public String getName() { - return call(name, "noname"); + return name == null ? "noname" : name.getName(); } public URL getURL() { - return call(url, null); + return url == null ? null : url.getURL(); } - public void reset() throws Exception { + public void reset() { if (reset != null) { - reset.call(); + reset.reset(); } } - public void shutdown() throws Exception { + public void shutdown() { if (shutdown != null) { - shutdown.call(); + shutdown.shutdown(); } } @@ -59,25 +59,24 @@ public static AServerInfo create(final Lookup interfaces) // END: aserverinfo.create { - Callable nameP = new Callable() { - public String call() throws Exception { + NameProvider nameP = new NameProvider() { + public String getName() { NameProvider p = interfaces.lookup(NameProvider.class); return p == null ? "noname" : p.getName(); } }; - Callable urlP = new Callable() { - public URL call() throws Exception { + URLProvider urlP = new URLProvider() { + public URL getURL() { URLProvider p = interfaces.lookup(URLProvider.class); return p == null ? null : p.getURL(); } }; - Callable resetP = new Callable() { - public Void call() throws Exception { + ResetHandler resetP = new ResetHandler() { + public void reset() { ResetHandler h = interfaces.lookup(ResetHandler.class); if (h != null) { h.reset(); } - return null; } }; @@ -88,7 +87,7 @@ public static AServerInfo create(NameProvider nameProvider, URLProvider urlProvider, ResetHandler reset) // END: aserverinfo.regularcreate { - return create(nameProvider, urlProvider, reset, null); + return new AServerInfo(nameProvider, urlProvider, reset, null); } // BEGIN: aserverinfo.regularcreate.withshutdown @@ -101,39 +100,7 @@ public static AServerInfo create(NameProvider nameProvider, URLProvider urlProvider, ResetHandler reset, ShutdownHandler shutdown) // END: aserverinfo.regularcreate.withshutdown { - final NameProvider np = nameProvider; - final URLProvider up = urlProvider; - final ResetHandler h = reset; - final ShutdownHandler s = shutdown; - - Callable nameP = new Callable() { - public String call() throws Exception { - return np == null ? "noname" : np.getName(); - } - }; - Callable urlP = new Callable() { - public URL call() throws Exception { - return up == null ? null : up.getURL(); - } - }; - Callable resetP = new Callable() { - public Void call() throws Exception { - if (h != null) { - h.reset(); - } - return null; - } - }; - Callable shutP = new Callable() { - public Void call() throws Exception { - if (s != null) { - s.shutdown(); - } - return null; - } - }; - - return new AServerInfo(nameP, urlP, resetP, shutP); + return new AServerInfo(nameProvider, urlProvider, reset, shutdown); } // @@ -146,57 +113,17 @@ } 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); + return new AServerInfo(np, 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); + return new AServerInfo(this.name, up, 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); + return new AServerInfo(this.name, this.url, h, 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); + return new AServerInfo(this.name, this.url, this.reset, s); } - - // - // Support implementations - // - - private static T call(Callable name, T defValue) { - try { - return name.call(); - } catch (Exception ex) { - return defValue; - } - } - - }