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