source: josm/src/org/openstreetmap/josm/data/osm/LineSegment.java@ 8

Last change on this file since 8 was 8, checked in by imi, 19 years ago
  • added Selection Dialog
  • added support for graphic engines with a better default engine
  • reorganized data classes with back references
File size: 2.3 KB
Line 
1package org.openstreetmap.josm.data.osm;
2
3import java.util.Collection;
4import java.util.Collections;
5import java.util.LinkedList;
6
7import org.openstreetmap.josm.data.osm.visitor.Visitor;
8
9
10/**
11 * One track line segment consisting of a pair of nodes (start/end)
12 *
13 * @author imi
14 */
15public class LineSegment extends OsmPrimitive {
16
17 /**
18 * The starting node of the line segment
19 */
20 private Node start;
21
22 /**
23 * The ending node of the line segment
24 */
25 private Node end;
26
27 /**
28 * The tracks, this line segment is part of.
29 */
30 transient Collection<Track> parent = new LinkedList<Track>();
31
32 /**
33 * Create an line segment from the given starting and ending node
34 * @param start Starting node of the line segment.
35 * @param end Ending node of the line segment.
36 */
37 public LineSegment(Node start, Node end) {
38 this.start = start;
39 this.end = end;
40 start.parentSegment.add(this);
41 end.parentSegment.add(this);
42 }
43
44 /**
45 * Return all parent tracks this line segment is part of. The list is readonly.
46 */
47 public Collection<Track> getParents() {
48 return Collections.unmodifiableCollection(parent);
49 }
50
51 public void setStart(Node start) {
52 this.start.parentSegment.remove(this);
53 this.start = start;
54 start.parentSegment.add(this);
55 }
56 public Node getStart() {
57 return start;
58 }
59 public void setEnd(Node end) {
60 this.end.parentSegment.remove(this);
61 this.end = end;
62 end.parentSegment.add(this);
63 }
64 public Node getEnd() {
65 return end;
66 }
67
68 /**
69 * The LineSegment is going to be destroyed. Unlink all back references.
70 */
71 void destroy() {
72 start.parentSegment.remove(this);
73 end.parentSegment.remove(this);
74 }
75
76 /**
77 * Return start and end in a list.
78 */
79 @Override
80 public Collection<Node> getAllNodes() {
81 LinkedList<Node> nodes = new LinkedList<Node>();
82 nodes.add(getStart());
83 nodes.add(getEnd());
84 return nodes;
85 }
86
87 /**
88 * Line segments are equal, if their starting and ending node and their
89 * keys are equal.
90 */
91 @Override
92 public boolean equals(Object obj) {
93 if (!(obj instanceof LineSegment))
94 return false;
95 return super.equals(obj) &&
96 getStart().equals(((LineSegment)obj).getStart()) &&
97 getEnd().equals(((LineSegment)obj).getEnd());
98 }
99
100 @Override
101 public int hashCode() {
102 return super.hashCode() + getStart().hashCode() + getEnd().hashCode();
103 }
104
105 @Override
106 public void visit(Visitor visitor) {
107 visitor.visit(this);
108 }
109}
Note: See TracBrowser for help on using the repository browser.