Way simplified internals of the AServerInfo class
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:53:57 +0200
changeset 92f6e64df4f962
parent 91 3280450405a0
child 93 f335133cc6d6
Way simplified internals of the AServerInfo class
samples/aserverinfo/src/org/apidesign/aserverinfo/AServerInfo.java
     1.1 --- a/samples/aserverinfo/src/org/apidesign/aserverinfo/AServerInfo.java	Sat Jun 14 09:53:57 2008 +0200
     1.2 +++ b/samples/aserverinfo/src/org/apidesign/aserverinfo/AServerInfo.java	Sat Jun 14 09:53:57 2008 +0200
     1.3 @@ -1,43 +1,43 @@
     1.4  package org.apidesign.aserverinfo;
     1.5  
     1.6  import java.net.URL;
     1.7 -import java.util.concurrent.Callable;
     1.8  import org.openide.util.Lookup;
     1.9  
    1.10  public final class AServerInfo {
    1.11 -    private final Callable<String> name;
    1.12 -    private final Callable<URL> url;
    1.13 -    private final Callable<Void> reset;
    1.14 -    private final Callable<Void> shutdown;
    1.15 +    private final NameProvider name;
    1.16 +    private final URLProvider url;
    1.17 +    private final ResetHandler reset;
    1.18 +    private final ShutdownHandler shutdown;
    1.19  
    1.20 -    private AServerInfo(Callable<String> name, Callable<URL> url, Callable<Void> reset, Callable<Void> s) {
    1.21 +    private AServerInfo(NameProvider name, URLProvider url, ResetHandler reset, ShutdownHandler shutdown) {
    1.22          this.name = name;
    1.23          this.url = url;
    1.24          this.reset = reset;
    1.25 -        this.shutdown = s;
    1.26 +        this.shutdown = shutdown;
    1.27      }
    1.28 +
    1.29      
    1.30      //
    1.31      // API for clients
    1.32      //
    1.33      
    1.34      public String getName() {
    1.35 -        return call(name, "noname");
    1.36 +        return name == null ? "noname" : name.getName();
    1.37      }
    1.38      
    1.39      public URL getURL() {
    1.40 -        return call(url, null);
    1.41 +        return url == null ? null : url.getURL();
    1.42      }
    1.43      
    1.44 -    public void reset() throws Exception {
    1.45 +    public void reset() {
    1.46          if (reset != null) {
    1.47 -            reset.call();
    1.48 +            reset.reset();
    1.49          }
    1.50      }
    1.51      
    1.52 -    public void shutdown() throws Exception {
    1.53 +    public void shutdown() {
    1.54          if (shutdown != null) {
    1.55 -            shutdown.call();
    1.56 +            shutdown.shutdown();
    1.57          }
    1.58      }
    1.59  
    1.60 @@ -59,25 +59,24 @@
    1.61      public static AServerInfo create(final Lookup interfaces) 
    1.62      // END: aserverinfo.create
    1.63      {
    1.64 -        Callable<String> nameP = new Callable<String>() {
    1.65 -            public String call() throws Exception {
    1.66 +        NameProvider nameP = new NameProvider() {
    1.67 +            public String getName() {
    1.68                  NameProvider p = interfaces.lookup(NameProvider.class);
    1.69                  return p == null ? "noname" : p.getName();
    1.70              }
    1.71          };
    1.72 -        Callable<URL> urlP = new Callable<URL>() {
    1.73 -            public URL call() throws Exception {
    1.74 +        URLProvider urlP = new URLProvider() {
    1.75 +            public URL getURL() {
    1.76                  URLProvider p = interfaces.lookup(URLProvider.class);
    1.77                  return p == null ? null : p.getURL();
    1.78              }
    1.79          };
    1.80 -        Callable<Void> resetP = new Callable<Void>() {
    1.81 -            public Void call() throws Exception {
    1.82 +        ResetHandler resetP = new ResetHandler() {
    1.83 +            public void reset() {
    1.84                  ResetHandler h = interfaces.lookup(ResetHandler.class);
    1.85                  if (h != null) {
    1.86                      h.reset();
    1.87                  }
    1.88 -                return null;
    1.89              }
    1.90          };
    1.91          
    1.92 @@ -88,7 +87,7 @@
    1.93      public static AServerInfo create(NameProvider nameProvider, URLProvider urlProvider, ResetHandler reset)
    1.94      // END: aserverinfo.regularcreate
    1.95      {
    1.96 -        return create(nameProvider, urlProvider, reset, null);
    1.97 +        return new AServerInfo(nameProvider, urlProvider, reset, null);
    1.98      }
    1.99      
   1.100      // BEGIN: aserverinfo.regularcreate.withshutdown
   1.101 @@ -101,39 +100,7 @@
   1.102      public static AServerInfo create(NameProvider nameProvider, URLProvider urlProvider, ResetHandler reset, ShutdownHandler shutdown)
   1.103      // END: aserverinfo.regularcreate.withshutdown
   1.104      {
   1.105 -        final NameProvider np = nameProvider;
   1.106 -        final URLProvider up = urlProvider;
   1.107 -        final ResetHandler h = reset;
   1.108 -        final ShutdownHandler s = shutdown;
   1.109 -        
   1.110 -        Callable<String> nameP = new Callable<String>() {
   1.111 -            public String call() throws Exception {
   1.112 -                return np == null ? "noname" : np.getName();
   1.113 -            }
   1.114 -        };
   1.115 -        Callable<URL> urlP = new Callable<URL>() {
   1.116 -            public URL call() throws Exception {
   1.117 -                return up == null ? null : up.getURL();
   1.118 -            }
   1.119 -        };
   1.120 -        Callable<Void> resetP = new Callable<Void>() {
   1.121 -            public Void call() throws Exception {
   1.122 -                if (h != null) {
   1.123 -                    h.reset();
   1.124 -                }
   1.125 -                return null;
   1.126 -            }
   1.127 -        };
   1.128 -        Callable<Void> shutP = new Callable<Void>() {
   1.129 -            public Void call() throws Exception {
   1.130 -                if (s != null) {
   1.131 -                    s.shutdown();
   1.132 -                }
   1.133 -                return null;
   1.134 -            }
   1.135 -        };
   1.136 -        
   1.137 -        return new AServerInfo(nameP, urlP, resetP, shutP);
   1.138 +        return new AServerInfo(nameProvider, urlProvider, reset, shutdown);
   1.139      }
   1.140  
   1.141      //
   1.142 @@ -146,57 +113,17 @@
   1.143      }
   1.144      
   1.145      public final AServerInfo nameProvider(final NameProvider np) {
   1.146 -        Callable<String> nameP = new Callable<String>() {
   1.147 -            public String call() throws Exception {
   1.148 -                return np == null ? "noname" : np.getName();
   1.149 -            }
   1.150 -        };
   1.151 -        return new AServerInfo(nameP, this.url, this.reset, this.shutdown);
   1.152 +        return new AServerInfo(np, this.url, this.reset, this.shutdown);
   1.153      }
   1.154      // END: aserverinfo.cumulative.empty
   1.155  
   1.156      public final AServerInfo urlProvider(final URLProvider up) {
   1.157 -        Callable<URL> urlP = new Callable<URL>() {
   1.158 -            public URL call() throws Exception {
   1.159 -                return up == null ? null : up.getURL();
   1.160 -            }
   1.161 -        };
   1.162 -        return new AServerInfo(this.name, urlP, this.reset, this.shutdown);
   1.163 +        return new AServerInfo(this.name, up, this.reset, this.shutdown);
   1.164      }
   1.165      public final AServerInfo reset(final ResetHandler h) {
   1.166 -        Callable<Void> resetP = new Callable<Void>() {
   1.167 -            public Void call() throws Exception {
   1.168 -                if (h != null) {
   1.169 -                    h.reset();
   1.170 -                }
   1.171 -                return null;
   1.172 -            }
   1.173 -        };
   1.174 -        return new AServerInfo(this.name, this.url, resetP, this.shutdown);
   1.175 +        return new AServerInfo(this.name, this.url, h, this.shutdown);
   1.176      }
   1.177      public final AServerInfo shutdown(final ShutdownHandler s) {
   1.178 -        Callable<Void> shutP = new Callable<Void>() {
   1.179 -            public Void call() throws Exception {
   1.180 -                if (s != null) {
   1.181 -                    s.shutdown();
   1.182 -                }
   1.183 -                return null;
   1.184 -            }
   1.185 -        };
   1.186 -        return new AServerInfo(this.name, this.url, this.reset, shutP);
   1.187 +        return new AServerInfo(this.name, this.url, this.reset, s);
   1.188      }
   1.189 -    
   1.190 -    //
   1.191 -    // Support implementations
   1.192 -    //
   1.193 -    
   1.194 -    private static <T> T call(Callable<T> name, T defValue) {
   1.195 -        try {
   1.196 -            return name.call();
   1.197 -        } catch (Exception ex) {
   1.198 -            return defValue;
   1.199 -        }
   1.200 -    }
   1.201 -
   1.202 -    
   1.203  }