move compare method to AbstractBean
[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.PropertyChangeListener;
23
24 import net.pterodactylus.util.beans.AbstractBean;
25
26 /**
27  * Container for a Freenet node. A Node is capable of notifying
28  * {@link PropertyChangeListener}s if any of the contained properties change.
29  * 
30  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
31  * @version $Id$
32  */
33 public class Node extends AbstractBean {
34
35         /** Name of the “name” property. */
36         public static final String PROPERTY_NAME = "name";
37
38         /** Name of the “hostname” property. */
39         public static final String PROPERTY_HOSTNAME = "hostname";
40
41         /** Name of the “port” property. */
42         public static final String PROPERTY_PORT = "port";
43
44         /** The name of the node. */
45         private String name;
46
47         /** The hostname of the node. */
48         private String hostname;
49
50         /** The port number of the node. */
51         private int port;
52
53         //
54         // EVENT MANAGEMENT
55         //
56
57         /**
58          * Returns the user-given name of the node.
59          * 
60          * @return The name of the node
61          */
62         public String getName() {
63                 return name;
64         }
65
66         /**
67          * Sets the user-given name of the node.
68          * 
69          * @param name
70          *            The name of the node
71          */
72         public void setName(String name) {
73                 String oldName = this.name;
74                 this.name = name;
75                 if (!equal(oldName, name)) {
76                         firePropertyChange(PROPERTY_NAME, oldName, name);
77                 }
78         }
79
80         /**
81          * Returns the hostname of the node.
82          * 
83          * @return The hostname of the node
84          */
85         public String getHostname() {
86                 return hostname;
87         }
88
89         /**
90          * Sets the hostname of the node.
91          * 
92          * @param hostname
93          *            The hostname of the node
94          */
95         public void setHostname(String hostname) {
96                 String oldHostname = this.hostname;
97                 this.hostname = hostname;
98                 if (!equal(oldHostname, hostname)) {
99                         firePropertyChange(PROPERTY_HOSTNAME, oldHostname, hostname);
100                 }
101         }
102
103         /**
104          * Returns the port number of the node.
105          * 
106          * @return The port number of the node
107          */
108         public int getPort() {
109                 return port;
110         }
111
112         /**
113          * Sets the port number of the node.
114          * 
115          * @param port
116          *            The port number of the node
117          */
118         public void setPort(int port) {
119                 int oldPort = this.port;
120                 this.port = port;
121                 if (oldPort != port) {
122                         firePropertyChange(PROPERTY_PORT, oldPort, port);
123                 }
124         }
125
126         /**
127          * {@inheritDoc}
128          */
129         @Override
130         public String toString() {
131                 return name + " (" + hostname + ((port == 9481) ? ("") : (":" + port)) + ")";
132         }
133
134 }