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.
31 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
36 /** Property change listeners. */
37 private final List<PropertyChangeListener> propertyChangeListeners = Collections.synchronizedList(new ArrayList<PropertyChangeListener>());
39 /** The name of the node. */
42 /** The hostname of the node. */
43 private String hostname;
45 /** The port number of the node. */
53 * Adds a property change listener.
55 * @param propertyChangeListener
56 * The property change listener to add
58 public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
59 propertyChangeListeners.add(propertyChangeListener);
63 * Removes a property change listener.
65 * @param propertyChangeListener
66 * The property change listener to remove
68 public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
69 propertyChangeListeners.remove(propertyChangeListener);
73 * Notifies all listeners that a property has changed.
76 * The name of the property
78 * The old value of the property
80 * The new value of the property
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);
95 * Returns the user-given name of the node.
97 * @return The name of the node
99 public String getName() {
104 * Sets the user-given name of the node.
107 * The name of the node
109 public void setName(String name) {
110 String oldName = this.name;
112 if (((oldName != null) && (name == null)) || ((oldName == null) && (name != null)) || ((name != null) && !name.equals(oldName))) {
113 firePropertyChange("name", oldName, name);
118 * Returns the hostname of the node.
120 * @return The hostname of the node
122 public String getHostname() {
127 * Sets the hostname of the node.
130 * The hostname of the node
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);
141 * Returns the port number of the node.
143 * @return The port number of the node
145 public int getPort() {
150 * Sets the port number of the node.
153 * The port number of the node
155 public void setPort(int port) {
156 int oldPort = this.port;
158 if (oldPort != port) {
159 firePropertyChange("port", oldPort, port);
164 // * {@inheritDoc} Two Node objects are considered equal if their hostnames
165 // * and their port numbers are equal.
168 // public boolean equals(Object object) {
169 // if ((object == null) || !(object instanceof Node)) {
172 // Node node = (Node) object;
173 // return hostname.equals(node.hostname) && port == node.port;
180 // public int hashCode() {
181 // return hostname.hashCode() ^ (-1 - port);
188 public String toString() {
189 return name + " (" + hostname + ((port == 9481) ? ("") : (":" + port)) + ")";