1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm.history;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Collections;
|
---|
8 | import java.util.Date;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
12 | import org.openstreetmap.josm.data.osm.User;
|
---|
13 | import org.openstreetmap.josm.data.osm.Way;
|
---|
14 | import org.openstreetmap.josm.data.osm.WayData;
|
---|
15 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Represents an immutable OSM way in the context of a historical view on OSM data.
|
---|
19 | * @since 1670
|
---|
20 | */
|
---|
21 | public class HistoryWay extends HistoryOsmPrimitive {
|
---|
22 |
|
---|
23 | private final List<Long> nodeIds = new ArrayList<>();
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Constructs a new {@code HistoryWay}.
|
---|
27 | *
|
---|
28 | * @param id the id (> 0 required)
|
---|
29 | * @param version the version (> 0 required)
|
---|
30 | * @param visible whether the node is still visible
|
---|
31 | * @param user the user (!= null required)
|
---|
32 | * @param changesetId the changeset id (> 0 required if {@code checkHistoricParams} is true)
|
---|
33 | * @param timestamp the timestamp (!= null required if {@code checkHistoricParams} is true)
|
---|
34 | * @throws IllegalArgumentException if preconditions are violated
|
---|
35 | */
|
---|
36 | public HistoryWay(long id, long version, boolean visible, User user, long changesetId, Date timestamp) {
|
---|
37 | super(id, version, visible, user, changesetId, timestamp);
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Constructs a new {@code HistoryWay} with a configurable checking of historic parameters.
|
---|
42 | * This is needed to build virtual HistoryWays for modified ways, which do not have a timestamp and a changeset id.
|
---|
43 | *
|
---|
44 | * @param id the id (> 0 required)
|
---|
45 | * @param version the version (> 0 required)
|
---|
46 | * @param visible whether the node is still visible
|
---|
47 | * @param user the user (!= null required)
|
---|
48 | * @param changesetId the changeset id (> 0 required if {@code checkHistoricParams} is true)
|
---|
49 | * @param timestamp the timestamp (!= null required if {@code checkHistoricParams} is true)
|
---|
50 | * @param checkHistoricParams if true, checks values of {@code changesetId} and {@code timestamp}
|
---|
51 | * @throws IllegalArgumentException if preconditions are violated
|
---|
52 | * @since 5440
|
---|
53 | */
|
---|
54 | public HistoryWay(long id, long version, boolean visible, User user, long changesetId, Date timestamp, boolean checkHistoricParams) {
|
---|
55 | super(id, version, visible, user, changesetId, timestamp, checkHistoricParams);
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Constructs a new {@code HistoryWay} with a given list of node ids.
|
---|
60 | *
|
---|
61 | * @param id the id (> 0 required)
|
---|
62 | * @param version the version (> 0 required)
|
---|
63 | * @param visible whether the node is still visible
|
---|
64 | * @param user the user (!= null required)
|
---|
65 | * @param changesetId the changeset id (> 0 required if {@code checkHistoricParams} is true)
|
---|
66 | * @param timestamp the timestamp (!= null required if {@code checkHistoricParams} is true)
|
---|
67 | * @param nodeIdList the node ids (!= null required)
|
---|
68 | * @throws IllegalArgumentException if preconditions are violated
|
---|
69 | */
|
---|
70 | public HistoryWay(long id, long version, boolean visible, User user, long changesetId, Date timestamp, List<Long> nodeIdList) {
|
---|
71 | this(id, version, visible, user, changesetId, timestamp);
|
---|
72 | CheckParameterUtil.ensureParameterNotNull(nodeIdList, "nodeIdList");
|
---|
73 | this.nodeIds.addAll(nodeIdList);
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Constructs a new {@code HistoryWay} from an existing {@link Way}.
|
---|
78 | * @param w the way
|
---|
79 | */
|
---|
80 | public HistoryWay(Way w) {
|
---|
81 | super(w);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * replies the number of nodes in this way
|
---|
86 | * @return the number of nodes
|
---|
87 | */
|
---|
88 | public int getNumNodes() {
|
---|
89 | return nodeIds.size();
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * replies the idx-th node id in the list of node ids of this way
|
---|
94 | *
|
---|
95 | * @param idx the index
|
---|
96 | * @return the idx-th node id
|
---|
97 | * @throws IndexOutOfBoundsException if idx < 0 || idx >= {#see {@link #getNumNodes()}
|
---|
98 | */
|
---|
99 | public long getNodeId(int idx) {
|
---|
100 | if (idx < 0 || idx >= nodeIds.size())
|
---|
101 | throw new IndexOutOfBoundsException(tr("Parameter {0} not in range 0..{1}. Got ''{2}''.", "idx", nodeIds.size(), idx));
|
---|
102 | return nodeIds.get(idx);
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * replies an immutable list of the ways node ids
|
---|
107 | *
|
---|
108 | * @return the ways node ids
|
---|
109 | */
|
---|
110 | public List<Long> getNodes() {
|
---|
111 | return Collections.unmodifiableList(nodeIds);
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * replies the ways type, i.e. {@link OsmPrimitiveType#WAY}
|
---|
116 | *
|
---|
117 | * @return the ways type
|
---|
118 | */
|
---|
119 | @Override
|
---|
120 | public OsmPrimitiveType getType() {
|
---|
121 | return OsmPrimitiveType.WAY;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * adds a node id to the list nodes of this way
|
---|
126 | *
|
---|
127 | * @param ref the node id to add
|
---|
128 | */
|
---|
129 | public void addNode(long ref) {
|
---|
130 | nodeIds.add(ref);
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Replies true if this way is closed.
|
---|
135 | *
|
---|
136 | * @return true if this way is closed.
|
---|
137 | */
|
---|
138 | public boolean isClosed() {
|
---|
139 | return getNumNodes() >= 3 && nodeIds.get(0) == nodeIds.get(nodeIds.size()-1);
|
---|
140 | }
|
---|
141 |
|
---|
142 | @Override
|
---|
143 | public String getDisplayName(HistoryNameFormatter formatter) {
|
---|
144 | return formatter.format(this);
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Fills the way attributes with values from this history.
|
---|
149 | * @param data way data to fill
|
---|
150 | * @return filled way data
|
---|
151 | * @since 11878
|
---|
152 | */
|
---|
153 | public WayData fillPrimitiveData(WayData data) {
|
---|
154 | super.fillPrimitiveCommonData(data);
|
---|
155 | data.setNodes(nodeIds);
|
---|
156 | return data;
|
---|
157 | }
|
---|
158 | }
|
---|