samples/aserverinfo/src/org/apidesign/aserverinfo/AServerInfo.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:53:57 +0200
changeset 91 3280450405a0
parent 90 30a0c70c6579
child 92 f6e64df4f962
permissions -rw-r--r--
review2: Note about Tune and DVB Central. Finished example with AServerInfo
jtulach@90
     1
package org.apidesign.aserverinfo;
jtulach@90
     2
jtulach@90
     3
import java.net.URL;
jtulach@90
     4
import java.util.concurrent.Callable;
jtulach@90
     5
import org.openide.util.Lookup;
jtulach@90
     6
jtulach@90
     7
public final class AServerInfo {
jtulach@90
     8
    private final Callable<String> name;
jtulach@90
     9
    private final Callable<URL> url;
jtulach@90
    10
    private final Callable<Void> reset;
jtulach@90
    11
    private final Callable<Void> shutdown;
jtulach@90
    12
jtulach@91
    13
    private AServerInfo(Callable<String> name, Callable<URL> url, Callable<Void> reset, Callable<Void> s) {
jtulach@90
    14
        this.name = name;
jtulach@90
    15
        this.url = url;
jtulach@90
    16
        this.reset = reset;
jtulach@90
    17
        this.shutdown = s;
jtulach@90
    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@90
    25
        return call(name, "noname");
jtulach@90
    26
    }
jtulach@90
    27
    
jtulach@90
    28
    public URL getURL() {
jtulach@90
    29
        return call(url, null);
jtulach@90
    30
    }
jtulach@90
    31
    
jtulach@90
    32
    public void reset() throws Exception {
jtulach@91
    33
        if (reset != null) {
jtulach@91
    34
            reset.call();
jtulach@91
    35
        }
jtulach@90
    36
    }
jtulach@90
    37
    
jtulach@90
    38
    public void shutdown() throws Exception {
jtulach@91
    39
        if (shutdown != null) {
jtulach@91
    40
            shutdown.call();
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@90
    62
        Callable<String> nameP = new Callable<String>() {
jtulach@90
    63
            public String call() throws Exception {
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@90
    68
        Callable<URL> urlP = new Callable<URL>() {
jtulach@90
    69
            public URL call() throws Exception {
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@90
    74
        Callable<Void> resetP = new Callable<Void>() {
jtulach@90
    75
            public Void call() throws Exception {
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
                return null;
jtulach@90
    81
            }
jtulach@90
    82
        };
jtulach@90
    83
        
jtulach@90
    84
        return new AServerInfo(nameP, urlP, resetP, null);
jtulach@90
    85
    }
jtulach@90
    86
    
jtulach@90
    87
    // BEGIN: aserverinfo.regularcreate
jtulach@90
    88
    public static AServerInfo create(NameProvider nameProvider, URLProvider urlProvider, ResetHandler reset)
jtulach@90
    89
    // END: aserverinfo.regularcreate
jtulach@90
    90
    {
jtulach@90
    91
        return create(nameProvider, urlProvider, reset, null);
jtulach@90
    92
    }
jtulach@90
    93
    
jtulach@90
    94
    // BEGIN: aserverinfo.regularcreate.withshutdown
jtulach@90
    95
    /** @since 2.0 */
jtulach@90
    96
    public interface ShutdownHandler {
jtulach@90
    97
        public void shutdown();
jtulach@90
    98
    }
jtulach@90
    99
    
jtulach@90
   100
    /** @since 2.0 */
jtulach@90
   101
    public static AServerInfo create(NameProvider nameProvider, URLProvider urlProvider, ResetHandler reset, ShutdownHandler shutdown)
jtulach@90
   102
    // END: aserverinfo.regularcreate.withshutdown
jtulach@90
   103
    {
jtulach@90
   104
        final NameProvider np = nameProvider;
jtulach@90
   105
        final URLProvider up = urlProvider;
jtulach@90
   106
        final ResetHandler h = reset;
jtulach@90
   107
        final ShutdownHandler s = shutdown;
jtulach@90
   108
        
jtulach@90
   109
        Callable<String> nameP = new Callable<String>() {
jtulach@90
   110
            public String call() throws Exception {
jtulach@90
   111
                return np == null ? "noname" : np.getName();
jtulach@90
   112
            }
jtulach@90
   113
        };
jtulach@90
   114
        Callable<URL> urlP = new Callable<URL>() {
jtulach@90
   115
            public URL call() throws Exception {
jtulach@90
   116
                return up == null ? null : up.getURL();
jtulach@90
   117
            }
jtulach@90
   118
        };
jtulach@90
   119
        Callable<Void> resetP = new Callable<Void>() {
jtulach@90
   120
            public Void call() throws Exception {
jtulach@90
   121
                if (h != null) {
jtulach@90
   122
                    h.reset();
jtulach@90
   123
                }
jtulach@90
   124
                return null;
jtulach@90
   125
            }
jtulach@90
   126
        };
jtulach@90
   127
        Callable<Void> shutP = new Callable<Void>() {
jtulach@90
   128
            public Void call() throws Exception {
jtulach@90
   129
                if (s != null) {
jtulach@90
   130
                    s.shutdown();
jtulach@90
   131
                }
jtulach@90
   132
                return null;
jtulach@90
   133
            }
jtulach@90
   134
        };
jtulach@90
   135
        
jtulach@90
   136
        return new AServerInfo(nameP, urlP, resetP, shutP);
jtulach@90
   137
    }
jtulach@91
   138
jtulach@91
   139
    //
jtulach@91
   140
    // cumulative factory methods
jtulach@91
   141
    //
jtulach@90
   142
    
jtulach@91
   143
    // BEGIN: aserverinfo.cumulative.factory
jtulach@91
   144
    public static AServerInfo empty() {
jtulach@91
   145
        return new AServerInfo(null, null, null, null);
jtulach@91
   146
    }
jtulach@90
   147
    
jtulach@91
   148
    public final AServerInfo nameProvider(final NameProvider np) {
jtulach@91
   149
        Callable<String> nameP = new Callable<String>() {
jtulach@91
   150
            public String call() throws Exception {
jtulach@91
   151
                return np == null ? "noname" : np.getName();
jtulach@91
   152
            }
jtulach@91
   153
        };
jtulach@91
   154
        return new AServerInfo(nameP, this.url, this.reset, this.shutdown);
jtulach@91
   155
    }
jtulach@91
   156
    // END: aserverinfo.cumulative.empty
jtulach@91
   157
jtulach@91
   158
    public final AServerInfo urlProvider(final URLProvider up) {
jtulach@91
   159
        Callable<URL> urlP = new Callable<URL>() {
jtulach@91
   160
            public URL call() throws Exception {
jtulach@91
   161
                return up == null ? null : up.getURL();
jtulach@91
   162
            }
jtulach@91
   163
        };
jtulach@91
   164
        return new AServerInfo(this.name, urlP, this.reset, this.shutdown);
jtulach@91
   165
    }
jtulach@91
   166
    public final AServerInfo reset(final ResetHandler h) {
jtulach@91
   167
        Callable<Void> resetP = new Callable<Void>() {
jtulach@91
   168
            public Void call() throws Exception {
jtulach@91
   169
                if (h != null) {
jtulach@91
   170
                    h.reset();
jtulach@91
   171
                }
jtulach@91
   172
                return null;
jtulach@91
   173
            }
jtulach@91
   174
        };
jtulach@91
   175
        return new AServerInfo(this.name, this.url, resetP, this.shutdown);
jtulach@91
   176
    }
jtulach@91
   177
    public final AServerInfo shutdown(final ShutdownHandler s) {
jtulach@91
   178
        Callable<Void> shutP = new Callable<Void>() {
jtulach@91
   179
            public Void call() throws Exception {
jtulach@91
   180
                if (s != null) {
jtulach@91
   181
                    s.shutdown();
jtulach@91
   182
                }
jtulach@91
   183
                return null;
jtulach@91
   184
            }
jtulach@91
   185
        };
jtulach@91
   186
        return new AServerInfo(this.name, this.url, this.reset, shutP);
jtulach@91
   187
    }
jtulach@91
   188
    
jtulach@91
   189
    //
jtulach@91
   190
    // Support implementations
jtulach@91
   191
    //
jtulach@91
   192
    
jtulach@91
   193
    private static <T> T call(Callable<T> name, T defValue) {
jtulach@91
   194
        try {
jtulach@91
   195
            return name.call();
jtulach@91
   196
        } catch (Exception ex) {
jtulach@91
   197
            return defValue;
jtulach@91
   198
        }
jtulach@91
   199
    }
jtulach@91
   200
jtulach@90
   201
    
jtulach@90
   202
}