1 | package org.openstreetmap.josm.data.osm;
|
---|
2 |
|
---|
3 | import java.text.SimpleDateFormat;
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.Collections;
|
---|
6 | import java.util.Date;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import java.util.Map;
|
---|
9 | import java.util.Map.Entry;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
12 |
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * An OSM primitive can be associated with a key/value pair. It can be created, deleted
|
---|
16 | * and updated within the OSM-Server.
|
---|
17 | *
|
---|
18 | * Although OsmPrimitive is designed as a base class, it is not to be meant to subclass
|
---|
19 | * it by any other than from the package org.openstreetmap.josm.data.osm (thus the
|
---|
20 | * visibility of the constructor). The available primitives are a fixed set that are given
|
---|
21 | * by the server environment and not an extendible data stuff.
|
---|
22 | *
|
---|
23 | * @author imi
|
---|
24 | */
|
---|
25 | abstract public class OsmPrimitive {
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * The key/value list for this primitive.
|
---|
29 | */
|
---|
30 | public Map<String, String> keys;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Unique identifier in OSM. This is used to reidentify objects in the server.
|
---|
34 | * An id of 0 means an unknown id. The object has not been uploaded yet to
|
---|
35 | * know what id it will get.
|
---|
36 | */
|
---|
37 | public long id = 0;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * <code>true</code>, if the object has been modified since it was loaded from
|
---|
41 | * the server. In this case, on next upload, this object will be updated.
|
---|
42 | * Deleted objects are deleted from the server. If the objects are added (id=0),
|
---|
43 | * the modified is ignored and the object is added to the server.
|
---|
44 | */
|
---|
45 | public boolean modified = false;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * <code>true</code>, if the object has been deleted.
|
---|
49 | */
|
---|
50 | public boolean deleted = false;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * If set to true, this object is currently selected.
|
---|
54 | */
|
---|
55 | public volatile boolean selected = false;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Time of last modification to this object. This is not set by JOSM but
|
---|
59 | * read from the server and delivered back to the server unmodified. It is
|
---|
60 | * used to check against edit conflicts.
|
---|
61 | */
|
---|
62 | public Date timestamp = null;
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Implementation of the visitor scheme. Subclases have to call the correct
|
---|
66 | * visitor function.
|
---|
67 | * @param visitor The visitor from which the visit() function must be called.
|
---|
68 | */
|
---|
69 | abstract public void visit(Visitor visitor);
|
---|
70 |
|
---|
71 | public final void delete(boolean deleted) {
|
---|
72 | this.deleted = deleted;
|
---|
73 | selected = false;
|
---|
74 | modified = true;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Equal, if the id (and class) is equal. If both ids are 0, use the super classes
|
---|
79 | * equal instead.
|
---|
80 | *
|
---|
81 | * An primitive is equal to its incomplete counter part.
|
---|
82 | */
|
---|
83 | @Override public final boolean equals(Object obj) {
|
---|
84 | if (obj == null || getClass() != obj.getClass() || id == 0 || ((OsmPrimitive)obj).id == 0)
|
---|
85 | return super.equals(obj);
|
---|
86 | return id == ((OsmPrimitive)obj).id;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Return the id as hashcode or supers hashcode if 0.
|
---|
91 | *
|
---|
92 | * An primitive has the same hashcode as its incomplete counter part.
|
---|
93 | */
|
---|
94 | @Override public final int hashCode() {
|
---|
95 | return id == 0 ? super.hashCode() : (int)id;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Set the given value to the given key
|
---|
100 | * @param key The key, for which the value is to be set.
|
---|
101 | * @param value The value for the key.
|
---|
102 | */
|
---|
103 | public final void put(String key, String value) {
|
---|
104 | if (value == null)
|
---|
105 | remove(key);
|
---|
106 | else {
|
---|
107 | if (keys == null)
|
---|
108 | keys = new HashMap<String, String>();
|
---|
109 | keys.put(key, value);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | /**
|
---|
113 | * Remove the given key from the list.
|
---|
114 | */
|
---|
115 | public final void remove(String key) {
|
---|
116 | if (keys != null) {
|
---|
117 | keys.remove(key);
|
---|
118 | if (keys.isEmpty())
|
---|
119 | keys = null;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | public final String get(String key) {
|
---|
124 | return keys == null ? null : keys.get(key);
|
---|
125 | }
|
---|
126 |
|
---|
127 | public final Collection<Entry<String, String>> entrySet() {
|
---|
128 | if (keys == null)
|
---|
129 | return Collections.emptyList();
|
---|
130 | return keys.entrySet();
|
---|
131 | }
|
---|
132 |
|
---|
133 | public final Collection<String> keySet() {
|
---|
134 | if (keys == null)
|
---|
135 | return Collections.emptyList();
|
---|
136 | return keys.keySet();
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Get and write all attributes from the parameter. Does not fire any listener, so
|
---|
141 | * use this only in the data initializing phase
|
---|
142 | */
|
---|
143 | public void cloneFrom(OsmPrimitive osm) {
|
---|
144 | keys = osm.keys == null ? null : new HashMap<String, String>(osm.keys);
|
---|
145 | id = osm.id;
|
---|
146 | modified = osm.modified;
|
---|
147 | deleted = osm.deleted;
|
---|
148 | selected = osm.selected;
|
---|
149 | timestamp = osm.timestamp;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Perform an equality compare for all non-volatile fields not only for the id
|
---|
154 | * but for the whole object (for conflict resolving etc)
|
---|
155 | */
|
---|
156 | public boolean realEqual(OsmPrimitive osm) {
|
---|
157 | return
|
---|
158 | id == osm.id &&
|
---|
159 | modified == osm.modified &&
|
---|
160 | deleted == osm.deleted &&
|
---|
161 | (timestamp == null ? osm.timestamp==null : timestamp.equals(osm.timestamp)) &&
|
---|
162 | (keys == null ? osm.keys==null : keys.equals(osm.keys));
|
---|
163 | }
|
---|
164 |
|
---|
165 | public String getTimeStr() {
|
---|
166 | return timestamp == null ? null : new SimpleDateFormat("y-M-d H:m:s").format(timestamp);
|
---|
167 | }
|
---|
168 | }
|
---|