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;
+
/**
* Container for a Freenet node.
*
*/
public class Node {
+ /** Property change listeners. */
+ private final List<PropertyChangeListener> propertyChangeListeners = Collections.synchronizedList(new ArrayList<PropertyChangeListener>());
+
/** The name of the node. */
private String name;
/** The port number of the node. */
private int port;
- /** Whether the node is running on the same machine as jSite. */
- private boolean sameMachine;
+ //
+ // EVENT MANAGEMENT
+ //
+
+ /**
+ * Adds a property change listener.
+ *
+ * @param propertyChangeListener
+ * The property change listener to add
+ */
+ public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
+ propertyChangeListeners.add(propertyChangeListener);
+ }
+
+ /**
+ * Removes a property change listener.
+ *
+ * @param propertyChangeListener
+ * The property change listener to remove
+ */
+ public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
+ propertyChangeListeners.remove(propertyChangeListener);
+ }
+
+ /**
+ * 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
+ */
+ private void firePropertyChange(String property, Object oldValue, Object newValue) {
+ PropertyChangeEvent propertyChangeEvent = new PropertyChangeEvent(this, property, oldValue, newValue);
+ for (PropertyChangeListener propertyChangeListener: propertyChangeListeners) {
+ propertyChangeListener.propertyChange(propertyChangeEvent);
+ }
+
+ }
+
+ //
+ // ACCESSORS
+ //
/**
* Returns the user-given name of the node.
* 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);
+ }
}
/**
* 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);
+ }
}
/**
* 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);
+ }
}
- /**
- * Returns whether this node is running on the same machine as jSite.
- *
- * @return the sameMachine <code>true</code> if, and only if, the node is
- * running on the same machine as jSite
- */
- boolean isSameMachine() {
- return sameMachine;
- }
+// /**
+// * {@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);
+// }
+
/**
- * Sets whether this node is running on the same machine as jSite.
- *
- * @param sameMachine
- * <code>true</code> if the node is running on the same machine
- * as jSite, <code>false</code> otherwise
+ * {@inheritDoc}
*/
- void setSameMachine(boolean sameMachine) {
- this.sameMachine = sameMachine;
+ @Override
+ public String toString() {
+ return name + " (" + hostname + ((port == 9481) ? ("") : (":" + port)) + ")";
}
}