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