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