make connections work
[jSite2.git] / src / net / pterodactylus / jsite / core / Node.java
1 /*
2  * jSite2 - Node.java -
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.jsite.core;
21
22 import java.beans.PropertyChangeEvent;
23 import java.beans.PropertyChangeListener;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27
28 /**
29  * Container for a Freenet node.
30  * 
31  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
32  * @version $Id$
33  */
34 public class Node {
35
36         /** Property change listeners. */
37         private final List<PropertyChangeListener> propertyChangeListeners = Collections.synchronizedList(new ArrayList<PropertyChangeListener>());
38
39         /** The name of the node. */
40         private String name;
41
42         /** The hostname of the node. */
43         private String hostname;
44
45         /** The port number of the node. */
46         private int port;
47
48         //
49         // EVENT MANAGEMENT
50         //
51
52         /**
53          * Adds a property change listener.
54          * 
55          * @param propertyChangeListener
56          *            The property change listener to add
57          */
58         public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
59                 propertyChangeListeners.add(propertyChangeListener);
60         }
61
62         /**
63          * Removes a property change listener.
64          * 
65          * @param propertyChangeListener
66          *            The property change listener to remove
67          */
68         public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
69                 propertyChangeListeners.remove(propertyChangeListener);
70         }
71
72         /**
73          * Notifies all listeners that a property has changed.
74          * 
75          * @param property
76          *            The name of the property
77          * @param oldValue
78          *            The old value of the property
79          * @param newValue
80          *            The new value of the property
81          */
82         private void firePropertyChange(String property, Object oldValue, Object newValue) {
83                 PropertyChangeEvent propertyChangeEvent = new PropertyChangeEvent(this, property, oldValue, newValue);
84                 for (PropertyChangeListener propertyChangeListener: propertyChangeListeners) {
85                         propertyChangeListener.propertyChange(propertyChangeEvent);
86                 }
87
88         }
89
90         //
91         // ACCESSORS
92         //
93
94         /**
95          * Returns the user-given name of the node.
96          * 
97          * @return The name of the node
98          */
99         public String getName() {
100                 return name;
101         }
102
103         /**
104          * Sets the user-given name of the node.
105          * 
106          * @param name
107          *            The name of the node
108          */
109         public void setName(String name) {
110                 String oldName = this.name;
111                 this.name = name;
112                 if (((oldName != null) && (name == null)) || ((oldName == null) && (name != null)) || ((name != null) && !name.equals(oldName))) {
113                         firePropertyChange("name", oldName, name);
114                 }
115         }
116
117         /**
118          * Returns the hostname of the node.
119          * 
120          * @return The hostname of the node
121          */
122         public String getHostname() {
123                 return hostname;
124         }
125
126         /**
127          * Sets the hostname of the node.
128          * 
129          * @param hostname
130          *            The hostname of the node
131          */
132         public void setHostname(String hostname) {
133                 String oldHostname = this.hostname;
134                 this.hostname = hostname;
135                 if (((oldHostname != null) && (hostname == null)) || ((oldHostname == null) && (hostname != null)) || ((hostname != null) && !hostname.equals(oldHostname))) {
136                         firePropertyChange("hostname", oldHostname, hostname);
137                 }
138         }
139
140         /**
141          * Returns the port number of the node.
142          * 
143          * @return The port number of the node
144          */
145         public int getPort() {
146                 return port;
147         }
148
149         /**
150          * Sets the port number of the node.
151          * 
152          * @param port
153          *            The port number of the node
154          */
155         public void setPort(int port) {
156                 int oldPort = this.port;
157                 this.port = port;
158                 if (oldPort != port) {
159                         firePropertyChange("port", oldPort, port);
160                 }
161         }
162
163 //      /**
164 //       * {@inheritDoc} Two Node objects are considered equal if their hostnames
165 //       * and their port numbers are equal.
166 //       */
167 //      @Override
168 //      public boolean equals(Object object) {
169 //              if ((object == null) || !(object instanceof Node)) {
170 //                      return false;
171 //              }
172 //              Node node = (Node) object;
173 //              return hostname.equals(node.hostname) && port == node.port;
174 //      }
175
176 //      /**
177 //       * {@inheritDoc}
178 //       */
179 //      @Override
180 //      public int hashCode() {
181 //              return hostname.hashCode() ^ (-1 - port);
182 //      }
183         
184         /**
185          * {@inheritDoc}
186          */
187         @Override
188         public String toString() {
189                 return name + " (" + hostname + ((port == 9481) ? ("") : (":" + port)) + ")";
190         }
191
192 }