skip known identifiers in initial request list
[jSite2.git] / src / net / pterodactylus / jsite / core / Node.java
index 854a668..afcce6a 100644 (file)
 
 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.
  * 
@@ -27,6 +33,9 @@ package net.pterodactylus.jsite.core;
  */
 public class Node {
 
+       /** Property change listeners. */
+       private final List<PropertyChangeListener> propertyChangeListeners = Collections.synchronizedList(new ArrayList<PropertyChangeListener>());
+
        /** The name of the node. */
        private String name;
 
@@ -36,8 +45,51 @@ public class Node {
        /** 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.
@@ -55,7 +107,11 @@ public class 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);
+               }
        }
 
        /**
@@ -74,7 +130,11 @@ public class Node {
         *            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);
+               }
        }
 
        /**
@@ -93,30 +153,34 @@ public class Node {
         *            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
-        */
-       public boolean isSameMachine() {
-               return sameMachine;
-       }
-
-       /**
-        * 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
-        */
-       public void setSameMachine(boolean sameMachine) {
-               this.sameMachine = 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);
+//     }
+       
        /**
         * {@inheritDoc}
         */