From 69f0ad903eba02c802eb8d346a2190c25c499e70 Mon Sep 17 00:00:00 2001 From: TJ Date: Mon, 4 May 2015 16:33:34 +0100 Subject: [PATCH] ServiceAddressMap: improve public API Make constructors public, add additional wrapper methods for the underlying Map, and expand javadocs. --- .../wsyd/libs/net/ServiceAddressMap.java | 38 ++++++++++++++++--- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/net/ServiceAddressMap.java b/src/uk/ac/ntu/n0521366/wsyd/libs/net/ServiceAddressMap.java index 72f4fef..dc008dc 100644 --- a/src/uk/ac/ntu/n0521366/wsyd/libs/net/ServiceAddressMap.java +++ b/src/uk/ac/ntu/n0521366/wsyd/libs/net/ServiceAddressMap.java @@ -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 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); + } } -- 2.17.1