source: josm/trunk/src/org/openstreetmap/josm/data/osm/Way.java@ 1997

Last change on this file since 1997 was 1997, checked in by Gubaer, 15 years ago

fixed #3328: show note= again at relations

  • Property svn:eol-style set to native
File size: 6.5 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.data.osm;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.util.ArrayList;
8import java.util.Arrays;
9import java.util.Collection;
10import java.util.HashSet;
11import java.util.List;
12
13import org.openstreetmap.josm.data.osm.visitor.Visitor;
14import org.openstreetmap.josm.tools.CopyList;
15import org.openstreetmap.josm.tools.Pair;
16
17/**
18 * One full way, consisting of a list of way nodes.
19 *
20 * @author imi
21 */
22public final class Way extends OsmPrimitive {
23
24 /**
25 * All way nodes in this way
26 *
27 * @deprecated This public field will become private or protected in the future.
28 * Use the new public API instead.
29 */
30 @Deprecated
31 public final List<Node> nodes = new ArrayList<Node>();
32
33 /**
34 *
35 * You can modify returned list but changes will not be propagated back
36 * to the Way. Use {@link #setNodes(List)} to update this way
37 * @return Nodes composing the way
38 * @since 1862
39 */
40 public List<Node> getNodes() {
41 return new CopyList<Node>(nodes.toArray(new Node[nodes.size()]));
42 }
43
44 /**
45 * @param nodes New way nodes. Can be null, in that case all way nodes are removed
46 * @since 1862
47 */
48 public void setNodes(List<Node> nodes) {
49 this.nodes.clear();
50 if (nodes != null) {
51 this.nodes.addAll(nodes);
52 }
53 clearCached();
54 }
55
56 /**
57 * Replies the number of nodes in this ways.
58 *
59 * @return the number of nodes in this ways.
60 * @since 1862
61 */
62 public int getNodesCount() {
63 return nodes.size();
64 }
65
66 /**
67 * Replies the node at position <code>index</code>.
68 *
69 * @param index the position
70 * @return the node at position <code>index</code>
71 * @exception IndexOutOfBoundsException thrown if <code>index</code> < 0
72 * or <code>index</code> >= {@see #getNodesCount()}
73 * @since 1862
74 */
75 public Node getNode(int index) {
76 return nodes.get(index);
77 }
78
79 /**
80 * Replies true if this way contains the node <code>node</code>, false
81 * otherwise. Replies false if <code>node</code> is null.
82 *
83 * @param node the node. May be null.
84 * @return true if this way contains the node <code>node</code>, false
85 * otherwise
86 * @since 1909
87 */
88 public boolean containsNode(Node node) {
89 if (node == null) return false;
90 return nodes.contains(node);
91 }
92
93 /* mappaint data */
94 public boolean isMappaintArea = false;
95 public Integer mappaintDrawnAreaCode = 0;
96 /* end of mappaint data */
97 @Override protected void clearCached() {
98 super.clearCached();
99 isMappaintArea = false;
100 mappaintDrawnAreaCode = 0;
101 }
102
103 public ArrayList<Pair<Node,Node>> getNodePairs(boolean sort) {
104 ArrayList<Pair<Node,Node>> chunkSet = new ArrayList<Pair<Node,Node>>();
105 if (incomplete) return chunkSet;
106 Node lastN = null;
107 for (Node n : this.nodes) {
108 if (lastN == null) {
109 lastN = n;
110 continue;
111 }
112 Pair<Node,Node> np = new Pair<Node,Node>(lastN, n);
113 if (sort) {
114 Pair.sort(np);
115 }
116 chunkSet.add(np);
117 lastN = n;
118 }
119 return chunkSet;
120 }
121
122
123 @Override public void visit(Visitor visitor) {
124 visitor.visit(this);
125 }
126
127 /**
128 * Create an identical clone of the argument (including the id).
129 *
130 * @param original the original way. Must not be null.
131 */
132 public Way(Way original) {
133 cloneFrom(original);
134 }
135
136 /**
137 * Create an empty way without id. Use this only if you set meaningful
138 * values yourself.
139 */
140 public Way() {
141 }
142
143 /**
144 * Create an incomplete Way with a given id.
145 *
146 * @param id the id. id > 0 required.
147 */
148 public Way(long id) {
149 // FIXME: shouldn't we check for id > 0?
150 //
151 this.id = id;
152 incomplete = true;
153 }
154
155 @Override public void cloneFrom(OsmPrimitive osm) {
156 super.cloneFrom(osm);
157 nodes.clear();
158 nodes.addAll(((Way)osm).nodes);
159 }
160
161 @Override public String toString() {
162 if (incomplete) return "{Way id="+id+" version="+version+" (incomplete)}";
163 return "{Way id="+id+" version="+version+" nodes="+Arrays.toString(nodes.toArray())+"}";
164 }
165
166 @Override
167 public boolean hasEqualSemanticAttributes(OsmPrimitive other) {
168 if (other == null || ! (other instanceof Way) )
169 return false;
170 if (! super.hasEqualSemanticAttributes(other))
171 return false;
172 Way w = (Way)other;
173 return nodes.equals(w.nodes);
174 }
175
176 public int compareTo(OsmPrimitive o) {
177 if (o instanceof Relation)
178 return 1;
179 return o instanceof Way ? Long.valueOf(id).compareTo(o.id) : -1;
180 }
181
182 public void removeNode(Node n) {
183 if (incomplete) return;
184 boolean closed = (lastNode() == n && firstNode() == n);
185 int i;
186 while ((i = nodes.indexOf(n)) >= 0) {
187 nodes.remove(i);
188 }
189 i = nodes.size();
190 if (closed && i > 2) {
191 addNode(firstNode());
192 } else if (i >= 2 && i <= 3 && nodes.get(0) == nodes.get(i-1)) {
193 nodes.remove(i-1);
194 }
195 }
196
197 public void removeNodes(Collection<? extends OsmPrimitive> selection) {
198 if (incomplete) return;
199 for(OsmPrimitive p : selection) {
200 if (p instanceof Node) {
201 removeNode((Node)p);
202 }
203 }
204 }
205
206 public void addNode(Node n) {
207 if (incomplete) return;
208 clearCached();
209 nodes.add(n);
210 }
211
212 public void addNode(int offs, Node n) {
213 if (incomplete) return;
214 clearCached();
215 nodes.add(offs, n);
216 }
217
218 public boolean isClosed() {
219 if (incomplete) return false;
220 return nodes.size() >= 3 && lastNode() == firstNode();
221 }
222
223 public Node lastNode() {
224 if (incomplete || nodes.size() == 0) return null;
225 return nodes.get(nodes.size()-1);
226 }
227
228 public Node firstNode() {
229 if (incomplete || nodes.size() == 0) return null;
230 return nodes.get(0);
231 }
232
233 public boolean isFirstLastNode(Node n) {
234 if (incomplete || nodes.size() == 0) return false;
235 return n == firstNode() || n == lastNode();
236 }
237
238 @Override
239 public String getDisplayName(NameFormatter formatter) {
240 return formatter.format(this);
241 }
242}
Note: See TracBrowser for help on using the repository browser.