samples/aserverinfo/src/org/apidesign/aserverinfo/cummulativefactory/ServerConnector.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 30 Oct 2014 21:30:10 +0100
changeset 409 40cabcdcd2be
parent 292 60bb8519cb2d
permissions -rw-r--r--
Updating to NBMs from NetBeans 8.0.1 as some of them are required to run on JDK8
     1 package org.apidesign.aserverinfo.cummulativefactory;
     2 
     3 import org.apidesign.aserverinfo.spi.NameProvider;
     4 import org.apidesign.aserverinfo.spi.ResetHandler;
     5 import org.apidesign.aserverinfo.spi.URLProvider;
     6 import java.net.URL;
     7 import org.apidesign.aserverinfo.spi.ShutdownHandler;
     8 
     9 // BEGIN: aserverinfo.api
    10 /** A class to connect to server, identify it and manipulate with
    11  * its state. The <a href="http://apidesign.org">Practical API Design</a>
    12  * book used to call it AServerInfo.
    13  * <p>
    14  */
    15 public final class ServerConnector {
    16     public String getName() {
    17         return name == null ? "noname" : name.getName();
    18     }
    19     
    20     public URL getURL() {
    21         return url == null ? null : url.getURL();
    22     }
    23     
    24     public void reset() {
    25         if (reset != null) {
    26             reset.reset();
    27         }
    28     }
    29 
    30     /** Additional method for API clients not available from first version.
    31      * @since 2.0
    32      */
    33     public void shutdown() {
    34         if (shutdown != null) {
    35             shutdown.shutdown();
    36         }
    37     }
    38 // FINISH: aserverinfo.api
    39 
    40     //
    41     // cumulative factory methods
    42     //
    43     
    44     // BEGIN: aserverinfo.cumulative.factory
    45     public static ServerConnector empty() {
    46         return new ServerConnector(null, null, null, null);
    47     }
    48     
    49     public final ServerConnector nameProvider(NameProvider np) {
    50         return new ServerConnector(np, this.url, this.reset, this.shutdown);
    51     }
    52 
    53     public final ServerConnector urlProvider(URLProvider up) {
    54         return new ServerConnector(this.name, up, this.reset, this.shutdown);
    55     }
    56     public final ServerConnector reset(ResetHandler h) {
    57         return new ServerConnector(this.name, this.url, h, this.shutdown);
    58     }
    59     /** All one needs to do when there is a need to add new
    60      * style of creation is to add new <em>clonning</em> method.
    61      * @param handler
    62      * @return
    63      * @since 2.0
    64      */
    65     public final ServerConnector shutdown(ShutdownHandler handler) {
    66         return new ServerConnector(this.name, this.url, this.reset, handler);
    67     }
    68     // END: aserverinfo.cumulative.factory
    69 
    70     //
    71     // private part
    72     //
    73 
    74     private final NameProvider name;
    75     private final URLProvider url;
    76     private final ResetHandler reset;
    77     private final ShutdownHandler shutdown;
    78 
    79     private ServerConnector(
    80         NameProvider name, URLProvider url,
    81         ResetHandler reset, ShutdownHandler shutdown
    82     ) {
    83         this.name = name;
    84         this.url = url;
    85         this.reset = reset;
    86         this.shutdown = shutdown;
    87     }
    88 }