# HG changeset patch # User Severin Gehwolf # Date 1362670279 -3600 # Node ID aa0c8707c880ceccaf2809e99b9fe7b349b4085c # Parent 8ea56b57334c6aa8111da9debd7acf32a74f980f Cleanup Connection class. Reviewed-by: omajid Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-March/006006.html diff -r 8ea56b57334c -r aa0c8707c880 storage/core/src/main/java/com/redhat/thermostat/storage/core/Connection.java --- a/storage/core/src/main/java/com/redhat/thermostat/storage/core/Connection.java Thu Feb 21 16:00:50 2013 +0100 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/Connection.java Thu Mar 07 16:31:19 2013 +0100 @@ -39,34 +39,17 @@ import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; +/** + * + * {@link Storage} delegates to this class for connecting to storage. + * + */ public abstract class Connection { - public enum ConnectionType { - LOCAL(false), - REMOTE(true), - CLUSTER(true), - ; - - boolean isDisplayable = false; - boolean needsUrl = false; - - private ConnectionType(boolean needsUrl) { - this.needsUrl = needsUrl; - } - - private ConnectionType(boolean isDisplayable, boolean needsUrl) { - this.isDisplayable = isDisplayable; - } - - public boolean isDisplayable() { - return isDisplayable; - } - - public boolean needsUrl() { - return needsUrl; - } - } - + /** + * @see {@link ConnectionListener}, + * + */ public enum ConnectionStatus { CONNECTED, CONNECTING, @@ -75,56 +58,104 @@ } public interface ConnectionListener { + /** + * Called by the connection reporting a status update. + * + * @param newStatus The new status. + * + * @see ConnectionStatus + */ public void changed(ConnectionStatus newStatus); } protected boolean connected = false; - private ConnectionType type; private String url; private List listeners = new CopyOnWriteArrayList<>(); - public void setType(ConnectionType type) { - this.type = type; - } - - public ConnectionType getType() { - return type; - } - + /** + * Sets the connection URL to which to connect to. + * + * @param url The connection URL. + */ public void setUrl(String url) { this.url = url; } + /** + * + * @return The connection URL. + */ public String getUrl() { return url; } @Override public String toString() { - if (url == null) { - return type.toString(); - } - return type.toString() + " to " + url; + return url; } + /** + * Connects to storage. Implementors are responsible to fire at least the + * following connection events: + * + */ public abstract void connect(); + /** + * Disconnects from storage. Implementors are responsible to fire at least + * the following connection events: + * + */ public abstract void disconnect(); + + /** + * May be used to determine the state of a connection if no connect or + * disconnect attempt is currently in progress. + * + * @return Provided the connection is not in a transitive state (e.g. + * ongoing connect/disconnect), true if storage is connected. False + * otherwise. + */ public boolean isConnected() { return connected; } + /** + * Adds a connection listener to this connection. This may be used for + * progress reporting. + * + * @param listener + * The listener which will be notified of connection events. + */ public void addListener(ConnectionListener listener) { this.listeners.add(listener); } + /** + * Removes a formerly registered listener. No-op if the given listener + * is not currently registered. + * + * @param listener The listener which should get removed. + */ public void removeListener(ConnectionListener listener) { this.listeners.remove(listener); } + /** + * Notifies all registered listeners of this ConnectionStatus event. + * + * @param status The status which is passed on to listeners. + */ protected void fireChanged(ConnectionStatus status) { for (ConnectionListener listener: listeners) { listener.changed(status);