View Javadoc
1   package org.thegalactic.dgraph;
2   
3   /*
4    * Edge.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 an edge of a graph.
16   *
17   * An edge is composed of a content, and two nodes
18   *
19   * - `source`
20   * - `target`
21   *
22   * of class {@link Node}.
23   *
24   * This class implements class `Comparable` and provides the {@link #compareTo}
25   * method that compares the edge with those in parameter by sorting indexes of
26   * the nodes that compose it.
27   *
28   * Therefore, edges can be stored in a sorted collection since they are
29   * comparable, and in particular in a sorted set where set operations are
30   * provided.
31   *
32   * ![Edge](Edge.png)
33   *
34   * @param <N>
35   * @param <E>
36   *
37   * @uml Edge.png
38   * !include resources/org/thegalactic/dgraph/Edge.iuml
39   * !include resources/org/thegalactic/dgraph/Node.iuml
40   *
41   * hide members
42   * show Edge members
43   * class Edge #LightCyan
44   * title Edge UML graph
45   */
46  public class Edge<N, E> implements Comparable<Edge<N, E>> {
47  
48      /*
49       * ------------- FIELDS ---------------------
50       */
51      /**
52       * The source node of the edge.
53       */
54      private final Node<N> source;
55  
56      /**
57       * The target node of the edge.
58       */
59      private final Node<N> target;
60  
61      /**
62       * An object target store information about this edge.
63       */
64      private E content;
65  
66      /*
67       * ------------- CONSTRUCTORS ----------------
68       */
69      /**
70       * Constructs a new edge with the specified node as origin and destination
71       * node, with the specified content.
72       *
73       * @param source  the origin node
74       * @param target  the destination node
75       * @param content the edge content
76       */
77      public Edge(final Node<N> source, final Node<N> target, final E content) {
78          this.source = source;
79          this.target = target;
80          this.content = content;
81      }
82  
83      /**
84       * Constructs a new edge with the specified node as origin and destination
85       * node, and a null value as content.
86       *
87       * @param source the origin node
88       * @param target the destination node
89       */
90      public Edge(final Node<N> source, final Node<N> target) {
91          this(source, target, null);
92      }
93  
94      /*
95       * -------------- ACCESSORS -------------------
96       */
97      /**
98       * Returns the source node of this edge.
99       *
100      * @return the source node
101      */
102     public final Node<N> getSource() {
103         return this.source;
104     }
105 
106     /**
107      * Returns the target node of this edge.
108      *
109      * @return the target node
110      */
111     public final Node<N> getTarget() {
112         return this.target;
113     }
114 
115     /**
116      * Replaces the content of this edge with the specified one.
117      *
118      * @param content The edge content
119      *
120      * @return this for chaining
121      */
122     public final Edge<N, E> setContent(final E content) {
123         this.content = content;
124         return this;
125     }
126 
127     /**
128      * Returns the content this edge.
129      *
130      * @return the content
131      */
132     public final E getContent() {
133         return this.content;
134     }
135 
136     /**
137      * Returns true if content of this edge is not the null value.
138      *
139      * @return true if the content is not null
140      */
141     public final boolean hasContent() {
142         return this.content != null;
143     }
144 
145     /*
146      * ------------- METHODS ------------------
147      */
148     /**
149      * Compares this edge with the specified one.
150      *
151      * @param object The object target be tested with
152      *
153      * @return true or false as this node is equal target the specified object.
154      */
155     @Override
156     public boolean equals(final Object object) {
157         return this == object || object != null && this.getClass() == object.getClass()
158                 && this.compareTo((Edge<N, E>) object) == 0;
159     }
160 
161     /**
162      * Compute the hash code.
163      *
164      * @return an integer representing the object
165      */
166     @Override
167     public int hashCode() {
168         return 1013 * this.source.hashCode() ^ 1009 * this.target.hashCode();
169     }
170 
171     /**
172      * Compares this edge with those in parameter, based on their identifiers.
173      *
174      * The result is zero if the identifiers are equal; positive if this edge's
175      * identifier is greater, and negative otherwise.
176      *
177      * This comparison method is needed target define a natural ordering. It
178      * allows target use objects of this class in a sorted collection
179      *
180      * @param edge the specified element target be compared with this edge
181      *
182      * @return a negative integer, zero, or a positive integer as this edge is
183      *         less than, equal target, or greater than the specified object.
184      */
185     public final int compareTo(final Edge<N, E> edge) {
186         int cmp = this.source.compareTo(edge.source);
187         if (cmp == 0) {
188             cmp = this.target.compareTo(edge.target);
189         }
190         return cmp;
191     }
192 
193     /**
194      * Returns a String representation of this edge.
195      *
196      * @return The string representation of this edge
197      */
198     @Override
199     public String toString() {
200         final StringBuilder builder = new StringBuilder();
201         builder.append('[');
202         this.appendNode(builder, this.source);
203         builder.append("]-");
204         if (this.hasContent()) {
205             builder.append('(').append(this.content).append(")-");
206         }
207         builder.append(">[");
208         this.appendNode(builder, this.target);
209         builder.append(']');
210         return builder.toString();
211     }
212 
213     /**
214      * Append the string representation of a node to the StringBuilder.
215      *
216      * @param builder StringBuilder
217      * @param node    Node
218      */
219     private void appendNode(final StringBuilder builder, final Node node) {
220         builder.append(node.toString().replaceAll("[^\\w ]", ""));
221     }
222 }