X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fcore%2FNode.java;h=3d35b4727189f31fd7ed94e06423880ea5725fba;hb=c63257e8cc0ba1a5aca9364b22171abe7279d479;hp=afcce6a8a410833e7a1271dc41db5355a618fb30;hpb=f58c676a286a7cd8d37c3f510e787144a9bff5ad;p=jSite2.git diff --git a/src/net/pterodactylus/jsite/core/Node.java b/src/net/pterodactylus/jsite/core/Node.java index afcce6a..3d35b47 100644 --- a/src/net/pterodactylus/jsite/core/Node.java +++ b/src/net/pterodactylus/jsite/core/Node.java @@ -19,22 +19,31 @@ package net.pterodactylus.jsite.core; -import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; + +import net.pterodactylus.jsite.util.IdGenerator; +import net.pterodactylus.util.beans.AbstractBean; +import net.pterodactylus.util.number.Hex; /** - * Container for a Freenet node. - * + * Container for a Freenet node. A Node is capable of notifying + * {@link PropertyChangeListener}s if any of the contained properties change. + * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> - * @version $Id$ */ -public class Node { +public class Node extends AbstractBean { + + /** Name of the “name” property. */ + public static final String PROPERTY_NAME = "name"; - /** Property change listeners. */ - private final List propertyChangeListeners = Collections.synchronizedList(new ArrayList()); + /** Name of the “hostname” property. */ + public static final String PROPERTY_HOSTNAME = "hostname"; + + /** Name of the “port” property. */ + public static final String PROPERTY_PORT = "port"; + + /** Internal ID. */ + private String id; /** The name of the node. */ private String name; @@ -45,55 +54,39 @@ public class Node { /** The port number of the node. */ private int port; - // - // EVENT MANAGEMENT - // - /** - * Adds a property change listener. - * - * @param propertyChangeListener - * The property change listener to add + * Creates a new node. */ - public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) { - propertyChangeListeners.add(propertyChangeListener); + public Node() { + id = Hex.toHex(IdGenerator.generateId()); } /** - * Removes a property change listener. - * - * @param propertyChangeListener - * The property change listener to remove + * Returns the internal ID of the node. + * + * @return The internal ID of the node */ - public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) { - propertyChangeListeners.remove(propertyChangeListener); + String getId() { + return id; } /** - * Notifies all listeners that a property has changed. - * - * @param property - * The name of the property - * @param oldValue - * The old value of the property - * @param newValue - * The new value of the property + * Sets the internal ID of the node. + * + * @param id + * The internal ID of the node */ - private void firePropertyChange(String property, Object oldValue, Object newValue) { - PropertyChangeEvent propertyChangeEvent = new PropertyChangeEvent(this, property, oldValue, newValue); - for (PropertyChangeListener propertyChangeListener: propertyChangeListeners) { - propertyChangeListener.propertyChange(propertyChangeEvent); + void setId(String id) { + if (id == null) { + this.id = Hex.toHex(IdGenerator.generateId()); + } else { + this.id = id; } - } - // - // ACCESSORS - // - /** * Returns the user-given name of the node. - * + * * @return The name of the node */ public String getName() { @@ -102,21 +95,19 @@ public class Node { /** * Sets the user-given name of the node. - * + * * @param name * The name of the node */ public void setName(String name) { String oldName = this.name; this.name = name; - if (((oldName != null) && (name == null)) || ((oldName == null) && (name != null)) || ((name != null) && !name.equals(oldName))) { - firePropertyChange("name", oldName, name); - } + fireIfPropertyChanged(PROPERTY_NAME, oldName, name); } /** * Returns the hostname of the node. - * + * * @return The hostname of the node */ public String getHostname() { @@ -125,21 +116,19 @@ public class Node { /** * Sets the hostname of the node. - * + * * @param hostname * The hostname of the node */ public void setHostname(String hostname) { String oldHostname = this.hostname; this.hostname = hostname; - if (((oldHostname != null) && (hostname == null)) || ((oldHostname == null) && (hostname != null)) || ((hostname != null) && !hostname.equals(oldHostname))) { - firePropertyChange("hostname", oldHostname, hostname); - } + fireIfPropertyChanged(PROPERTY_HOSTNAME, oldHostname, hostname); } /** * Returns the port number of the node. - * + * * @return The port number of the node */ public int getPort() { @@ -148,45 +137,22 @@ public class Node { /** * Sets the port number of the node. - * + * * @param port * The port number of the node */ public void setPort(int port) { int oldPort = this.port; this.port = port; - if (oldPort != port) { - firePropertyChange("port", oldPort, port); - } + fireIfPropertyChanged(PROPERTY_PORT, oldPort, port); } -// /** -// * {@inheritDoc} Two Node objects are considered equal if their hostnames -// * and their port numbers are equal. -// */ -// @Override -// public boolean equals(Object object) { -// if ((object == null) || !(object instanceof Node)) { -// return false; -// } -// Node node = (Node) object; -// return hostname.equals(node.hostname) && port == node.port; -// } - -// /** -// * {@inheritDoc} -// */ -// @Override -// public int hashCode() { -// return hostname.hashCode() ^ (-1 - port); -// } - /** * {@inheritDoc} */ @Override public String toString() { - return name + " (" + hostname + ((port == 9481) ? ("") : (":" + port)) + ")"; + return getClass().getName() + "[name=" + name + ",hostname=" + hostname + ",port=" + port + "]"; } }