source: josm/trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java@ 1499

Last change on this file since 1499 was 1499, checked in by stoecker, 15 years ago

close #2302 - patch by jttt - optimizations and encapsulation

  • Property svn:eol-style set to native
File size: 10.5 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.data.osm;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.ArrayList;
7import java.util.Arrays;
8import java.util.Collection;
9import java.util.Collections;
10import java.util.Date;
11import java.util.HashMap;
12import java.util.Map;
13import java.util.Map.Entry;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.osm.visitor.Visitor;
17import org.openstreetmap.josm.gui.mappaint.ElemStyle;
18
19
20/**
21 * An OSM primitive can be associated with a key/value pair. It can be created, deleted
22 * and updated within the OSM-Server.
23 *
24 * Although OsmPrimitive is designed as a base class, it is not to be meant to subclass
25 * it by any other than from the package {@link org.openstreetmap.josm.data.osm}. The available primitives are a fixed set that are given
26 * by the server environment and not an extendible data stuff.
27 *
28 * @author imi
29 */
30abstract public class OsmPrimitive implements Comparable<OsmPrimitive> {
31
32 /**
33 * The key/value list for this primitive.
34 */
35 public Map<String, String> keys;
36
37 /* mappaint data */
38 public ElemStyle mappaintStyle = null;
39 public Integer mappaintVisibleCode = 0;
40 public Integer mappaintDrawnCode = 0;
41 public Collection<String> errors;
42
43 public void putError(String text, Boolean isError)
44 {
45 if(errors == null)
46 errors = new ArrayList<String>();
47 String s = isError ? tr("Error: {0}", text) : tr("Warning: {0}", text);
48 errors.add(s);
49 }
50 public void clearErrors()
51 {
52 errors = null;
53 }
54 /* This should not be called from outside. Fixing the UI to add relevant
55 get/set functions calling this implicitely is preferred, so we can have
56 transparent cache handling in the future. */
57 protected void clearCached()
58 {
59 mappaintVisibleCode = 0;
60 mappaintDrawnCode = 0;
61 mappaintStyle = null;
62 }
63 /* end of mappaint data */
64
65 /**
66 * Unique identifier in OSM. This is used to identify objects on the server.
67 * An id of 0 means an unknown id. The object has not been uploaded yet to
68 * know what id it will get.
69 *
70 * Do not write to this attribute except you know exactly what you are doing.
71 * More specific, it is not good to set this to 0 and think the object is now
72 * new to the server! To create a new object, call the default constructor of
73 * the respective class.
74 */
75 public long id = 0;
76
77 /**
78 * <code>true</code> if the object has been modified since it was loaded from
79 * the server. In this case, on next upload, this object will be updated.
80 * Deleted objects are deleted from the server. If the objects are added (id=0),
81 * the modified is ignored and the object is added to the server.
82 */
83 public boolean modified = false;
84
85 /**
86 * <code>true</code>, if the object has been deleted.
87 */
88 public boolean deleted = false;
89
90 /**
91 * Visibility status as specified by the server. The visible attribute was
92 * introduced with the 0.4 API to be able to communicate deleted objects
93 * (they will have visible=false). Currently JOSM does never deal with
94 * these, so this is really for future use only.
95 */
96 public boolean visible = true;
97
98 /**
99 * User that last modified this primitive, as specified by the server.
100 * Never changed by JOSM.
101 */
102 public User user = null;
103
104 /**
105 * If set to true, this object is currently selected.
106 */
107 public volatile boolean selected = false;
108
109 /**
110 * If set to true, this object is highlighted. Currently this is only used to
111 * show which ways/nodes will connect
112 */
113 public volatile boolean highlighted = false;
114
115 private int timestamp;
116
117 public void setTimestamp(Date timestamp) {
118 this.timestamp = (int)(timestamp.getTime() / 1000);
119 }
120
121 /**
122 * Time of last modification to this object. This is not set by JOSM but
123 * read from the server and delivered back to the server unmodified. It is
124 * used to check against edit conflicts.
125 *
126 */
127 public Date getTimestamp() {
128 return new Date(timestamp * 1000l);
129 }
130
131 public boolean isTimestampEmpty() {
132 return timestamp == 0;
133 }
134
135 /**
136 * If set to true, this object is incomplete, which means only the id
137 * and type is known (type is the objects instance class)
138 */
139 public boolean incomplete = false;
140
141 /**
142 * Contains the version number as returned by the API. Needed to
143 * ensure update consistency
144 */
145 public int version = -1;
146
147 private static Collection<String> uninteresting = null;
148 /**
149 * Contains a list of "uninteresting" keys that do not make an object
150 * "tagged".
151 * Initialized by checkTagged()
152 */
153 public static Collection<String> getUninterestingKeys() {
154 if(uninteresting == null) {
155 uninteresting = Main.pref.getCollection("tags.uninteresting",
156 Arrays.asList(new String[]{"source","note","comment","converted_by","created_by"}));
157 }
158 return uninteresting;
159 }
160
161
162 private static Collection<String> directionKeys = null;
163
164 /**
165 * Contains a list of direction-dependent keys that make an object
166 * direction dependent.
167 * Initialized by checkDirectionTagged()
168 */
169 public static Collection<String> getDirectionKeys() {
170 if(directionKeys == null) {
171 directionKeys = Main.pref.getCollection("tags.direction",
172 Arrays.asList(new String[]{"oneway","incline","incline_steep","aerialway"}));
173 }
174 return directionKeys;
175 }
176
177 /**
178 * Implementation of the visitor scheme. Subclasses have to call the correct
179 * visitor function.
180 * @param visitor The visitor from which the visit() function must be called.
181 */
182 abstract public void visit(Visitor visitor);
183
184 public final void delete(boolean deleted) {
185 this.deleted = deleted;
186 selected = false;
187 modified = true;
188 }
189
190 /**
191 * Equal, if the id (and class) is equal.
192 *
193 * An primitive is equal to its incomplete counter part.
194 */
195 @Override public boolean equals(Object obj) {
196 if (id == 0) return obj == this;
197 if (obj instanceof OsmPrimitive) { // not null too
198 return ((OsmPrimitive)obj).id == id && obj.getClass() == getClass();
199 }
200 return false;
201 }
202
203 /**
204 * Return the id plus the class type encoded as hashcode or super's hashcode if id is 0.
205 *
206 * An primitive has the same hashcode as its incomplete counterpart.
207 */
208 @Override public final int hashCode() {
209 if (id == 0)
210 return super.hashCode();
211 final int[] ret = new int[1];
212 Visitor v = new Visitor(){
213 public void visit(Node n) { ret[0] = 1; }
214 public void visit(Way w) { ret[0] = 2; }
215 public void visit(Relation e) { ret[0] = 3; }
216 };
217 visit(v);
218 return id == 0 ? super.hashCode() : (int)(id<<2)+ret[0];
219 }
220
221 /**
222 * Set the given value to the given key
223 * @param key The key, for which the value is to be set.
224 * @param value The value for the key.
225 */
226 public final void put(String key, String value) {
227 if (value == null)
228 remove(key);
229 else {
230 if (keys == null)
231 keys = new HashMap<String, String>();
232 keys.put(key, value);
233 }
234 mappaintStyle = null;
235 }
236 /**
237 * Remove the given key from the list.
238 */
239 public final void remove(String key) {
240 if (keys != null) {
241 keys.remove(key);
242 if (keys.isEmpty())
243 keys = null;
244 }
245 mappaintStyle = null;
246 }
247
248 public String getName() {
249 return null;
250 }
251
252 public final String get(String key) {
253 return keys == null ? null : keys.get(key);
254 }
255
256 public final Collection<Entry<String, String>> entrySet() {
257 if (keys == null)
258 return Collections.emptyList();
259 return keys.entrySet();
260 }
261
262 public final Collection<String> keySet() {
263 if (keys == null)
264 return Collections.emptyList();
265 return keys.keySet();
266 }
267
268 /**
269 * Get and write all attributes from the parameter. Does not fire any listener, so
270 * use this only in the data initializing phase
271 */
272 public void cloneFrom(OsmPrimitive osm) {
273 keys = osm.keys == null ? null : new HashMap<String, String>(osm.keys);
274 id = osm.id;
275 modified = osm.modified;
276 deleted = osm.deleted;
277 selected = osm.selected;
278 timestamp = osm.timestamp;
279 version = osm.version;
280 incomplete = osm.incomplete;
281 clearCached();
282 clearErrors();
283 }
284
285 /**
286 * Perform an equality compare for all non-volatile fields not only for the id
287 * but for the whole object (for conflict resolving)
288 * @param semanticOnly if <code>true</code>, modified flag and timestamp are not compared
289 */
290 public boolean realEqual(OsmPrimitive osm, boolean semanticOnly) {
291 return id == osm.id
292 && incomplete == osm.incomplete
293 && deleted == osm.deleted
294 && (semanticOnly || (modified == osm.modified
295 && timestamp == osm.timestamp
296 && version == osm.version
297 && visible == osm.visible
298 && (user == null ? osm.user==null : user==osm.user)))
299 && (keys == null ? osm.keys==null : keys.equals(osm.keys));
300 }
301
302 /**
303 * true if this object is considered "tagged". To be "tagged", an object
304 * must have one or more "non-standard" tags. "created_by" and "source"
305 * are typically considered "standard" tags and do not make an object
306 * "tagged".
307 */
308 public boolean isTagged() {
309 // TODO Cache value after keys are made private
310 getUninterestingKeys();
311 if (keys != null) {
312 for (Entry<String,String> e : keys.entrySet()) {
313 if (!uninteresting.contains(e.getKey())) {
314 return true;
315 }
316 }
317 }
318 return false;
319 }
320 /**
321 * true if this object has direction dependent tags (e.g. oneway)
322 */
323 public boolean hasDirectionKeys() {
324 // TODO Cache value after keys are made private
325 getDirectionKeys();
326 if (keys != null) {
327 for (Entry<String,String> e : keys.entrySet()) {
328 if (directionKeys.contains(e.getKey())) {
329 return true;
330 }
331 }
332 }
333 return false;
334 }
335}
Note: See TracBrowser for help on using the repository browser.