samples/aserverinfo/src/org/apidesign/aserverinfo/AServerInfo.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 09 Nov 2008 15:44:53 +0100
changeset 290 f1828e0123fe
parent 154 0fd5e9c500b9
permissions -rw-r--r--
Showing API for AServerInfo
jtulach@90
     1
package org.apidesign.aserverinfo;
jtulach@90
     2
jtulach@90
     3
import java.net.URL;
jtulach@90
     4
import org.openide.util.Lookup;
jtulach@90
     5
jtulach@290
     6
// BEGIN: aserverinfo.api
jtulach@90
     7
public final class AServerInfo {
jtulach@90
     8
    public String getName() {
jtulach@92
     9
        return name == null ? "noname" : name.getName();
jtulach@90
    10
    }
jtulach@90
    11
    
jtulach@90
    12
    public URL getURL() {
jtulach@92
    13
        return url == null ? null : url.getURL();
jtulach@90
    14
    }
jtulach@90
    15
    
jtulach@92
    16
    public void reset() {
jtulach@91
    17
        if (reset != null) {
jtulach@92
    18
            reset.reset();
jtulach@91
    19
        }
jtulach@90
    20
    }
jtulach@90
    21
    
jtulach@92
    22
    public void shutdown() {
jtulach@91
    23
        if (shutdown != null) {
jtulach@92
    24
            shutdown.shutdown();
jtulach@90
    25
        }
jtulach@90
    26
    }
jtulach@290
    27
// FINISH: aserverinfo.api
jtulach@290
    28
jtulach@290
    29
    //
jtulach@290
    30
    // private part
jtulach@290
    31
    //
jtulach@290
    32
    
jtulach@290
    33
    private final NameProvider name;
jtulach@290
    34
    private final URLProvider url;
jtulach@290
    35
    private final ResetHandler reset;
jtulach@290
    36
    private final ShutdownHandler shutdown;
jtulach@290
    37
jtulach@290
    38
    private AServerInfo(
jtulach@290
    39
        NameProvider name, URLProvider url,
jtulach@290
    40
        ResetHandler reset, ShutdownHandler shutdown
jtulach@290
    41
    ) {
jtulach@290
    42
        this.name = name;
jtulach@290
    43
        this.url = url;
jtulach@290
    44
        this.reset = reset;
jtulach@290
    45
        this.shutdown = shutdown;
jtulach@290
    46
    }
jtulach@90
    47
jtulach@91
    48
    //
jtulach@91
    49
    // factories
jtulach@91
    50
    //
jtulach@91
    51
jtulach@90
    52
    // BEGIN: aserverinfo.create
jtulach@90
    53
    public interface NameProvider {
jtulach@90
    54
        public String getName();
jtulach@90
    55
    }
jtulach@90
    56
    public interface URLProvider {
jtulach@90
    57
        public URL getURL();
jtulach@90
    58
    }
jtulach@90
    59
    public interface ResetHandler {
jtulach@90
    60
        public void reset();
jtulach@90
    61
    }
jtulach@90
    62
    
jtulach@90
    63
    public static AServerInfo create(final Lookup interfaces) 
jtulach@90
    64
    // END: aserverinfo.create
jtulach@90
    65
    {
jtulach@92
    66
        NameProvider nameP = new NameProvider() {
jtulach@92
    67
            public String getName() {
jtulach@90
    68
                NameProvider p = interfaces.lookup(NameProvider.class);
jtulach@90
    69
                return p == null ? "noname" : p.getName();
jtulach@90
    70
            }
jtulach@90
    71
        };
jtulach@92
    72
        URLProvider urlP = new URLProvider() {
jtulach@92
    73
            public URL getURL() {
jtulach@90
    74
                URLProvider p = interfaces.lookup(URLProvider.class);
jtulach@90
    75
                return p == null ? null : p.getURL();
jtulach@90
    76
            }
jtulach@90
    77
        };
jtulach@92
    78
        ResetHandler resetP = new ResetHandler() {
jtulach@92
    79
            public void reset() {
jtulach@90
    80
                ResetHandler h = interfaces.lookup(ResetHandler.class);
jtulach@90
    81
                if (h != null) {
jtulach@90
    82
                    h.reset();
jtulach@90
    83
                }
jtulach@90
    84
            }
jtulach@90
    85
        };
jtulach@90
    86
        
jtulach@90
    87
        return new AServerInfo(nameP, urlP, resetP, null);
jtulach@90
    88
    }
jtulach@90
    89
    
jtulach@90
    90
    // BEGIN: aserverinfo.regularcreate
jtulach@154
    91
    public static AServerInfo create(
jtulach@154
    92
        NameProvider nameProvider, 
jtulach@154
    93
        URLProvider urlProvider, 
jtulach@154
    94
        ResetHandler reset
jtulach@154
    95
    )
jtulach@90
    96
    // END: aserverinfo.regularcreate
jtulach@90
    97
    {
jtulach@92
    98
        return new AServerInfo(nameProvider, urlProvider, reset, null);
jtulach@90
    99
    }
jtulach@90
   100
    
jtulach@90
   101
    // BEGIN: aserverinfo.regularcreate.withshutdown
jtulach@90
   102
    /** @since 2.0 */
jtulach@90
   103
    public interface ShutdownHandler {
jtulach@90
   104
        public void shutdown();
jtulach@90
   105
    }
jtulach@90
   106
    
jtulach@90
   107
    /** @since 2.0 */
jtulach@154
   108
    public static AServerInfo create(
jtulach@154
   109
        NameProvider nameProvider, 
jtulach@154
   110
        URLProvider urlProvider, 
jtulach@154
   111
        ResetHandler reset, 
jtulach@154
   112
        ShutdownHandler shutdown
jtulach@154
   113
    )
jtulach@90
   114
    // END: aserverinfo.regularcreate.withshutdown
jtulach@90
   115
    {
jtulach@92
   116
        return new AServerInfo(nameProvider, urlProvider, reset, shutdown);
jtulach@90
   117
    }
jtulach@91
   118
jtulach@91
   119
    //
jtulach@91
   120
    // cumulative factory methods
jtulach@91
   121
    //
jtulach@90
   122
    
jtulach@91
   123
    // BEGIN: aserverinfo.cumulative.factory
jtulach@91
   124
    public static AServerInfo empty() {
jtulach@91
   125
        return new AServerInfo(null, null, null, null);
jtulach@91
   126
    }
jtulach@90
   127
    
jtulach@91
   128
    public final AServerInfo nameProvider(final NameProvider np) {
jtulach@92
   129
        return new AServerInfo(np, this.url, this.reset, this.shutdown);
jtulach@91
   130
    }
jtulach@93
   131
    // END: aserverinfo.cumulative.factory
jtulach@91
   132
jtulach@91
   133
    public final AServerInfo urlProvider(final URLProvider up) {
jtulach@92
   134
        return new AServerInfo(this.name, up, this.reset, this.shutdown);
jtulach@91
   135
    }
jtulach@91
   136
    public final AServerInfo reset(final ResetHandler h) {
jtulach@92
   137
        return new AServerInfo(this.name, this.url, h, this.shutdown);
jtulach@91
   138
    }
jtulach@91
   139
    public final AServerInfo shutdown(final ShutdownHandler s) {
jtulach@92
   140
        return new AServerInfo(this.name, this.url, this.reset, s);
jtulach@91
   141
    }
jtulach@90
   142
}