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