samples/aserverinfo/src/org/apidesign/aserverinfo/cummulativefactory/ServerConnector.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/aserverinfo/src/org/apidesign/aserverinfo/cummulativefactory/ServerConnector.java Sat Nov 15 06:53:43 2008 +0100
1.3 @@ -0,0 +1,88 @@
1.4 +package org.apidesign.aserverinfo.cummulativefactory;
1.5 +
1.6 +import org.apidesign.aserverinfo.spi.NameProvider;
1.7 +import org.apidesign.aserverinfo.spi.ResetHandler;
1.8 +import org.apidesign.aserverinfo.spi.URLProvider;
1.9 +import java.net.URL;
1.10 +import org.apidesign.aserverinfo.spi.ShutdownHandler;
1.11 +
1.12 +// BEGIN: aserverinfo.api
1.13 +/** A class to connect to server, identify it and manipulate with
1.14 + * it state. The <a href="http://apidesign.org">Practical API Design</a>
1.15 + * book used to call it AServerInfo.
1.16 + * <p>
1.17 + */
1.18 +public final class ServerConnector {
1.19 + public String getName() {
1.20 + return name == null ? "noname" : name.getName();
1.21 + }
1.22 +
1.23 + public URL getURL() {
1.24 + return url == null ? null : url.getURL();
1.25 + }
1.26 +
1.27 + public void reset() {
1.28 + if (reset != null) {
1.29 + reset.reset();
1.30 + }
1.31 + }
1.32 +
1.33 + /** Additional method for API clients not available from first version.
1.34 + * @since 2.0
1.35 + */
1.36 + public void shutdown() {
1.37 + if (shutdown != null) {
1.38 + shutdown.shutdown();
1.39 + }
1.40 + }
1.41 +// FINISH: aserverinfo.api
1.42 +
1.43 + //
1.44 + // cumulative factory methods
1.45 + //
1.46 +
1.47 + // BEGIN: aserverinfo.cumulative.factory
1.48 + public static ServerConnector empty() {
1.49 + return new ServerConnector(null, null, null, null);
1.50 + }
1.51 +
1.52 + public final ServerConnector nameProvider(NameProvider np) {
1.53 + return new ServerConnector(np, this.url, this.reset, this.shutdown);
1.54 + }
1.55 +
1.56 + public final ServerConnector urlProvider(URLProvider up) {
1.57 + return new ServerConnector(this.name, up, this.reset, this.shutdown);
1.58 + }
1.59 + public final ServerConnector reset(ResetHandler h) {
1.60 + return new ServerConnector(this.name, this.url, h, this.shutdown);
1.61 + }
1.62 + /** All one needs to do when there is a need to add new
1.63 + * style of creation is to add new <em>clonning</em> method.
1.64 + * @param handler
1.65 + * @return
1.66 + * @since 2.0
1.67 + */
1.68 + public final ServerConnector shutdown(ShutdownHandler handler) {
1.69 + return new ServerConnector(this.name, this.url, this.reset, handler);
1.70 + }
1.71 + // END: aserverinfo.cumulative.factory
1.72 +
1.73 + //
1.74 + // private part
1.75 + //
1.76 +
1.77 + private final NameProvider name;
1.78 + private final URLProvider url;
1.79 + private final ResetHandler reset;
1.80 + private final ShutdownHandler shutdown;
1.81 +
1.82 + private ServerConnector(
1.83 + NameProvider name, URLProvider url,
1.84 + ResetHandler reset, ShutdownHandler shutdown
1.85 + ) {
1.86 + this.name = name;
1.87 + this.url = url;
1.88 + this.reset = reset;
1.89 + this.shutdown = shutdown;
1.90 + }
1.91 +}