samples/aserverinfo/src/org/apidesign/aserverinfo/builder/ServerInfo.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 15 Nov 2008 08:19:20 +0100
changeset 294 0753acb192c7
child 300 b704be5f8463
permissions -rw-r--r--
Adding builder pattern
     1 package org.apidesign.aserverinfo.builder;
     2 
     3 import org.apidesign.aserverinfo.spi.NameProvider;
     4 import org.apidesign.aserverinfo.spi.ResetHandler;
     5 import org.apidesign.aserverinfo.spi.ShutdownHandler;
     6 import org.apidesign.aserverinfo.spi.URLProvider;
     7 
     8 /**
     9  *
    10  * @author Jaroslav Tulach <jtulach@netbeans.org>
    11  */
    12 public class ServerInfo {
    13 
    14     //
    15     // cumulative factory methods for the builder pattern
    16     //
    17 
    18     // BEGIN: aserverinfo.builder.factory
    19     public static ServerInfo empty() {
    20         return new ServerInfo(null, null, null, null);
    21     }
    22 
    23     public final ServerInfo nameProvider(NameProvider np) {
    24         return new ServerInfo(np, this.url, this.reset, this.shutdown);
    25     }
    26 
    27     public final ServerInfo urlProvider(URLProvider up) {
    28         return new ServerInfo(this.name, up, this.reset, this.shutdown);
    29     }
    30     public final ServerInfo reset(ResetHandler h) {
    31         return new ServerInfo(this.name, this.url, h, this.shutdown);
    32     }
    33     /** All one needs to do when there is a need to add new
    34      * style of creation is to add new method for a builder.
    35      * @param handler
    36      * @return
    37      * @since 2.0
    38      */
    39     public final ServerInfo shutdown(ShutdownHandler handler) {
    40         return new ServerInfo(this.name, this.url, this.reset, handler);
    41     }
    42     
    43     /** Creates the server connector based on current values in the 
    44      * info. Builder factory method.
    45      * @return server connector
    46      */
    47     public final ServerConnector connect() {
    48         return new ServerConnector(name, url, reset, shutdown);
    49     }
    50     // END: aserverinfo.builder.factory
    51 
    52     private final NameProvider name;
    53     private final URLProvider url;
    54     private final ResetHandler reset;
    55     private final ShutdownHandler shutdown;
    56 
    57     private ServerInfo(
    58         NameProvider name, URLProvider url,
    59         ResetHandler reset, ShutdownHandler shutdown
    60     ) {
    61         this.name = name;
    62         this.url = url;
    63         this.reset = reset;
    64         this.shutdown = shutdown;
    65     }
    66 
    67 }