df7492f3c99864516f620ea18dbd8f1ade5b85c5
[jSite.git] / src / main / java / de / todesbaum / jsite / application / Node.java
1 /*
2  * jSite - Node.java - Copyright © 2006–2012 David Roden
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 package de.todesbaum.jsite.application;
20
21 /**
22  * Container for node information.
23  *
24  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
25  */
26 public class Node extends de.todesbaum.util.freenet.fcp2.Node {
27
28         /** The name of the node. */
29         protected String name;
30
31         /**
32          * Creates a new node with the given hostname and the default port.
33          *
34          * @see de.todesbaum.util.freenet.fcp2.Node#DEFAULT_PORT
35          * @param hostname
36          *            The hostname of the new node
37          */
38         public Node(String hostname) {
39                 this(hostname, DEFAULT_PORT);
40         }
41
42         /**
43          * Creates a new node with the given hostname and port.
44          *
45          * @param hostname
46          *            The hostname of the new node
47          * @param port
48          *            The port of the new node
49          */
50         public Node(String hostname, int port) {
51                 this(hostname, port, "");
52         }
53
54         /**
55          * Creates a new node with the given hostname, port, and name.
56          *
57          * @param hostname
58          *            The hostname of the new node
59          * @param port
60          *            The port of the new node
61          * @param name
62          *            The name of the node
63          */
64         public Node(String hostname, int port, String name) {
65                 super(hostname, port);
66                 this.name = name;
67         }
68
69         /**
70          * Creates a new node that gets its settings from the given node.
71          *
72          * @param node
73          *            The node to copy
74          */
75         public Node(Node node) {
76                 this(node.getHostname(), node.getPort());
77         }
78
79         /**
80          * Creates a new node from the given node, overwriting the name.
81          *
82          * @param node
83          *            The node to copy from
84          * @param name
85          *            The new name of the node
86          */
87         public Node(Node node, String name) {
88                 this(node.getHostname(), node.getPort(), name);
89         }
90
91         /**
92          * Sets the name of the node.
93          *
94          * @param name
95          *            The name of the node
96          */
97         public void setName(String name) {
98                 this.name = name;
99         }
100
101         /**
102          * Returns the name of the node.
103          *
104          * @return The name of the node
105          */
106         public String getName() {
107                 return name;
108         }
109
110         /**
111          * Sets the hostname of the node.
112          *
113          * @param hostname
114          *            The hostname of the node
115          */
116         public void setHostname(String hostname) {
117                 this.hostname = hostname;
118         }
119
120         /**
121          * Sets the port of the node.
122          *
123          * @param port
124          *            The port of the node
125          */
126         public void setPort(int port) {
127                 this.port = port;
128         }
129
130         /**
131          * {@inheritDoc}
132          * <p>
133          * A node is considered as being equal to this node its name, hostname, and
134          * port equal their counterparts in this node.
135          */
136         @Override
137         public boolean equals(Object o) {
138                 if ((o == null) || !(o instanceof Node)) {
139                         return false;
140                 }
141                 Node node = (Node) o;
142                 return name.equals(node.name) && hostname.equals(node.hostname) && (port == node.port);
143         }
144
145         /**
146          * {@inheritDoc}
147          * <p>
148          * The hashcode for a node is created from its name, its hostname, and its
149          * port.
150          */
151         @Override
152         public int hashCode() {
153                 return name.hashCode() ^ hostname.hashCode() ^ port;
154         }
155
156         /**
157          * {@inheritDoc}
158          * <p>
159          * Creates a textual representation of this node.
160          */
161         @Override
162         public String toString() {
163                 return name + " (" + hostname + ":" + port + ")";
164         }
165
166 }