3 * Copyright © 2008 David Roden
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.
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.
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.
20 package net.pterodactylus.jsite.core;
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;
29 * Container for a Freenet node. A Node is capable of notifying
30 * {@link PropertyChangeListener}s if any of the contained properties change.
32 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
37 /** Property change listeners. */
38 private final List<PropertyChangeListener> propertyChangeListeners = Collections.synchronizedList(new ArrayList<PropertyChangeListener>());
40 /** Name of the “name” property. */
41 public static final String PROPERTY_NAME = "name";
43 /** Name of the “hostname” property. */
44 public static final String PROPERTY_HOSTNAME = "hostname";
46 /** Name of the “port” property. */
47 public static final String PROPERTY_PORT = "port";
49 /** The name of the node. */
52 /** The hostname of the node. */
53 private String hostname;
55 /** The port number of the node. */
63 * Adds a property change listener.
65 * @param propertyChangeListener
66 * The property change listener to add
68 public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
69 propertyChangeListeners.add(propertyChangeListener);
73 * Removes a property change listener.
75 * @param propertyChangeListener
76 * The property change listener to remove
78 public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
79 propertyChangeListeners.remove(propertyChangeListener);
83 * Notifies all listeners that a property has changed.
86 * The name of the property
88 * The old value of the property
90 * The new value of the property
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);
105 * Returns the user-given name of the node.
107 * @return The name of the node
109 public String getName() {
114 * Sets the user-given name of the node.
117 * The name of the node
119 public void setName(String name) {
120 String oldName = this.name;
122 if (((oldName != null) && (name == null)) || ((oldName == null) && (name != null)) || ((name != null) && !name.equals(oldName))) {
123 firePropertyChange(PROPERTY_NAME, oldName, name);
128 * Returns the hostname of the node.
130 * @return The hostname of the node
132 public String getHostname() {
137 * Sets the hostname of the node.
140 * The hostname of the node
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);
151 * Returns the port number of the node.
153 * @return The port number of the node
155 public int getPort() {
160 * Sets the port number of the node.
163 * The port number of the node
165 public void setPort(int port) {
166 int oldPort = this.port;
168 if (oldPort != port) {
169 firePropertyChange(PROPERTY_PORT, oldPort, port);
177 public String toString() {
178 return name + " (" + hostname + ((port == 9481) ? ("") : (":" + port)) + ")";