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