9a4d3be7f8d9123a900fe1f45993dbc3b98ce91d
[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.jsite.util.IdGenerator;
25 import net.pterodactylus.util.beans.AbstractBean;
26 import net.pterodactylus.util.number.Hex;
27
28 /**
29  * Container for a Freenet node. A Node is capable of notifying
30  * {@link PropertyChangeListener}s if any of the contained properties change.
31  * 
32  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
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         /** Internal ID. */
46         private String id;
47
48         /** The name of the node. */
49         private String name;
50
51         /** The hostname of the node. */
52         private String hostname;
53
54         /** The port number of the node. */
55         private int port;
56
57         /**
58          * Creates a new node.
59          */
60         public Node() {
61                 id = Hex.toHex(IdGenerator.generateId());
62         }
63
64         /**
65          * Returns the internal ID of the node.
66          * 
67          * @return The internal ID of the node
68          */
69         String getId() {
70                 return id;
71         }
72
73         /**
74          * Sets the internal ID of the node.
75          * 
76          * @param id
77          *            The internal ID of the node
78          */
79         void setId(String id) {
80                 if (id == null) {
81                         this.id = Hex.toHex(IdGenerator.generateId());
82                 } else {
83                         this.id = id;
84                 }
85         }
86
87         /**
88          * Returns the user-given name of the node.
89          * 
90          * @return The name of the node
91          */
92         public String getName() {
93                 return name;
94         }
95
96         /**
97          * Sets the user-given name of the node.
98          * 
99          * @param name
100          *            The name of the node
101          */
102         public void setName(String name) {
103                 String oldName = this.name;
104                 this.name = name;
105                 fireIfPropertyChanged(PROPERTY_NAME, oldName, name);
106         }
107
108         /**
109          * Returns the hostname of the node.
110          * 
111          * @return The hostname of the node
112          */
113         public String getHostname() {
114                 return hostname;
115         }
116
117         /**
118          * Sets the hostname of the node.
119          * 
120          * @param hostname
121          *            The hostname of the node
122          */
123         public void setHostname(String hostname) {
124                 String oldHostname = this.hostname;
125                 this.hostname = hostname;
126                 fireIfPropertyChanged(PROPERTY_HOSTNAME, oldHostname, hostname);
127         }
128
129         /**
130          * Returns the port number of the node.
131          * 
132          * @return The port number of the node
133          */
134         public int getPort() {
135                 return port;
136         }
137
138         /**
139          * Sets the port number of the node.
140          * 
141          * @param port
142          *            The port number of the node
143          */
144         public void setPort(int port) {
145                 int oldPort = this.port;
146                 this.port = port;
147                 fireIfPropertyChanged(PROPERTY_PORT, oldPort, port);
148         }
149
150         /**
151          * {@inheritDoc}
152          */
153         @Override
154         public String toString() {
155                 return getClass().getName() + "[name=" + name + ",hostname=" + hostname + ",port=" + port + "]";
156         }
157
158 }