1 | package org.openstreetmap.josm.data.osm;
|
---|
2 |
|
---|
3 | import java.util.Collection;
|
---|
4 | import java.util.Collections;
|
---|
5 | import java.util.Date;
|
---|
6 | import java.util.HashMap;
|
---|
7 | import java.util.Map;
|
---|
8 | import java.util.Map.Entry;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
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 objects content (not the properties) has been
|
---|
41 | * modified since it was loaded from the server. In this case, on next upload,
|
---|
42 | * this object will be updated. Deleted objects are deleted from the server.
|
---|
43 | * If the objects are added (id=0), the modified is ignored and the object is
|
---|
44 | * added to the server.
|
---|
45 | */
|
---|
46 | public boolean modified = false;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * <code>true</code>, if the object's keys has been changed by JOSM since
|
---|
50 | * last update.
|
---|
51 | */
|
---|
52 | public boolean modifiedProperties = false;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * <code>true</code>, if the object has been deleted.
|
---|
56 | */
|
---|
57 | private boolean deleted = false;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * If set to true, this object is currently selected.
|
---|
61 | */
|
---|
62 | private boolean selected = false;
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Time of last modification to this object. This is not set by JOSM but
|
---|
66 | * read from the server and delivered back to the server unmodified. It is
|
---|
67 | * used to check against edit conflicts.
|
---|
68 | */
|
---|
69 | public Date lastModified = null;
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Implementation of the visitor scheme. Subclases have to call the correct
|
---|
73 | * visitor function.
|
---|
74 | * @param visitor The visitor from which the visit() function must be called.
|
---|
75 | */
|
---|
76 | abstract public void visit(Visitor visitor);
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Return <code>true</code>, if either <code>this.keys</code> and
|
---|
80 | * <code>other.keys</code> is <code>null</code> or if they do not share Keys
|
---|
81 | * with different values.
|
---|
82 | *
|
---|
83 | * @param other The second key-set to compare with.
|
---|
84 | * @return True, if the keysets are mergable
|
---|
85 | */
|
---|
86 | final public boolean keyPropertiesMergable(OsmPrimitive other) {
|
---|
87 | if ((keys == null) != (other.keys == null))
|
---|
88 | return false;
|
---|
89 |
|
---|
90 | if (keys != null) {
|
---|
91 | for (String k : keys.keySet())
|
---|
92 | if (other.keys.containsKey(k) && !keys.get(k).equals(other.keys.get(k)))
|
---|
93 | return false;
|
---|
94 | for (String k : other.keys.keySet())
|
---|
95 | if (keys.containsKey(k) && !other.keys.get(k).equals(keys.get(k)))
|
---|
96 | return false;
|
---|
97 | }
|
---|
98 | return true;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Mark the primitive as selected or not selected and fires a selection
|
---|
103 | * changed later, if the value actualy changed.
|
---|
104 | * @param selected Whether the primitive should be selected or not.
|
---|
105 | */
|
---|
106 | final public void setSelected(boolean selected) {
|
---|
107 | if (selected != this.selected)
|
---|
108 | Main.main.ds.fireSelectionChanged();
|
---|
109 | this.selected = selected;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * @return Return whether the primitive is selected on screen.
|
---|
114 | */
|
---|
115 | final public boolean isSelected() {
|
---|
116 | return selected;
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | public void setDeleted(boolean deleted) {
|
---|
121 | this.deleted = deleted;
|
---|
122 | setSelected(false);
|
---|
123 | }
|
---|
124 |
|
---|
125 | public boolean isDeleted() {
|
---|
126 | return deleted;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Equal, if the id (and class) is equal. If both ids are 0, use the super classes
|
---|
131 | * equal instead.
|
---|
132 | *
|
---|
133 | * An primitive is equal to its incomplete counter part.
|
---|
134 | */
|
---|
135 | @Override
|
---|
136 | public boolean equals(Object obj) {
|
---|
137 | if (obj == null || getClass() != obj.getClass() || id == 0 || ((OsmPrimitive)obj).id == 0)
|
---|
138 | return super.equals(obj);
|
---|
139 | return id == ((OsmPrimitive)obj).id;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Return the id as hashcode or supers hashcode if 0.
|
---|
144 | *
|
---|
145 | * An primitive has the same hashcode as its incomplete counter part.
|
---|
146 | */
|
---|
147 | @Override
|
---|
148 | public int hashCode() {
|
---|
149 | return id == 0 ? super.hashCode() : (int)id;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Set the given value to the given key
|
---|
154 | * @param key The key, for which the value is to be set.
|
---|
155 | * @param value The value for the key.
|
---|
156 | */
|
---|
157 | public void put(String key, String value) {
|
---|
158 | if (keys == null)
|
---|
159 | keys = new HashMap<String, String>();
|
---|
160 | keys.put(key, value);
|
---|
161 | }
|
---|
162 | /**
|
---|
163 | * Remove the given key from the list.
|
---|
164 | */
|
---|
165 | public void remove(String key) {
|
---|
166 | if (keys != null) {
|
---|
167 | keys.remove(key);
|
---|
168 | if (keys.isEmpty())
|
---|
169 | keys = null;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | public String get(String key) {
|
---|
174 | return keys == null ? null : keys.get(key);
|
---|
175 | }
|
---|
176 |
|
---|
177 | public Collection<Entry<String, String>> entrySet() {
|
---|
178 | if (keys == null)
|
---|
179 | return Collections.emptyList();
|
---|
180 | return keys.entrySet();
|
---|
181 | }
|
---|
182 |
|
---|
183 | public Collection<String> keySet() {
|
---|
184 | if (keys == null)
|
---|
185 | return Collections.emptyList();
|
---|
186 | return keys.keySet();
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Get and write all attributes from the parameter. Does not fire any listener, so
|
---|
191 | * use this only in the data initializing phase
|
---|
192 | */
|
---|
193 | public void cloneFrom(OsmPrimitive osm) {
|
---|
194 | keys = osm.keys;
|
---|
195 | id = osm.id;
|
---|
196 | modified = osm.modified;
|
---|
197 | modifiedProperties = osm.modifiedProperties;
|
---|
198 | deleted = osm.deleted;
|
---|
199 | selected = osm.selected;
|
---|
200 | lastModified = osm.lastModified;
|
---|
201 | }
|
---|
202 | }
|
---|