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.Collections;
|
---|
7 | import java.util.Date;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.Locale;
|
---|
10 | import java.util.Map;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
13 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
14 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Represents an immutable OSM primitive in the context of a historical view on
|
---|
18 | * OSM data.
|
---|
19 | *
|
---|
20 | */
|
---|
21 | public abstract class HistoryOsmPrimitive implements Comparable<HistoryOsmPrimitive> {
|
---|
22 |
|
---|
23 | private long id;
|
---|
24 | private boolean visible;
|
---|
25 | private String user;
|
---|
26 | private long uid;
|
---|
27 | private long changesetId;
|
---|
28 | private Date timestamp;
|
---|
29 | private long version;
|
---|
30 | private HashMap<String, String> tags;
|
---|
31 |
|
---|
32 | protected void ensurePositiveLong(long value, String name) {
|
---|
33 | if (value <= 0)
|
---|
34 | throw new IllegalArgumentException(tr("Parameter ''{0}'' > 0 expected. Got ''{1}''.", name, value));
|
---|
35 | }
|
---|
36 |
|
---|
37 | protected void ensureNotNull(Object obj, String name) {
|
---|
38 | if (obj == null)
|
---|
39 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", name));
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * constructor
|
---|
44 | *
|
---|
45 | * @param id the id (>0 required)
|
---|
46 | * @param version the version (> 0 required)
|
---|
47 | * @param visible whether the primitive is still visible
|
---|
48 | * @param user the user (! null required)
|
---|
49 | * @param uid the user id (> 0 required)
|
---|
50 | * @param changesetId the changeset id (may be null if the changeset isn't known)
|
---|
51 | * @param timestamp the timestamp (! null required)
|
---|
52 | *
|
---|
53 | * @throws IllegalArgumentException thrown if preconditions are violated
|
---|
54 | */
|
---|
55 | public HistoryOsmPrimitive(long id, long version, boolean visible, String user, long uid, long changesetId, Date timestamp) throws IllegalArgumentException {
|
---|
56 | ensurePositiveLong(id, "id");
|
---|
57 | ensurePositiveLong(version, "version");
|
---|
58 | if(uid != -1) {
|
---|
59 | ensurePositiveLong(uid, "uid");
|
---|
60 | }
|
---|
61 | ensureNotNull(user, "user");
|
---|
62 | ensureNotNull(timestamp, "timestamp");
|
---|
63 | this.id = id;
|
---|
64 | this.version = version;
|
---|
65 | this.visible = visible;
|
---|
66 | this.user = user;
|
---|
67 | this.uid = uid;
|
---|
68 | // FIXME: restrict to IDs > 0 as soon as OsmPrimitive holds the
|
---|
69 | // changeset id too
|
---|
70 | this.changesetId = changesetId;
|
---|
71 | this.timestamp = timestamp;
|
---|
72 | tags = new HashMap<String, String>();
|
---|
73 | }
|
---|
74 |
|
---|
75 | public long getId() {
|
---|
76 | return id;
|
---|
77 | }
|
---|
78 |
|
---|
79 | public PrimitiveId getPrimitiveId() {
|
---|
80 | return new SimplePrimitiveId(id, getType());
|
---|
81 | }
|
---|
82 |
|
---|
83 | public boolean isVisible() {
|
---|
84 | return visible;
|
---|
85 | }
|
---|
86 | public String getUser() {
|
---|
87 | return user;
|
---|
88 | }
|
---|
89 | public long getUid() {
|
---|
90 | return uid;
|
---|
91 | }
|
---|
92 | public long getChangesetId() {
|
---|
93 | return changesetId;
|
---|
94 | }
|
---|
95 | public Date getTimestamp() {
|
---|
96 | return timestamp;
|
---|
97 | }
|
---|
98 |
|
---|
99 | public long getVersion() {
|
---|
100 | return version;
|
---|
101 | }
|
---|
102 |
|
---|
103 | public boolean matches(long id, long version) {
|
---|
104 | return this.id == id && this.version == version;
|
---|
105 | }
|
---|
106 |
|
---|
107 | public boolean matches(long id) {
|
---|
108 | return this.id == id;
|
---|
109 | }
|
---|
110 |
|
---|
111 | public abstract OsmPrimitiveType getType();
|
---|
112 |
|
---|
113 | public int compareTo(HistoryOsmPrimitive o) {
|
---|
114 | if (this.id != o.id)
|
---|
115 | throw new ClassCastException(tr("Can''t compare primitive with ID ''{0}'' to primitive with ID ''{1}''.", o.id, this.id));
|
---|
116 | return Long.valueOf(this.version).compareTo(o.version);
|
---|
117 | }
|
---|
118 |
|
---|
119 | public void put(String key, String value) {
|
---|
120 | tags.put(key, value);
|
---|
121 | }
|
---|
122 |
|
---|
123 | public String get(String key) {
|
---|
124 | return tags.get(key);
|
---|
125 | }
|
---|
126 |
|
---|
127 | public boolean hasTag(String key) {
|
---|
128 | return tags.get(key) != null;
|
---|
129 | }
|
---|
130 |
|
---|
131 | public Map<String,String> getTags() {
|
---|
132 | return Collections.unmodifiableMap(tags);
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Sets the tags for this history primitive. Removes all
|
---|
137 | * tags if <code>tags</code> is null.
|
---|
138 | *
|
---|
139 | * @param tags the tags. May be null.
|
---|
140 | */
|
---|
141 | public void setTags(Map<String,String> tags) {
|
---|
142 | if (tags == null) {
|
---|
143 | this.tags = new HashMap<String, String>();
|
---|
144 | } else {
|
---|
145 | this.tags = new HashMap<String, String>(tags);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Replies the name of this primitive. The default implementation replies the value
|
---|
151 | * of the tag <tt>name</tt> or null, if this tag is not present.
|
---|
152 | *
|
---|
153 | * @return the name of this primitive
|
---|
154 | */
|
---|
155 | public String getName() {
|
---|
156 | if (get("name") != null)
|
---|
157 | return get("name");
|
---|
158 | return null;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Replies the display name of a primitive formatted by <code>formatter</code>
|
---|
163 | *
|
---|
164 | * @return the display name
|
---|
165 | */
|
---|
166 | public abstract String getDisplayName(HistoryNameFormatter formatter);
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Replies the a localized name for this primitive given by the value of the tags (in this order)
|
---|
170 | * <ul>
|
---|
171 | * <li>name:lang_COUNTRY_Variant of the current locale</li>
|
---|
172 | * <li>name:lang_COUNTRY of the current locale</li>
|
---|
173 | * <li>name:lang of the current locale</li>
|
---|
174 | * <li>name of the current locale</li>
|
---|
175 | * </ul>
|
---|
176 | *
|
---|
177 | * null, if no such tag exists
|
---|
178 | *
|
---|
179 | * @return the name of this primitive
|
---|
180 | */
|
---|
181 | public String getLocalName() {
|
---|
182 | String key = "name:" + Locale.getDefault().toString();
|
---|
183 | if (get(key) != null)
|
---|
184 | return get(key);
|
---|
185 | key = "name:" + Locale.getDefault().getLanguage() + "_" + Locale.getDefault().getCountry();
|
---|
186 | if (get(key) != null)
|
---|
187 | return get(key);
|
---|
188 | key = "name:" + Locale.getDefault().getLanguage();
|
---|
189 | if (get(key) != null)
|
---|
190 | return get(key);
|
---|
191 | return getName();
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 |
|
---|
196 | @Override
|
---|
197 | public int hashCode() {
|
---|
198 | final int prime = 31;
|
---|
199 | int result = 1;
|
---|
200 | result = prime * result + (int) (id ^ (id >>> 32));
|
---|
201 | result = prime * result + (int) (version ^ (version >>> 32));
|
---|
202 | return result;
|
---|
203 | }
|
---|
204 |
|
---|
205 | @Override
|
---|
206 | public boolean equals(Object obj) {
|
---|
207 | if (this == obj)
|
---|
208 | return true;
|
---|
209 | if (!(obj instanceof HistoryOsmPrimitive))
|
---|
210 | return false;
|
---|
211 | // equal semantics is valid for subclasses like {@see HistoryOsmNode} etc. too.
|
---|
212 | // So, don't enforce equality of class.
|
---|
213 | //
|
---|
214 | // if (getClass() != obj.getClass())
|
---|
215 | // return false;
|
---|
216 | HistoryOsmPrimitive other = (HistoryOsmPrimitive) obj;
|
---|
217 | if (id != other.id)
|
---|
218 | return false;
|
---|
219 | if (version != other.version)
|
---|
220 | return false;
|
---|
221 | return true;
|
---|
222 | }
|
---|
223 | }
|
---|