No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Node.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package org.insa.graph;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.List;
  5. /**
  6. * Class representing a Node in a {@link Graph}.
  7. *
  8. * This class holds information regarding nodes in the graph together with the
  9. * successors associated to the nodes.
  10. *
  11. * Nodes are comparable based on their ID.
  12. *
  13. */
  14. public final class Node implements Comparable<Node> {
  15. /**
  16. * Link the two given nodes with one or two arcs (depending on roadInformation),
  17. * with the given attributes.
  18. *
  19. * If `roadInformation.isOneWay()` is true, only a forward arc is created
  20. * (origin to destination) and added to origin. Otherwise, a corresponding
  21. * backward arc is created and add to destination.
  22. *
  23. * @param origin Origin of the arc.
  24. * @param destination Destination of the arc.
  25. * @param length Length of the arc.
  26. * @param roadInformation Information corresponding to the arc.
  27. * @param points Points for the arc.
  28. *
  29. * @return The newly created forward arc (origin to destination).
  30. */
  31. public static Arc linkNodes(Node origin, Node destination, float length,
  32. RoadInformation roadInformation, ArrayList<Point> points) {
  33. Arc arc = null;
  34. if (roadInformation.isOneWay()) {
  35. arc = new ArcForward(origin, destination, length, roadInformation, points);
  36. origin.addSuccessor(arc);
  37. }
  38. else {
  39. Arc d2o;
  40. if (origin.getId() < destination.getId()) {
  41. arc = new ArcForward(origin, destination, length, roadInformation, points);
  42. d2o = new ArcBackward(arc);
  43. }
  44. else {
  45. Collections.reverse(points);
  46. d2o = new ArcForward(destination, origin, length, roadInformation, points);
  47. arc = new ArcBackward(d2o);
  48. }
  49. origin.addSuccessor(arc);
  50. destination.addSuccessor(d2o);
  51. }
  52. return arc;
  53. }
  54. // ID of the node.
  55. private final int id;
  56. // Point of this graph.
  57. private final Point point;
  58. // Successors.
  59. private final ArrayList<Arc> successors;
  60. /**
  61. * Create a new Node with the given ID corresponding to the given Point with an
  62. * empty list of successors.
  63. *
  64. * @param id ID of the node.
  65. * @param point Position of the node.
  66. */
  67. public Node(int id, Point point) {
  68. this.id = id;
  69. this.point = point;
  70. this.successors = new ArrayList<Arc>();
  71. }
  72. /**
  73. * Add a successor to this node.
  74. *
  75. * @param arc Arc to the successor.
  76. */
  77. protected void addSuccessor(Arc arc) {
  78. successors.add(arc);
  79. }
  80. /**
  81. * @return ID of this node.
  82. */
  83. public int getId() {
  84. return id;
  85. }
  86. /**
  87. * @return Number of successors of this node.
  88. */
  89. public int getNumberOfSuccessors() {
  90. return this.successors.size();
  91. }
  92. /**
  93. * @return true if this node has at least one successor.
  94. */
  95. public boolean hasSuccessors() {
  96. return !this.successors.isEmpty();
  97. }
  98. /**
  99. * @return List of successors of this node (unmodifiable list).
  100. *
  101. * @see Collections#unmodifiableList(List)
  102. */
  103. public List<Arc> getSuccessors() {
  104. return Collections.unmodifiableList(this.successors);
  105. }
  106. /**
  107. * @return Location of this node.
  108. */
  109. public Point getPoint() {
  110. return point;
  111. }
  112. @Override
  113. public boolean equals(Object other) {
  114. if (other == null) {
  115. return false;
  116. }
  117. if (other instanceof Node) {
  118. return getId() == ((Node) other).getId();
  119. }
  120. return false;
  121. }
  122. /**
  123. * Compare the ID of this node with the ID of the given node.
  124. *
  125. * @param other Node to compare this node with.
  126. *
  127. * @see java.lang.Comparable#compareTo(java.lang.Object)
  128. */
  129. @Override
  130. public int compareTo(Node other) {
  131. return Integer.compare(getId(), other.getId());
  132. }
  133. }