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;
28 import net.pterodactylus.util.beans.Comparer;
31 * Container for a Freenet node. A Node is capable of notifying
32 * {@link PropertyChangeListener}s if any of the contained properties change.
34 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
39 /** Property change listeners. */
40 private final List<PropertyChangeListener> propertyChangeListeners = Collections.synchronizedList(new ArrayList<PropertyChangeListener>());
42 /** Name of the “name” property. */
43 public static final String PROPERTY_NAME = "name";
45 /** Name of the “hostname” property. */
46 public static final String PROPERTY_HOSTNAME = "hostname";
48 /** Name of the “port” property. */
49 public static final String PROPERTY_PORT = "port";
51 /** The name of the node. */
54 /** The hostname of the node. */
55 private String hostname;
57 /** The port number of the node. */
65 * Adds a property change listener.
67 * @param propertyChangeListener
68 * The property change listener to add
70 public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
71 propertyChangeListeners.add(propertyChangeListener);
75 * Removes a property change listener.
77 * @param propertyChangeListener
78 * The property change listener to remove
80 public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
81 propertyChangeListeners.remove(propertyChangeListener);
85 * Notifies all listeners that a property has changed.
88 * The name of the property
90 * The old value of the property
92 * The new value of the property
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);
107 * Returns the user-given name of the node.
109 * @return The name of the node
111 public String getName() {
116 * Sets the user-given name of the node.
119 * The name of the node
121 public void setName(String name) {
122 String oldName = this.name;
124 if (!Comparer.equal(oldName, name)) {
125 firePropertyChange(PROPERTY_NAME, oldName, name);
130 * Returns the hostname of the node.
132 * @return The hostname of the node
134 public String getHostname() {
139 * Sets the hostname of the node.
142 * The hostname of the node
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);
153 * Returns the port number of the node.
155 * @return The port number of the node
157 public int getPort() {
162 * Sets the port number of the node.
165 * The port number of the node
167 public void setPort(int port) {
168 int oldPort = this.port;
170 if (oldPort != port) {
171 firePropertyChange(PROPERTY_PORT, oldPort, port);
179 public String toString() {
180 return name + " (" + hostname + ((port == 9481) ? ("") : (":" + port)) + ")";