source: josm/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java@ 159

Last change on this file since 159 was 159, checked in by imi, 18 years ago
  • moved translations into plugins
  • added main menu control to main
  • added ImageProvider access to plugins
File size: 4.9 KB
Line 
1package org.openstreetmap.josm.data.osm;
2
3import java.text.SimpleDateFormat;
4import java.util.Collection;
5import java.util.Collections;
6import java.util.Date;
7import java.util.HashMap;
8import java.util.Map;
9import java.util.Map.Entry;
10
11import 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 */
25abstract public class OsmPrimitive implements Comparable<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 plus the class type encoded as hashcode or supers hashcode if id is 0.
91 *
92 * An primitive has the same hashcode as its incomplete counter part.
93 */
94 @Override public final int hashCode() {
95 if (id == 0)
96 return super.hashCode();
97 final int[] ret = new int[1];
98 Visitor v = new Visitor(){
99 public void visit(Node n) { ret[0] = 1; }
100 public void visit(Segment s) { ret[0] = 2; }
101 public void visit(Way w) { ret[0] = 3; }
102 };
103 visit(v);
104 return id == 0 ? super.hashCode() : (int)(id<<3)+ret[0];
105 }
106
107 /**
108 * Set the given value to the given key
109 * @param key The key, for which the value is to be set.
110 * @param value The value for the key.
111 */
112 public final void put(String key, String value) {
113 if (value == null)
114 remove(key);
115 else {
116 if (keys == null)
117 keys = new HashMap<String, String>();
118 keys.put(key, value);
119 }
120 }
121 /**
122 * Remove the given key from the list.
123 */
124 public final void remove(String key) {
125 if (keys != null) {
126 keys.remove(key);
127 if (keys.isEmpty())
128 keys = null;
129 }
130 }
131
132 public final String get(String key) {
133 return keys == null ? null : keys.get(key);
134 }
135
136 public final Collection<Entry<String, String>> entrySet() {
137 if (keys == null)
138 return Collections.emptyList();
139 return keys.entrySet();
140 }
141
142 public final Collection<String> keySet() {
143 if (keys == null)
144 return Collections.emptyList();
145 return keys.keySet();
146 }
147
148 /**
149 * Get and write all attributes from the parameter. Does not fire any listener, so
150 * use this only in the data initializing phase
151 */
152 public void cloneFrom(OsmPrimitive osm) {
153 keys = osm.keys == null ? null : new HashMap<String, String>(osm.keys);
154 id = osm.id;
155 modified = osm.modified;
156 deleted = osm.deleted;
157 selected = osm.selected;
158 timestamp = osm.timestamp;
159 }
160
161 /**
162 * Perform an equality compare for all non-volatile fields not only for the id
163 * but for the whole object (for conflict resolving etc)
164 */
165 public boolean realEqual(OsmPrimitive osm) {
166 return
167 id == osm.id &&
168 modified == osm.modified &&
169 deleted == osm.deleted &&
170 (timestamp == null ? osm.timestamp==null : timestamp.equals(osm.timestamp)) &&
171 (keys == null ? osm.keys==null : keys.equals(osm.keys));
172 }
173
174 public String getTimeStr() {
175 return timestamp == null ? null : new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp);
176 }
177}
Note: See TracBrowser for help on using the repository browser.