1 package org.thegalactic.dgraph;
2
3 /*
4 * Node.java
5 *
6 * Copyright: 2010-2015 Karell Bertet, France
7 * Copyright: 2015-2016 The Galactic Organization, France
8 *
9 * License: http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html CeCILL-B license
10 *
11 * This file is part of java-lattices.
12 * You can redistribute it and/or modify it under the terms of the CeCILL-B license.
13 */
14 /**
15 * This class gives a standard representation for a node of a graph.
16 *
17 * A node is composed of a `content` and an automatically computed and unique
18 * index.
19 *
20 * This class implements the `Comparable` interface aiming at sorting nodes by
21 * providing the {@link #compareTo} method that compares the node with those in
22 * parameter by comparing their indexes.
23 *
24 * Since nodes are comparable, they can be stored in a sorted collection, and in
25 * particular in a sorted set where set operations are provided.
26 *
27 * 
28 *
29 * @param <N> the content type
30 *
31 * @uml Node.png !include resources/org/thegalactic/dgraph/Node.iuml
32 *
33 * hide members show Node members class Node #LightCyan title Node UML graph
34 */
35 public class Node<N> implements Comparable<Node<N>>, Cloneable {
36
37 /*
38 * ------------- FIELDS ---------------------
39 */
40 /**
41 * An uniquely defined identifier for this node.
42 */
43 private int identifier;
44
45 /**
46 * An object to store information about the element.
47 */
48 private N content;
49
50 /**
51 * The total number of nodes.
52 *
53 * Initialised to 0, it is incremented by the constructor, and used to
54 * inialize the identifier.
55 */
56 private static int count;
57
58 /*
59 * ------------- CONSTRUCTORS ------------------
60 */
61 /**
62 * Constructs a new node containing the specified content.
63 *
64 * Identifier of this node is initalized with the `count` variable which is
65 * the incremented.
66 *
67 * @param content Content for this node
68 */
69 public Node(final N content) {
70 this.identifier = ++count;
71 this.content = content;
72 }
73
74 /**
75 * Constructs a new node with a null content.
76 *
77 * Identifier of this node is initalized with the `count` variable which is
78 * the incremented.
79 */
80 public Node() {
81 this(null);
82 }
83
84 /*
85 * -------------- ACCESSORS -------------------
86 */
87 /**
88 * Get the identifier.
89 *
90 * @return the node identifier
91 */
92 public final int getIdentifier() {
93 return this.identifier;
94 }
95
96 /**
97 * Get the content.
98 *
99 * @return the node content
100 */
101 public final N getContent() {
102 return this.content;
103 }
104
105 /**
106 * Set the content.
107 *
108 * @param content Content for this node
109 *
110 * @return this for chaining
111 */
112 public final Node setContent(final N content) {
113 this.content = content;
114 return this;
115 }
116
117 /**
118 * Test if this node has a content.
119 *
120 * @return true if this node has a content
121 */
122 public final boolean hasContent() {
123 return this.content != null;
124 }
125
126 /*
127 * --------------- SAVING METHOD ------------
128 */
129 /**
130 * Returns a string representation of this node without spaces.
131 *
132 * @return the string representation
133 */
134 @Override
135 public String toString() {
136 String string;
137 if (this.content == null) {
138 string = String.valueOf(this.identifier);
139 } else {
140 string = this.content.toString();
141 }
142 return string;
143 }
144
145 /*
146 * --------------- OVERRIDDEN METHODS ------------
147 */
148 /**
149 * Returns a clone of this node.
150 *
151 * @return a clone of this node
152 *
153 * @throws java.lang.CloneNotSupportedException
154 */
155 @Override
156 public Node clone() throws CloneNotSupportedException {
157 final Node node = (Node) super.clone();
158 node.identifier = ++count;
159 return node;
160 }
161
162 /**
163 * Compares this node with the specified one.
164 *
165 * @param object The object to be tested with
166 *
167 * @return true or false as this node is equal to the specified object.
168 */
169 @Override
170 public boolean equals(final Object object) {
171 return this == object || object != null && this.getClass() == object.getClass()
172 && this.identifier == ((Node) object).identifier;
173 }
174
175 /**
176 * Compute the hash code.
177 *
178 * @return an integer representing the object
179 */
180 @Override
181 public int hashCode() {
182 return this.identifier;
183 }
184
185 /**
186 * Compares this node with those in parameter, based on their identifiers.
187 *
188 * The result is zero if the identifiers are equal; positive if this node's
189 * identifier is greater, and negative otherwise. This comparison method is
190 * needed to define a natural ordering. It allows to use objects of this
191 * class in a sorted collection.
192 *
193 * @param node the specified element to be compared with this node
194 *
195 * @return a negative integer, zero, or a positive integer as this node is
196 * less than, equal to, or greater than the specified object.
197 */
198 public final int compareTo(final Node node) {
199 return this.identifier - node.identifier;
200 }
201 }