1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.Date;
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.Map;
|
---|
13 | import java.util.Map.Entry;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
17 | import 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 | */
|
---|
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 | /* 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] = 0; }
|
---|
214 | public void visit(Way w) { ret[0] = 1; }
|
---|
215 | public void visit(Relation e) { ret[0] = 2; }
|
---|
216 | public void visit(Changeset cs) { ret[0] = 3; }
|
---|
217 | };
|
---|
218 | visit(v);
|
---|
219 | return id == 0 ? super.hashCode() : (int)(id<<2)+ret[0];
|
---|
220 | }
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * Set the given value to the given key
|
---|
224 | * @param key The key, for which the value is to be set.
|
---|
225 | * @param value The value for the key.
|
---|
226 | */
|
---|
227 | public final void put(String key, String value) {
|
---|
228 | if (value == null)
|
---|
229 | remove(key);
|
---|
230 | else {
|
---|
231 | if (keys == null)
|
---|
232 | keys = new HashMap<String, String>();
|
---|
233 | keys.put(key, value);
|
---|
234 | }
|
---|
235 | mappaintStyle = null;
|
---|
236 | }
|
---|
237 | /**
|
---|
238 | * Remove the given key from the list.
|
---|
239 | */
|
---|
240 | public final void remove(String key) {
|
---|
241 | if (keys != null) {
|
---|
242 | keys.remove(key);
|
---|
243 | if (keys.isEmpty())
|
---|
244 | keys = null;
|
---|
245 | }
|
---|
246 | mappaintStyle = null;
|
---|
247 | }
|
---|
248 |
|
---|
249 | public String getName() {
|
---|
250 | return null;
|
---|
251 | }
|
---|
252 |
|
---|
253 | public final String get(String key) {
|
---|
254 | return keys == null ? null : keys.get(key);
|
---|
255 | }
|
---|
256 |
|
---|
257 | public final Collection<Entry<String, String>> entrySet() {
|
---|
258 | if (keys == null)
|
---|
259 | return Collections.emptyList();
|
---|
260 | return keys.entrySet();
|
---|
261 | }
|
---|
262 |
|
---|
263 | public final Collection<String> keySet() {
|
---|
264 | if (keys == null)
|
---|
265 | return Collections.emptyList();
|
---|
266 | return keys.keySet();
|
---|
267 | }
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Get and write all attributes from the parameter. Does not fire any listener, so
|
---|
271 | * use this only in the data initializing phase
|
---|
272 | */
|
---|
273 | public void cloneFrom(OsmPrimitive osm) {
|
---|
274 | keys = osm.keys == null ? null : new HashMap<String, String>(osm.keys);
|
---|
275 | id = osm.id;
|
---|
276 | modified = osm.modified;
|
---|
277 | deleted = osm.deleted;
|
---|
278 | selected = osm.selected;
|
---|
279 | timestamp = osm.timestamp;
|
---|
280 | version = osm.version;
|
---|
281 | incomplete = osm.incomplete;
|
---|
282 | clearCached();
|
---|
283 | clearErrors();
|
---|
284 | }
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * Perform an equality compare for all non-volatile fields not only for the id
|
---|
288 | * but for the whole object (for conflict resolving)
|
---|
289 | * @param semanticOnly if <code>true</code>, modified flag and timestamp are not compared
|
---|
290 | */
|
---|
291 | public boolean realEqual(OsmPrimitive osm, boolean semanticOnly) {
|
---|
292 | return id == osm.id
|
---|
293 | && incomplete == osm.incomplete
|
---|
294 | && deleted == osm.deleted
|
---|
295 | && (semanticOnly || (modified == osm.modified
|
---|
296 | && timestamp == osm.timestamp
|
---|
297 | && version == osm.version
|
---|
298 | && visible == osm.visible
|
---|
299 | && (user == null ? osm.user==null : user==osm.user)))
|
---|
300 | && (keys == null ? osm.keys==null : keys.equals(osm.keys));
|
---|
301 | }
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * true if this object is considered "tagged". To be "tagged", an object
|
---|
305 | * must have one or more "non-standard" tags. "created_by" and "source"
|
---|
306 | * are typically considered "standard" tags and do not make an object
|
---|
307 | * "tagged".
|
---|
308 | */
|
---|
309 | public boolean isTagged() {
|
---|
310 | // TODO Cache value after keys are made private
|
---|
311 | getUninterestingKeys();
|
---|
312 | if (keys != null) {
|
---|
313 | for (Entry<String,String> e : keys.entrySet()) {
|
---|
314 | if (!uninteresting.contains(e.getKey())) {
|
---|
315 | return true;
|
---|
316 | }
|
---|
317 | }
|
---|
318 | }
|
---|
319 | return false;
|
---|
320 | }
|
---|
321 | /**
|
---|
322 | * true if this object has direction dependent tags (e.g. oneway)
|
---|
323 | */
|
---|
324 | public boolean hasDirectionKeys() {
|
---|
325 | // TODO Cache value after keys are made private
|
---|
326 | getDirectionKeys();
|
---|
327 | if (keys != null) {
|
---|
328 | for (Entry<String,String> e : keys.entrySet()) {
|
---|
329 | if (directionKeys.contains(e.getKey())) {
|
---|
330 | return true;
|
---|
331 | }
|
---|
332 | }
|
---|
333 | }
|
---|
334 | return false;
|
---|
335 | }
|
---|
336 | }
|
---|