source: josm/trunk/src/org/openstreetmap/josm/data/osm/history/HistoryOsmPrimitive.java@ 6296

Last change on this file since 6296 was 6296, checked in by Don-vip, 11 years ago

Sonar/Findbugs - Avoid commented-out lines of code, javadoc

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