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.text.MessageFormat;
|
---|
7 | import java.util.Collections;
|
---|
8 | import java.util.Date;
|
---|
9 | import java.util.HashMap;
|
---|
10 | import java.util.Locale;
|
---|
11 | import java.util.Map;
|
---|
12 | import java.util.Objects;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
15 | import org.openstreetmap.josm.data.osm.Node;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
18 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
19 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
20 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
21 | import org.openstreetmap.josm.data.osm.User;
|
---|
22 | import org.openstreetmap.josm.data.osm.Way;
|
---|
23 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Represents an immutable OSM primitive in the context of a historical view on
|
---|
27 | * OSM data.
|
---|
28 | *
|
---|
29 | */
|
---|
30 | public abstract class HistoryOsmPrimitive implements Comparable<HistoryOsmPrimitive> {
|
---|
31 |
|
---|
32 | private long id;
|
---|
33 | private boolean visible;
|
---|
34 | private User user;
|
---|
35 | private long changesetId;
|
---|
36 | private Changeset changeset;
|
---|
37 | private Date timestamp;
|
---|
38 | private long version;
|
---|
39 | private Map<String, String> tags;
|
---|
40 |
|
---|
41 | protected final void ensurePositiveLong(long value, String name) {
|
---|
42 | if (value <= 0) {
|
---|
43 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected. Got ''{1}''.", name, value));
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Constructs a new {@code HistoryOsmPrimitive}.
|
---|
49 | *
|
---|
50 | * @param id the id (> 0 required)
|
---|
51 | * @param version the version (> 0 required)
|
---|
52 | * @param visible whether the primitive is still visible
|
---|
53 | * @param user the user (!= null required)
|
---|
54 | * @param changesetId the changeset id (> 0 required)
|
---|
55 | * @param timestamp the timestamp (!= null required)
|
---|
56 | *
|
---|
57 | * @throws IllegalArgumentException if preconditions are violated
|
---|
58 | */
|
---|
59 | public HistoryOsmPrimitive(long id, long version, boolean visible, User user, long changesetId, Date timestamp) {
|
---|
60 | this(id, version, visible, user, changesetId, timestamp, true);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Constructs a new {@code HistoryOsmPrimitive} with a configurable checking of historic parameters.
|
---|
65 | * This is needed to build virtual HistoryOsmPrimitives for modified primitives, which do not have a timestamp and a changeset id.
|
---|
66 | *
|
---|
67 | * @param id the id (> 0 required)
|
---|
68 | * @param version the version (> 0 required)
|
---|
69 | * @param visible whether the primitive is still visible
|
---|
70 | * @param user the user (!= null required)
|
---|
71 | * @param changesetId the changeset id (> 0 required if {@code checkHistoricParams} is true)
|
---|
72 | * @param timestamp the timestamp (!= null required if {@code checkHistoricParams} is true)
|
---|
73 | * @param checkHistoricParams if true, checks values of {@code changesetId} and {@code timestamp}
|
---|
74 | *
|
---|
75 | * @throws IllegalArgumentException if preconditions are violated
|
---|
76 | * @since 5440
|
---|
77 | */
|
---|
78 | public HistoryOsmPrimitive(long id, long version, boolean visible, User user, long changesetId, Date timestamp,
|
---|
79 | boolean checkHistoricParams) {
|
---|
80 | ensurePositiveLong(id, "id");
|
---|
81 | ensurePositiveLong(version, "version");
|
---|
82 | CheckParameterUtil.ensureParameterNotNull(user, "user");
|
---|
83 | if (checkHistoricParams) {
|
---|
84 | ensurePositiveLong(changesetId, "changesetId");
|
---|
85 | CheckParameterUtil.ensureParameterNotNull(timestamp, "timestamp");
|
---|
86 | }
|
---|
87 | this.id = id;
|
---|
88 | this.version = version;
|
---|
89 | this.visible = visible;
|
---|
90 | this.user = user;
|
---|
91 | this.changesetId = changesetId;
|
---|
92 | this.timestamp = timestamp;
|
---|
93 | tags = new HashMap<>();
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Constructs a new {@code HistoryOsmPrimitive} from an existing {@link OsmPrimitive}.
|
---|
98 | * @param p the primitive
|
---|
99 | */
|
---|
100 | public HistoryOsmPrimitive(OsmPrimitive p) {
|
---|
101 | this(p.getId(), p.getVersion(), p.isVisible(), p.getUser(), p.getChangesetId(), p.getTimestamp());
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Replies a new {@link HistoryNode}, {@link HistoryWay} or {@link HistoryRelation} from an existing {@link OsmPrimitive}.
|
---|
106 | * @param p the primitive
|
---|
107 | * @return a new {@code HistoryNode}, {@code HistoryWay} or {@code HistoryRelation} from {@code p}.
|
---|
108 | */
|
---|
109 | public static HistoryOsmPrimitive forOsmPrimitive(OsmPrimitive p) {
|
---|
110 | if (p instanceof Node) {
|
---|
111 | return new HistoryNode((Node) p);
|
---|
112 | } else if (p instanceof Way) {
|
---|
113 | return new HistoryWay((Way) p);
|
---|
114 | } else if (p instanceof Relation) {
|
---|
115 | return new HistoryRelation((Relation) p);
|
---|
116 | } else {
|
---|
117 | return null;
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | public long getId() {
|
---|
122 | return id;
|
---|
123 | }
|
---|
124 |
|
---|
125 | public PrimitiveId getPrimitiveId() {
|
---|
126 | return new SimplePrimitiveId(id, getType());
|
---|
127 | }
|
---|
128 |
|
---|
129 | public boolean isVisible() {
|
---|
130 | return visible;
|
---|
131 | }
|
---|
132 |
|
---|
133 | public User getUser() {
|
---|
134 | return user;
|
---|
135 | }
|
---|
136 |
|
---|
137 | public long getChangesetId() {
|
---|
138 | return changesetId;
|
---|
139 | }
|
---|
140 |
|
---|
141 | public Date getTimestamp() {
|
---|
142 | return timestamp;
|
---|
143 | }
|
---|
144 |
|
---|
145 | public long getVersion() {
|
---|
146 | return version;
|
---|
147 | }
|
---|
148 |
|
---|
149 | public boolean matches(long id, long version) {
|
---|
150 | return this.id == id && this.version == version;
|
---|
151 | }
|
---|
152 |
|
---|
153 | public boolean matches(long id) {
|
---|
154 | return this.id == id;
|
---|
155 | }
|
---|
156 |
|
---|
157 | public abstract OsmPrimitiveType getType();
|
---|
158 |
|
---|
159 | @Override
|
---|
160 | public int compareTo(HistoryOsmPrimitive o) {
|
---|
161 | if (this.id != o.id)
|
---|
162 | throw new ClassCastException(tr("Cannot compare primitive with ID ''{0}'' to primitive with ID ''{1}''.", o.id, this.id));
|
---|
163 | return Long.compare(this.version, o.version);
|
---|
164 | }
|
---|
165 |
|
---|
166 | public void put(String key, String value) {
|
---|
167 | tags.put(key, value);
|
---|
168 | }
|
---|
169 |
|
---|
170 | public String get(String key) {
|
---|
171 | return tags.get(key);
|
---|
172 | }
|
---|
173 |
|
---|
174 | public boolean hasTag(String key) {
|
---|
175 | return tags.get(key) != null;
|
---|
176 | }
|
---|
177 |
|
---|
178 | public Map<String, String> getTags() {
|
---|
179 | return Collections.unmodifiableMap(tags);
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Returns the changeset for this history primitive.
|
---|
184 | * @return the changeset for this history primitive
|
---|
185 | */
|
---|
186 | public Changeset getChangeset() {
|
---|
187 | return changeset;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Sets the changeset for this history primitive.
|
---|
192 | * @param changeset the changeset for this history primitive
|
---|
193 | */
|
---|
194 | public void setChangeset(Changeset changeset) {
|
---|
195 | this.changeset = changeset;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Sets the tags for this history primitive. Removes all tags if <code>tags</code> is null.
|
---|
200 | *
|
---|
201 | * @param tags the tags. May be null.
|
---|
202 | */
|
---|
203 | public void setTags(Map<String, String> tags) {
|
---|
204 | if (tags == null) {
|
---|
205 | this.tags = new HashMap<>();
|
---|
206 | } else {
|
---|
207 | this.tags = new HashMap<>(tags);
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * Replies the name of this primitive. The default implementation replies the value
|
---|
213 | * of the tag <tt>name</tt> or null, if this tag is not present.
|
---|
214 | *
|
---|
215 | * @return the name of this primitive
|
---|
216 | */
|
---|
217 | public String getName() {
|
---|
218 | if (get("name") != null)
|
---|
219 | return get("name");
|
---|
220 | return null;
|
---|
221 | }
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Replies the display name of a primitive formatted by <code>formatter</code>
|
---|
225 | * @param formatter The formatter used to generate a display name
|
---|
226 | *
|
---|
227 | * @return the display name
|
---|
228 | */
|
---|
229 | public abstract String getDisplayName(HistoryNameFormatter formatter);
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Replies the a localized name for this primitive given by the value of the tags (in this order)
|
---|
233 | * <ul>
|
---|
234 | * <li>name:lang_COUNTRY_Variant of the current locale</li>
|
---|
235 | * <li>name:lang_COUNTRY of the current locale</li>
|
---|
236 | * <li>name:lang of the current locale</li>
|
---|
237 | * <li>name of the current locale</li>
|
---|
238 | * </ul>
|
---|
239 | *
|
---|
240 | * null, if no such tag exists
|
---|
241 | *
|
---|
242 | * @return the name of this primitive
|
---|
243 | */
|
---|
244 | public String getLocalName() {
|
---|
245 | String key = "name:" + Locale.getDefault();
|
---|
246 | if (get(key) != null)
|
---|
247 | return get(key);
|
---|
248 | key = "name:" + Locale.getDefault().getLanguage() + '_' + Locale.getDefault().getCountry();
|
---|
249 | if (get(key) != null)
|
---|
250 | return get(key);
|
---|
251 | key = "name:" + Locale.getDefault().getLanguage();
|
---|
252 | if (get(key) != null)
|
---|
253 | return get(key);
|
---|
254 | return getName();
|
---|
255 | }
|
---|
256 |
|
---|
257 | @Override
|
---|
258 | public int hashCode() {
|
---|
259 | return Objects.hash(id, version);
|
---|
260 | }
|
---|
261 |
|
---|
262 | @Override
|
---|
263 | public boolean equals(Object obj) {
|
---|
264 | if (this == obj) return true;
|
---|
265 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
266 | HistoryOsmPrimitive that = (HistoryOsmPrimitive) obj;
|
---|
267 | return id == that.id &&
|
---|
268 | version == that.version;
|
---|
269 | }
|
---|
270 |
|
---|
271 | @Override
|
---|
272 | public String toString() {
|
---|
273 | return getClass().getSimpleName() + " [version=" + version + ", id=" + id + ", visible=" + visible + ", "
|
---|
274 | + (timestamp != null ? "timestamp=" + timestamp : "") + ", "
|
---|
275 | + (user != null ? "user=" + user + ", " : "") + "changesetId="
|
---|
276 | + changesetId
|
---|
277 | + ']';
|
---|
278 | }
|
---|
279 | }
|
---|