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