001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.osm; 003 004import java.util.List; 005 006/** 007 * IWay captures the common functions of {@link Way} and {@link WayData}. 008 * @param <N> type of OSM node 009 * @since 4098 010 */ 011public interface IWay<N extends INode> extends IPrimitive { 012 013 /** 014 * Replies the number of nodes in this way. 015 * 016 * @return the number of nodes in this way. 017 */ 018 int getNodesCount(); 019 020 /** 021 * Determines if this way is empty, i.e. it has no nodes. 022 * @return {@code true} if this way is empty, i.e. it has no nodes 023 * @since 16119 024 */ 025 default boolean isEmpty() { 026 return getNodesCount() == 0; 027 } 028 029 /** 030 * Replies the real number of nodes in this way (full number of nodes minus one if this way is closed) 031 * 032 * @return the real number of nodes in this way. 033 * 034 * @see #getNodesCount() 035 * @see #isClosed() 036 * @since 5847 037 * @since 13564 (IWay) 038 */ 039 default int getRealNodesCount() { 040 int count = getNodesCount(); 041 return isClosed() ? count-1 : count; 042 } 043 044 /** 045 * Replies the node at position <code>index</code>. 046 * 047 * @param index the position 048 * @return the node at position <code>index</code> 049 * @throws ArrayIndexOutOfBoundsException if <code>index</code> < 0 050 * or <code>index</code> >= {@link #getNodesCount()} 051 * @since 1862 052 * @since 13717 (IWay) 053 */ 054 N getNode(int index); 055 056 /** 057 * Returns the list of nodes in this way. 058 * @return the list of nodes in this way 059 * @since 1862 060 * @since 13717 (IWay) 061 */ 062 List<N> getNodes(); 063 064 @Override 065 default List<N> getChildren() { 066 return this.getNodes(); 067 } 068 069 /** 070 * Returns the list of node ids in this way. 071 * @return the list of node ids in this way 072 * @since 13717 073 */ 074 List<Long> getNodeIds(); 075 076 /** 077 * Returns id of the node at given index. 078 * @param idx node index 079 * @return id of the node at given index 080 */ 081 long getNodeId(int idx); 082 083 /** 084 * Set new list of nodes to way. This method is preferred to multiple calls to addNode/removeNode 085 * and similar methods because nodes are internally saved as array which means lower memory overhead 086 * but also slower modifying operations. 087 * @param nodes New way nodes. Can be null, in that case all way nodes are removed 088 */ 089 void setNodes(List<N> nodes); 090 091 /** 092 * Determines if this way is closed. 093 * @return {@code true} if this way is closed, {@code false} otherwise 094 */ 095 boolean isClosed(); 096 097 @Override 098 default int compareTo(IPrimitive o) { 099 if (o instanceof IRelation) 100 return 1; 101 return o instanceof IWay ? Long.compare(getUniqueId(), o.getUniqueId()) : -1; 102 } 103 104 @Override 105 default String getDisplayName(NameFormatter formatter) { 106 return formatter.format(this); 107 } 108 109 /** 110 * Returns the first node of this way. 111 * The result equals {@link #getNode getNode}{@code (0)}. 112 * @return the first node of this way 113 * @since 13922 114 */ 115 N firstNode(); 116 117 /** 118 * Returns the last node of this way. 119 * The result equals <code>{@link #getNode getNode}({@link #getNodesCount getNodesCount} - 1)</code>. 120 * @return the last node of this way 121 * @since 13922 122 */ 123 N lastNode(); 124 125 /** 126 * Replies true if the given node is the first or the last one of this way, false otherwise. 127 * @param n The node to test 128 * @return true if the {@code n} is the first or the last node, false otherwise. 129 * @since 13922 130 */ 131 boolean isFirstLastNode(INode n); 132 133 /** 134 * Replies true if the given node is an inner node of this way, false otherwise. 135 * @param n The node to test 136 * @return true if the {@code n} is an inner node, false otherwise. 137 * @since 13922 138 */ 139 boolean isInnerNode(INode n); 140 141 /** 142 * Replies true if this way has incomplete nodes, false otherwise. 143 * @return true if this way has incomplete nodes, false otherwise. 144 * @since 18019 145 */ 146 default boolean hasIncompleteNodes() { 147 return false; 148 } 149}