Node.java

  1. package org.thegalactic.dgraph;

  2. /*
  3.  * Node.java
  4.  *
  5.  * Copyright: 2010-2015 Karell Bertet, France
  6.  * Copyright: 2015-2016 The Galactic Organization, France
  7.  *
  8.  * License: http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html CeCILL-B license
  9.  *
  10.  * This file is part of java-lattices.
  11.  * You can redistribute it and/or modify it under the terms of the CeCILL-B license.
  12.  */
  13. /**
  14.  * This class gives a standard representation for a node of a graph.
  15.  *
  16.  * A node is composed of a `content` and an automatically computed and unique
  17.  * index.
  18.  *
  19.  * This class implements the `Comparable` interface aiming at sorting nodes by
  20.  * providing the {@link #compareTo} method that compares the node with those in
  21.  * parameter by comparing their indexes.
  22.  *
  23.  * Since nodes are comparable, they can be stored in a sorted collection, and in
  24.  * particular in a sorted set where set operations are provided.
  25.  *
  26.  * ![Node](Node.png)
  27.  *
  28.  * @param <N> the content type
  29.  *
  30.  * @uml Node.png !include resources/org/thegalactic/dgraph/Node.iuml
  31.  *
  32.  * hide members show Node members class Node #LightCyan title Node UML graph
  33.  */
  34. public class Node<N> implements Comparable<Node<N>>, Cloneable {

  35.     /*
  36.      * ------------- FIELDS ---------------------
  37.      */
  38.     /**
  39.      * An uniquely defined identifier for this node.
  40.      */
  41.     private int identifier;

  42.     /**
  43.      * An object to store information about the element.
  44.      */
  45.     private N content;

  46.     /**
  47.      * The total number of nodes.
  48.      *
  49.      * Initialised to 0, it is incremented by the constructor, and used to
  50.      * inialize the identifier.
  51.      */
  52.     private static int count;

  53.     /*
  54.      * ------------- CONSTRUCTORS ------------------
  55.      */
  56.     /**
  57.      * Constructs a new node containing the specified content.
  58.      *
  59.      * Identifier of this node is initalized with the `count` variable which is
  60.      * the incremented.
  61.      *
  62.      * @param content Content for this node
  63.      */
  64.     public Node(final N content) {
  65.         this.identifier = ++count;
  66.         this.content = content;
  67.     }

  68.     /**
  69.      * Constructs a new node with a null content.
  70.      *
  71.      * Identifier of this node is initalized with the `count` variable which is
  72.      * the incremented.
  73.      */
  74.     public Node() {
  75.         this(null);
  76.     }

  77.     /*
  78.      * -------------- ACCESSORS -------------------
  79.      */
  80.     /**
  81.      * Get the identifier.
  82.      *
  83.      * @return the node identifier
  84.      */
  85.     public final int getIdentifier() {
  86.         return this.identifier;
  87.     }

  88.     /**
  89.      * Get the content.
  90.      *
  91.      * @return the node content
  92.      */
  93.     public final N getContent() {
  94.         return this.content;
  95.     }

  96.     /**
  97.      * Set the content.
  98.      *
  99.      * @param content Content for this node
  100.      *
  101.      * @return this for chaining
  102.      */
  103.     public final Node setContent(final N content) {
  104.         this.content = content;
  105.         return this;
  106.     }

  107.     /**
  108.      * Test if this node has a content.
  109.      *
  110.      * @return true if this node has a content
  111.      */
  112.     public final boolean hasContent() {
  113.         return this.content != null;
  114.     }

  115.     /*
  116.      * --------------- SAVING METHOD ------------
  117.      */
  118.     /**
  119.      * Returns a string representation of this node without spaces.
  120.      *
  121.      * @return the string representation
  122.      */
  123.     @Override
  124.     public String toString() {
  125.         String string;
  126.         if (this.content == null) {
  127.             string = String.valueOf(this.identifier);
  128.         } else {
  129.             string = this.content.toString();
  130.         }
  131.         return string;
  132.     }

  133.     /*
  134.      * --------------- OVERRIDDEN METHODS ------------
  135.      */
  136.     /**
  137.      * Returns a clone of this node.
  138.      *
  139.      * @return a clone of this node
  140.      *
  141.      * @throws java.lang.CloneNotSupportedException
  142.      */
  143.     @Override
  144.     public Node clone() throws CloneNotSupportedException {
  145.         final Node node = (Node) super.clone();
  146.         node.identifier = ++count;
  147.         return node;
  148.     }

  149.     /**
  150.      * Compares this node with the specified one.
  151.      *
  152.      * @param object The object to be tested with
  153.      *
  154.      * @return true or false as this node is equal to the specified object.
  155.      */
  156.     @Override
  157.     public boolean equals(final Object object) {
  158.         return this == object || object != null && this.getClass() == object.getClass()
  159.                 && this.identifier == ((Node) object).identifier;
  160.     }

  161.     /**
  162.      * Compute the hash code.
  163.      *
  164.      * @return an integer representing the object
  165.      */
  166.     @Override
  167.     public int hashCode() {
  168.         return this.identifier;
  169.     }

  170.     /**
  171.      * Compares this node with those in parameter, based on their identifiers.
  172.      *
  173.      * The result is zero if the identifiers are equal; positive if this node's
  174.      * identifier is greater, and negative otherwise. This comparison method is
  175.      * needed to define a natural ordering. It allows to use objects of this
  176.      * class in a sorted collection.
  177.      *
  178.      * @param node the specified element to be compared with this node
  179.      *
  180.      * @return a negative integer, zero, or a positive integer as this node is
  181.      *         less than, equal to, or greater than the specified object.
  182.      */
  183.     public final int compareTo(final Node node) {
  184.         return this.identifier - node.identifier;
  185.     }
  186. }