1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.text.SimpleDateFormat;
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.Collections;
|
---|
8 | import java.util.Date;
|
---|
9 | import java.util.HashMap;
|
---|
10 | import java.util.Map;
|
---|
11 | import java.util.Map.Entry;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
14 |
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * An OSM primitive can be associated with a key/value pair. It can be created, deleted
|
---|
18 | * and updated within the OSM-Server.
|
---|
19 | *
|
---|
20 | * Although OsmPrimitive is designed as a base class, it is not to be meant to subclass
|
---|
21 | * it by any other than from the package org.openstreetmap.josm.data.osm (thus the
|
---|
22 | * visibility of the constructor). The available primitives are a fixed set that are given
|
---|
23 | * by the server environment and not an extendible data stuff.
|
---|
24 | *
|
---|
25 | * @author imi
|
---|
26 | */
|
---|
27 | abstract public class OsmPrimitive implements Comparable<OsmPrimitive> {
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * The key/value list for this primitive.
|
---|
31 | */
|
---|
32 | public Map<String, String> keys;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Unique identifier in OSM. This is used to reidentify objects in the server.
|
---|
36 | * An id of 0 means an unknown id. The object has not been uploaded yet to
|
---|
37 | * know what id it will get.
|
---|
38 | *
|
---|
39 | * Do not write to this attribute except you know exactly what you are doing.
|
---|
40 | * More specific, it is not good to set this to 0 and think the object is now
|
---|
41 | * new to the server! To create a new object, call the default constructor of
|
---|
42 | * the respective class.
|
---|
43 | */
|
---|
44 | public long id = 0;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * <code>true</code>, if the object has been modified since it was loaded from
|
---|
48 | * the server. In this case, on next upload, this object will be updated.
|
---|
49 | * Deleted objects are deleted from the server. If the objects are added (id=0),
|
---|
50 | * the modified is ignored and the object is added to the server.
|
---|
51 | */
|
---|
52 | public boolean modified = false;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * <code>true</code>, if the object has been deleted.
|
---|
56 | */
|
---|
57 | public boolean deleted = false;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Visibility status as specified by the server. The visible attribute was
|
---|
61 | * introduced with the 0.4 API to be able to communicate deleted objects
|
---|
62 | * (they will have visible=false). Currently JOSM does never deal with
|
---|
63 | * these, so this is really for future use only.
|
---|
64 | */
|
---|
65 | public boolean visible = true;
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * User that last modified this primitive, as specified by the server.
|
---|
69 | * Never changed by JOSM.
|
---|
70 | */
|
---|
71 | public User user = null;
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * <code>true</code>, if the object has been shown. This property is not used
|
---|
75 | * internally by JOSM, but can be used by plugins that take over the object
|
---|
76 | * rendering.
|
---|
77 | */
|
---|
78 | public volatile boolean shown = false;
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * If set to true, this object is currently selected.
|
---|
82 | */
|
---|
83 | public volatile boolean selected = false;
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Time of last modification to this object. This is not set by JOSM but
|
---|
87 | * read from the server and delivered back to the server unmodified. It is
|
---|
88 | * used to check against edit conflicts.
|
---|
89 | */
|
---|
90 | public Date timestamp = null;
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * If set to true, this object is incomplete, which means only the id
|
---|
94 | * and type is known (type is the objects instance class)
|
---|
95 | */
|
---|
96 | public boolean incomplete = false;
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Implementation of the visitor scheme. Subclases have to call the correct
|
---|
100 | * visitor function.
|
---|
101 | * @param visitor The visitor from which the visit() function must be called.
|
---|
102 | */
|
---|
103 | abstract public void visit(Visitor visitor);
|
---|
104 |
|
---|
105 | public final void delete(boolean deleted) {
|
---|
106 | this.deleted = deleted;
|
---|
107 | selected = false;
|
---|
108 | modified = true;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Equal, if the id (and class) is equal. If both ids are 0, use the super classes
|
---|
113 | * equal instead.
|
---|
114 | *
|
---|
115 | * An primitive is equal to its incomplete counter part.
|
---|
116 | */
|
---|
117 | @Override public final boolean equals(Object obj) {
|
---|
118 | if (obj == null || getClass() != obj.getClass() || id == 0 || ((OsmPrimitive)obj).id == 0)
|
---|
119 | return super.equals(obj);
|
---|
120 | return id == ((OsmPrimitive)obj).id;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Return the id plus the class type encoded as hashcode or supers hashcode if id is 0.
|
---|
125 | *
|
---|
126 | * An primitive has the same hashcode as its incomplete counter part.
|
---|
127 | */
|
---|
128 | @Override public final int hashCode() {
|
---|
129 | if (id == 0)
|
---|
130 | return super.hashCode();
|
---|
131 | final int[] ret = new int[1];
|
---|
132 | Visitor v = new Visitor(){
|
---|
133 | public void visit(Node n) { ret[0] = 1; }
|
---|
134 | public void visit(Way w) { ret[0] = 2; }
|
---|
135 | public void visit(Relation e) { ret[0] = 3; }
|
---|
136 | };
|
---|
137 | visit(v);
|
---|
138 | return id == 0 ? super.hashCode() : (int)(id<<2)+ret[0];
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Set the given value to the given key
|
---|
143 | * @param key The key, for which the value is to be set.
|
---|
144 | * @param value The value for the key.
|
---|
145 | */
|
---|
146 | public final void put(String key, String value) {
|
---|
147 | if (value == null)
|
---|
148 | remove(key);
|
---|
149 | else {
|
---|
150 | if (keys == null)
|
---|
151 | keys = new HashMap<String, String>();
|
---|
152 | keys.put(key, value);
|
---|
153 | }
|
---|
154 | }
|
---|
155 | /**
|
---|
156 | * Remove the given key from the list.
|
---|
157 | */
|
---|
158 | public final void remove(String key) {
|
---|
159 | if (keys != null) {
|
---|
160 | keys.remove(key);
|
---|
161 | if (keys.isEmpty())
|
---|
162 | keys = null;
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | public final String get(String key) {
|
---|
167 | return keys == null ? null : keys.get(key);
|
---|
168 | }
|
---|
169 |
|
---|
170 | public final Collection<Entry<String, String>> entrySet() {
|
---|
171 | if (keys == null)
|
---|
172 | return Collections.emptyList();
|
---|
173 | return keys.entrySet();
|
---|
174 | }
|
---|
175 |
|
---|
176 | public final Collection<String> keySet() {
|
---|
177 | if (keys == null)
|
---|
178 | return Collections.emptyList();
|
---|
179 | return keys.keySet();
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Get and write all attributes from the parameter. Does not fire any listener, so
|
---|
184 | * use this only in the data initializing phase
|
---|
185 | */
|
---|
186 | public void cloneFrom(OsmPrimitive osm) {
|
---|
187 | keys = osm.keys == null ? null : new HashMap<String, String>(osm.keys);
|
---|
188 | id = osm.id;
|
---|
189 | modified = osm.modified;
|
---|
190 | deleted = osm.deleted;
|
---|
191 | selected = osm.selected;
|
---|
192 | timestamp = osm.timestamp;
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Perform an equality compare for all non-volatile fields not only for the id
|
---|
197 | * but for the whole object (for conflict resolving)
|
---|
198 | * @param semanticOnly if <code>true</code>, modified flag and timestamp are not compared
|
---|
199 | */
|
---|
200 | public boolean realEqual(OsmPrimitive osm, boolean semanticOnly) {
|
---|
201 | return
|
---|
202 | id == osm.id &&
|
---|
203 | (semanticOnly || (modified == osm.modified)) &&
|
---|
204 | deleted == osm.deleted &&
|
---|
205 | (semanticOnly || (timestamp == null ? osm.timestamp==null : timestamp.equals(osm.timestamp))) &&
|
---|
206 | (semanticOnly || (user == null ? osm.user==null : user==osm.user)) &&
|
---|
207 | (semanticOnly || (visible == osm.visible)) &&
|
---|
208 | (keys == null ? osm.keys==null : keys.equals(osm.keys));
|
---|
209 | }
|
---|
210 |
|
---|
211 | public String getTimeStr() {
|
---|
212 | return timestamp == null ? null : new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp);
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | }
|
---|