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

Last change on this file since 4602 was 4602, checked in by bastiK, 12 years ago

upgrade historic users to real users, so their CT status can be shown in the history panel

  • Property svn:eol-style set to native
File size: 7.1 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 * constructor
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 uid the user id (> 0 required)
51 * @param changesetId the changeset id (may be null if the changeset isn't known)
52 * @param timestamp the timestamp (! null required)
53 *
54 * @throws IllegalArgumentException thrown if preconditions are violated
55 */
56 public HistoryOsmPrimitive(long id, long version, boolean visible, User user, long changesetId, Date timestamp) throws IllegalArgumentException {
57 ensurePositiveLong(id, "id");
58 ensurePositiveLong(version, "version");
59 CheckParameterUtil.ensureParameterNotNull(user, "user");
60 CheckParameterUtil.ensureParameterNotNull(timestamp, "timestamp");
61 this.id = id;
62 this.version = version;
63 this.visible = visible;
64 this.user = user;
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 HistoryOsmPrimitive(OsmPrimitive p) {
73 this(p.getId(), p.getVersion(), p.isVisible(),
74 p.getUser(),
75 p.getChangesetId(), p.getTimestamp());
76 }
77
78 public static HistoryOsmPrimitive forOsmPrimitive(OsmPrimitive p) {
79 if (p instanceof Node) {
80 return new HistoryNode((Node) p);
81 } else if (p instanceof Way) {
82 return new HistoryWay((Way) p);
83 } else if (p instanceof Relation) {
84 return new HistoryRelation((Relation) p);
85 } else {
86 return null;
87 }
88 }
89
90 public long getId() {
91 return id;
92 }
93
94 public PrimitiveId getPrimitiveId() {
95 return new SimplePrimitiveId(id, getType());
96 }
97
98 public boolean isVisible() {
99 return visible;
100 }
101 public User getUser() {
102 return user;
103 }
104 public long getChangesetId() {
105 return changesetId;
106 }
107 public Date getTimestamp() {
108 return timestamp;
109 }
110
111 public long getVersion() {
112 return version;
113 }
114
115 public boolean matches(long id, long version) {
116 return this.id == id && this.version == version;
117 }
118
119 public boolean matches(long id) {
120 return this.id == id;
121 }
122
123 public abstract OsmPrimitiveType getType();
124
125 public int compareTo(HistoryOsmPrimitive o) {
126 if (this.id != o.id)
127 throw new ClassCastException(tr("Cannot compare primitive with ID ''{0}'' to primitive with ID ''{1}''.", o.id, this.id));
128 return Long.valueOf(this.version).compareTo(o.version);
129 }
130
131 public void put(String key, String value) {
132 tags.put(key, value);
133 }
134
135 public String get(String key) {
136 return tags.get(key);
137 }
138
139 public boolean hasTag(String key) {
140 return tags.get(key) != null;
141 }
142
143 public Map<String,String> getTags() {
144 return Collections.unmodifiableMap(tags);
145 }
146
147 /**
148 * Sets the tags for this history primitive. Removes all
149 * tags if <code>tags</code> is null.
150 *
151 * @param tags the tags. May be null.
152 */
153 public void setTags(Map<String,String> tags) {
154 if (tags == null) {
155 this.tags = new HashMap<String, String>();
156 } else {
157 this.tags = new HashMap<String, String>(tags);
158 }
159 }
160
161 /**
162 * Replies the name of this primitive. The default implementation replies the value
163 * of the tag <tt>name</tt> or null, if this tag is not present.
164 *
165 * @return the name of this primitive
166 */
167 public String getName() {
168 if (get("name") != null)
169 return get("name");
170 return null;
171 }
172
173 /**
174 * Replies the display name of a primitive formatted by <code>formatter</code>
175 *
176 * @return the display name
177 */
178 public abstract String getDisplayName(HistoryNameFormatter formatter);
179
180 /**
181 * Replies the a localized name for this primitive given by the value of the tags (in this order)
182 * <ul>
183 * <li>name:lang_COUNTRY_Variant of the current locale</li>
184 * <li>name:lang_COUNTRY of the current locale</li>
185 * <li>name:lang of the current locale</li>
186 * <li>name of the current locale</li>
187 * </ul>
188 *
189 * null, if no such tag exists
190 *
191 * @return the name of this primitive
192 */
193 public String getLocalName() {
194 String key = "name:" + Locale.getDefault().toString();
195 if (get(key) != null)
196 return get(key);
197 key = "name:" + Locale.getDefault().getLanguage() + "_" + Locale.getDefault().getCountry();
198 if (get(key) != null)
199 return get(key);
200 key = "name:" + Locale.getDefault().getLanguage();
201 if (get(key) != null)
202 return get(key);
203 return getName();
204 }
205
206 @Override
207 public int hashCode() {
208 final int prime = 31;
209 int result = 1;
210 result = prime * result + (int) (id ^ (id >>> 32));
211 result = prime * result + (int) (version ^ (version >>> 32));
212 return result;
213 }
214
215 @Override
216 public boolean equals(Object obj) {
217 if (this == obj)
218 return true;
219 if (!(obj instanceof HistoryOsmPrimitive))
220 return false;
221 // equal semantics is valid for subclasses like {@see HistoryOsmNode} etc. too.
222 // So, don't enforce equality of class.
223 //
224 // if (getClass() != obj.getClass())
225 // return false;
226 HistoryOsmPrimitive other = (HistoryOsmPrimitive) obj;
227 if (id != other.id)
228 return false;
229 if (version != other.version)
230 return false;
231 return true;
232 }
233}
Note: See TracBrowser for help on using the repository browser.