1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.text.ParseException;
|
---|
5 | import java.text.SimpleDateFormat;
|
---|
6 | import java.util.Arrays;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.Collections;
|
---|
9 | import java.util.Date;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.HashSet;
|
---|
12 | import java.util.Map;
|
---|
13 | import java.util.Map.Entry;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
16 | import org.openstreetmap.josm.tools.DateParser;
|
---|
17 |
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * An OSM primitive can be associated with a key/value pair. It can be created, deleted
|
---|
21 | * and updated within the OSM-Server.
|
---|
22 | *
|
---|
23 | * Although OsmPrimitive is designed as a base class, it is not to be meant to subclass
|
---|
24 | * it by any other than from the package org.openstreetmap.josm.data.osm (thus the
|
---|
25 | * visibility of the constructor). 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 | */
|
---|
30 | abstract 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 | /**
|
---|
38 | * Unique identifier in OSM. This is used to reidentify objects in the server.
|
---|
39 | * An id of 0 means an unknown id. The object has not been uploaded yet to
|
---|
40 | * know what id it will get.
|
---|
41 | *
|
---|
42 | * Do not write to this attribute except you know exactly what you are doing.
|
---|
43 | * More specific, it is not good to set this to 0 and think the object is now
|
---|
44 | * new to the server! To create a new object, call the default constructor of
|
---|
45 | * the respective class.
|
---|
46 | */
|
---|
47 | public long id = 0;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * <code>true</code>, if the object has been modified since it was loaded from
|
---|
51 | * the server. In this case, on next upload, this object will be updated.
|
---|
52 | * Deleted objects are deleted from the server. If the objects are added (id=0),
|
---|
53 | * the modified is ignored and the object is added to the server.
|
---|
54 | */
|
---|
55 | public boolean modified = false;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * <code>true</code>, if the object has been deleted.
|
---|
59 | */
|
---|
60 | public boolean deleted = false;
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Visibility status as specified by the server. The visible attribute was
|
---|
64 | * introduced with the 0.4 API to be able to communicate deleted objects
|
---|
65 | * (they will have visible=false). Currently JOSM does never deal with
|
---|
66 | * these, so this is really for future use only.
|
---|
67 | */
|
---|
68 | public boolean visible = true;
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * User that last modified this primitive, as specified by the server.
|
---|
72 | * Never changed by JOSM.
|
---|
73 | */
|
---|
74 | public User user = null;
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * true if this object is considered "tagged". To be "tagged", an object
|
---|
78 | * must have one or more "non-standard" tags. "created_by" and "source"
|
---|
79 | * are typically considered "standard" tags and do not make an object
|
---|
80 | * "tagged".
|
---|
81 | */
|
---|
82 | public boolean tagged = false;
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * If set to true, this object is currently selected.
|
---|
86 | */
|
---|
87 | public volatile boolean selected = false;
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Time of last modification to this object. This is not set by JOSM but
|
---|
91 | * read from the server and delivered back to the server unmodified. It is
|
---|
92 | * used to check against edit conflicts.
|
---|
93 | */
|
---|
94 | public String timestamp = null;
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * The timestamp is only parsed when this is really necessary, and this
|
---|
98 | * is the cache for the result.
|
---|
99 | */
|
---|
100 | public Date parsedTimestamp = null;
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * If set to true, this object is incomplete, which means only the id
|
---|
104 | * and type is known (type is the objects instance class)
|
---|
105 | */
|
---|
106 | public boolean incomplete = false;
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Contains a list of "uninteresting" keys that do not make an object
|
---|
110 | * "tagged".
|
---|
111 | */
|
---|
112 | public static Collection<String> uninteresting =
|
---|
113 | new HashSet<String>(Arrays.asList(new String[] {"source", "note", "created_by"}));
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Implementation of the visitor scheme. Subclases have to call the correct
|
---|
117 | * visitor function.
|
---|
118 | * @param visitor The visitor from which the visit() function must be called.
|
---|
119 | */
|
---|
120 | abstract public void visit(Visitor visitor);
|
---|
121 |
|
---|
122 | public final void delete(boolean deleted) {
|
---|
123 | this.deleted = deleted;
|
---|
124 | selected = false;
|
---|
125 | modified = true;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Returns the timestamp for this object, or the current time if none is set.
|
---|
130 | * Internally, parses the timestamp from XML into a Date object and caches it
|
---|
131 | * for possible repeated calls.
|
---|
132 | */
|
---|
133 | public Date getTimestamp() {
|
---|
134 | if (parsedTimestamp == null) {
|
---|
135 | try {
|
---|
136 | parsedTimestamp = DateParser.parse(timestamp);
|
---|
137 | } catch (ParseException ex) {
|
---|
138 | parsedTimestamp = new Date();
|
---|
139 | }
|
---|
140 | }
|
---|
141 | return parsedTimestamp;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Equal, if the id (and class) is equal. If both ids are 0, use the super classes
|
---|
146 | * equal instead.
|
---|
147 | *
|
---|
148 | * An primitive is equal to its incomplete counter part.
|
---|
149 | */
|
---|
150 | @Override public final boolean equals(Object obj) {
|
---|
151 | if (obj == null || getClass() != obj.getClass() || id == 0 || ((OsmPrimitive)obj).id == 0)
|
---|
152 | return super.equals(obj);
|
---|
153 | return id == ((OsmPrimitive)obj).id;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Return the id plus the class type encoded as hashcode or supers hashcode if id is 0.
|
---|
158 | *
|
---|
159 | * An primitive has the same hashcode as its incomplete counter part.
|
---|
160 | */
|
---|
161 | @Override public final int hashCode() {
|
---|
162 | if (id == 0)
|
---|
163 | return super.hashCode();
|
---|
164 | final int[] ret = new int[1];
|
---|
165 | Visitor v = new Visitor(){
|
---|
166 | public void visit(Node n) { ret[0] = 1; }
|
---|
167 | public void visit(Way w) { ret[0] = 2; }
|
---|
168 | public void visit(Relation e) { ret[0] = 3; }
|
---|
169 | };
|
---|
170 | visit(v);
|
---|
171 | return id == 0 ? super.hashCode() : (int)(id<<2)+ret[0];
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Set the given value to the given key
|
---|
176 | * @param key The key, for which the value is to be set.
|
---|
177 | * @param value The value for the key.
|
---|
178 | */
|
---|
179 | public final void put(String key, String value) {
|
---|
180 | if (value == null)
|
---|
181 | remove(key);
|
---|
182 | else {
|
---|
183 | if (keys == null)
|
---|
184 | keys = new HashMap<String, String>();
|
---|
185 | keys.put(key, value);
|
---|
186 | }
|
---|
187 | checkTagged();
|
---|
188 | }
|
---|
189 | /**
|
---|
190 | * Remove the given key from the list.
|
---|
191 | */
|
---|
192 | public final void remove(String key) {
|
---|
193 | if (keys != null) {
|
---|
194 | keys.remove(key);
|
---|
195 | if (keys.isEmpty())
|
---|
196 | keys = null;
|
---|
197 | }
|
---|
198 | checkTagged();
|
---|
199 | }
|
---|
200 |
|
---|
201 | public final String get(String key) {
|
---|
202 | return keys == null ? null : keys.get(key);
|
---|
203 | }
|
---|
204 |
|
---|
205 | public final Collection<Entry<String, String>> entrySet() {
|
---|
206 | if (keys == null)
|
---|
207 | return Collections.emptyList();
|
---|
208 | return keys.entrySet();
|
---|
209 | }
|
---|
210 |
|
---|
211 | public final Collection<String> keySet() {
|
---|
212 | if (keys == null)
|
---|
213 | return Collections.emptyList();
|
---|
214 | return keys.keySet();
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Get and write all attributes from the parameter. Does not fire any listener, so
|
---|
219 | * use this only in the data initializing phase
|
---|
220 | */
|
---|
221 | public void cloneFrom(OsmPrimitive osm) {
|
---|
222 | keys = osm.keys == null ? null : new HashMap<String, String>(osm.keys);
|
---|
223 | id = osm.id;
|
---|
224 | modified = osm.modified;
|
---|
225 | deleted = osm.deleted;
|
---|
226 | selected = osm.selected;
|
---|
227 | timestamp = osm.timestamp;
|
---|
228 | tagged = osm.tagged;
|
---|
229 | incomplete = osm.incomplete;
|
---|
230 | }
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Perform an equality compare for all non-volatile fields not only for the id
|
---|
234 | * but for the whole object (for conflict resolving)
|
---|
235 | * @param semanticOnly if <code>true</code>, modified flag and timestamp are not compared
|
---|
236 | */
|
---|
237 | public boolean realEqual(OsmPrimitive osm, boolean semanticOnly) {
|
---|
238 | return
|
---|
239 | id == osm.id &&
|
---|
240 | incomplete == osm.incomplete &&
|
---|
241 | (semanticOnly || (modified == osm.modified)) &&
|
---|
242 | deleted == osm.deleted &&
|
---|
243 | (semanticOnly || (timestamp == null ? osm.timestamp==null : timestamp.equals(osm.timestamp))) &&
|
---|
244 | (semanticOnly || (user == null ? osm.user==null : user==osm.user)) &&
|
---|
245 | (semanticOnly || (visible == osm.visible)) &&
|
---|
246 | (keys == null ? osm.keys==null : keys.equals(osm.keys));
|
---|
247 | }
|
---|
248 |
|
---|
249 | public String getTimeStr() {
|
---|
250 | return timestamp == null ? null : new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp);
|
---|
251 | }
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * Updates the "tagged" flag. "keys" property should probably be made private
|
---|
255 | * to make sure this gets called when keys are set.
|
---|
256 | */
|
---|
257 | public void checkTagged() {
|
---|
258 | tagged = false;
|
---|
259 | if (keys != null) {
|
---|
260 | for (Entry<String,String> e : keys.entrySet()) {
|
---|
261 | if (!uninteresting.contains(e.getKey())) {
|
---|
262 | tagged = true;
|
---|
263 | break;
|
---|
264 | }
|
---|
265 | }
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | }
|
---|