use comparer
[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 import net.pterodactylus.util.beans.Comparer;
29
30 /**
31  * Container for a Freenet node. A Node is capable of notifying
32  * {@link PropertyChangeListener}s if any of the contained properties change.
33  * 
34  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
35  * @version $Id$
36  */
37 public class Node {
38
39         /** Property change listeners. */
40         private final List<PropertyChangeListener> propertyChangeListeners = Collections.synchronizedList(new ArrayList<PropertyChangeListener>());
41
42         /** Name of the “name” property. */
43         public static final String PROPERTY_NAME = "name";
44
45         /** Name of the “hostname” property. */
46         public static final String PROPERTY_HOSTNAME = "hostname";
47
48         /** Name of the “port” property. */
49         public static final String PROPERTY_PORT = "port";
50
51         /** The name of the node. */
52         private String name;
53
54         /** The hostname of the node. */
55         private String hostname;
56
57         /** The port number of the node. */
58         private int port;
59
60         //
61         // EVENT MANAGEMENT
62         //
63
64         /**
65          * Adds a property change listener.
66          * 
67          * @param propertyChangeListener
68          *            The property change listener to add
69          */
70         public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
71                 propertyChangeListeners.add(propertyChangeListener);
72         }
73
74         /**
75          * Removes a property change listener.
76          * 
77          * @param propertyChangeListener
78          *            The property change listener to remove
79          */
80         public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
81                 propertyChangeListeners.remove(propertyChangeListener);
82         }
83
84         /**
85          * Notifies all listeners that a property has changed.
86          * 
87          * @param property
88          *            The name of the property
89          * @param oldValue
90          *            The old value of the property
91          * @param newValue
92          *            The new value of the property
93          */
94         private void firePropertyChange(String property, Object oldValue, Object newValue) {
95                 PropertyChangeEvent propertyChangeEvent = new PropertyChangeEvent(this, property, oldValue, newValue);
96                 for (PropertyChangeListener propertyChangeListener: propertyChangeListeners) {
97                         propertyChangeListener.propertyChange(propertyChangeEvent);
98                 }
99
100         }
101
102         //
103         // ACCESSORS
104         //
105
106         /**
107          * Returns the user-given name of the node.
108          * 
109          * @return The name of the node
110          */
111         public String getName() {
112                 return name;
113         }
114
115         /**
116          * Sets the user-given name of the node.
117          * 
118          * @param name
119          *            The name of the node
120          */
121         public void setName(String name) {
122                 String oldName = this.name;
123                 this.name = name;
124                 if (!Comparer.equal(oldName, name)) {
125                         firePropertyChange(PROPERTY_NAME, oldName, name);
126                 }
127         }
128
129         /**
130          * Returns the hostname of the node.
131          * 
132          * @return The hostname of the node
133          */
134         public String getHostname() {
135                 return hostname;
136         }
137
138         /**
139          * Sets the hostname of the node.
140          * 
141          * @param hostname
142          *            The hostname of the node
143          */
144         public void setHostname(String hostname) {
145                 String oldHostname = this.hostname;
146                 this.hostname = hostname;
147                 if (!Comparer.equal(oldHostname, hostname)) {
148                         firePropertyChange(PROPERTY_HOSTNAME, oldHostname, hostname);
149                 }
150         }
151
152         /**
153          * Returns the port number of the node.
154          * 
155          * @return The port number of the node
156          */
157         public int getPort() {
158                 return port;
159         }
160
161         /**
162          * Sets the port number of the node.
163          * 
164          * @param port
165          *            The port number of the node
166          */
167         public void setPort(int port) {
168                 int oldPort = this.port;
169                 this.port = port;
170                 if (oldPort != port) {
171                         firePropertyChange(PROPERTY_PORT, oldPort, port);
172                 }
173         }
174
175         /**
176          * {@inheritDoc}
177          */
178         @Override
179         public String toString() {
180                 return name + " (" + hostname + ((port == 9481) ? ("") : (":" + port)) + ")";
181         }
182
183 }