source: josm/trunk/src/org/openstreetmap/josm/data/osm/IWay.java@ 16119

Last change on this file since 16119 was 16119, checked in by Don-vip, 4 years ago

fix #18928 - fix various crashes with empty ways

  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.util.List;
5
6/**
7 * IWay captures the common functions of {@link Way} and {@link WayData}.
8 * @param <N> type of OSM node
9 * @since 4098
10 */
11public interface IWay<N extends INode> extends IPrimitive {
12
13 /**
14 * Replies the number of nodes in this way.
15 *
16 * @return the number of nodes in this way.
17 */
18 int getNodesCount();
19
20 /**
21 * Determines if this way is empty, i.e. it has no nodes.
22 * @return {@code true} if this way is empty, i.e. it has no nodes
23 * @since 16119
24 */
25 default boolean isEmpty() {
26 return getNodesCount() == 0;
27 }
28
29 /**
30 * Replies the real number of nodes in this way (full number of nodes minus one if this way is closed)
31 *
32 * @return the real number of nodes in this way.
33 *
34 * @see #getNodesCount()
35 * @see #isClosed()
36 * @since 5847
37 * @since 13564 (IWay)
38 */
39 default int getRealNodesCount() {
40 int count = getNodesCount();
41 return isClosed() ? count-1 : count;
42 }
43
44 /**
45 * Replies the node at position <code>index</code>.
46 *
47 * @param index the position
48 * @return the node at position <code>index</code>
49 * @throws ArrayIndexOutOfBoundsException if <code>index</code> &lt; 0
50 * or <code>index</code> &gt;= {@link #getNodesCount()}
51 * @since 1862
52 * @since 13717 (IWay)
53 */
54 N getNode(int index);
55
56 /**
57 * Returns the list of nodes in this way.
58 * @return the list of nodes in this way
59 * @since 1862
60 * @since 13717 (IWay)
61 */
62 List<N> getNodes();
63
64 /**
65 * Returns the list of node ids in this way.
66 * @return the list of node ids in this way
67 * @since 13717
68 */
69 List<Long> getNodeIds();
70
71 /**
72 * Returns id of the node at given index.
73 * @param idx node index
74 * @return id of the node at given index
75 */
76 long getNodeId(int idx);
77
78 /**
79 * Set new list of nodes to way. This method is preferred to multiple calls to addNode/removeNode
80 * and similar methods because nodes are internally saved as array which means lower memory overhead
81 * but also slower modifying operations.
82 * @param nodes New way nodes. Can be null, in that case all way nodes are removed
83 */
84 void setNodes(List<N> nodes);
85
86 /**
87 * Determines if this way is closed.
88 * @return {@code true} if this way is closed, {@code false} otherwise
89 */
90 boolean isClosed();
91
92 @Override
93 default int compareTo(IPrimitive o) {
94 if (o instanceof IRelation)
95 return 1;
96 return o instanceof IWay ? Long.compare(getUniqueId(), o.getUniqueId()) : -1;
97 }
98
99 @Override
100 default String getDisplayName(NameFormatter formatter) {
101 return formatter.format(this);
102 }
103
104 /**
105 * Returns the first node of this way.
106 * The result equals {@link #getNode getNode}{@code (0)}.
107 * @return the first node of this way
108 * @since 13922
109 */
110 N firstNode();
111
112 /**
113 * Returns the last node of this way.
114 * The result equals <code>{@link #getNode getNode}({@link #getNodesCount getNodesCount} - 1)</code>.
115 * @return the last node of this way
116 * @since 13922
117 */
118 N lastNode();
119
120 /**
121 * Replies true if the given node is the first or the last one of this way, false otherwise.
122 * @param n The node to test
123 * @return true if the {@code n} is the first or the last node, false otherwise.
124 * @since 13922
125 */
126 boolean isFirstLastNode(INode n);
127
128 /**
129 * Replies true if the given node is an inner node of this way, false otherwise.
130 * @param n The node to test
131 * @return true if the {@code n} is an inner node, false otherwise.
132 * @since 13922
133 */
134 boolean isInnerNode(INode n);
135}
Note: See TracBrowser for help on using the repository browser.