ServiceAddressMap: improve public API
authorTJ <hacker@iam.tj>
Mon, 4 May 2015 15:33:34 +0000 (16:33 +0100)
committerTJ <hacker@iam.tj>
Mon, 4 May 2015 15:33:34 +0000 (16:33 +0100)
Make constructors public, add additional wrapper methods for the
underlying Map, and expand javadocs.

src/uk/ac/ntu/n0521366/wsyd/libs/net/ServiceAddressMap.java

index 72f4fef..dc008dc 100644 (file)
@@ -64,7 +64,7 @@ public class ServiceAddressMap {
          * @param timeInMillis time last seen
          * @param state whether record was added through dynamic discovery
          */
-        LastSeenHost(InetSocketAddress address, long timeInMillis, STATE state) {
+        public LastSeenHost(InetSocketAddress address, long timeInMillis, STATE state) {
             this.address = address;
             this.timeInMillis = timeInMillis;
             this.state = state;
@@ -75,10 +75,18 @@ public class ServiceAddressMap {
          * 
          * @param host 
          */
-        LastSeenHost(InetSocketAddress host) {
+        public LastSeenHost(InetSocketAddress host) {
             this(host, System.currentTimeMillis(), STATE.DYNAMIC);
         }
         
+        /**
+         * Construct a new instance of with user-defined STATE.
+         * 
+         * @param host 
+         */
+        public LastSeenHost(InetSocketAddress host, STATE state) {
+            this(host, System.currentTimeMillis(), state);
+        }
         /**
          * Formatted string representation of InetAddress and timestamp.
          * @return the representation
@@ -133,8 +141,6 @@ public class ServiceAddressMap {
         java.util.Enumeration<String> keys = this._serviceToAddressMap.keys();
         while (keys.hasMoreElements()) {
             String key = keys.nextElement();
-
-            // XXX: special handling for "all" target - never remove it
             LastSeenHost host = _serviceToAddressMap.get(key);
             if (host != null && host.state == LastSeenHost.STATE.DYNAMIC) {
                 if (host.timeInMillis < expireTime) {
@@ -175,11 +181,31 @@ public class ServiceAddressMap {
         return result;
     }
     
-    public void put(String service, LastSeenHost host) {
-        this._serviceToAddressMap.put(service, host);
+    /**
+     * Add an entry to the known services list.
+     * @param service Service name at this unique host IP address/port combination
+     * @param host new host record
+     * @return Host record previously associated with this service, or null if key didn't exist
+     */
+    public LastSeenHost put(String service, LastSeenHost host) {
+        return this._serviceToAddressMap.put(service, host);
     }
     
+    /**
+     * Get the host record associated with a service.
+     * @param service Service name required
+     * @return Host record associated with this service, or null if service isn't known
+     */
     public LastSeenHost get(String service) {
         return this._serviceToAddressMap.get(service);
     }
+    
+    /**
+     * 
+     * @param service
+     * @return 
+     */
+    public boolean containsService(String service) {
+        return this._serviceToAddressMap.containsKey(service);
+    }
 }