source: josm/trunk/src/org/openstreetmap/josm/data/osm/PrimitiveData.java@ 2404

Last change on this file since 2404 was 2404, checked in by jttt, 14 years ago

Use Dataset.getPrimitiveById() instead of custom looking for primitive in Way.load() and Relation.load()

  • Property svn:mime-type set to text/plain
File size: 4.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.HashMap;
7import java.util.List;
8import java.util.Map;
9
10/**
11 *
12 * This class can be used to save properties of OsmPrimitive. The main difference between PrimitiveData
13 * and OsmPrimitive is that PrimitiveData is not part of the dataset and changes in PrimitiveData are not
14 * reported by events
15 *
16 */
17public abstract class PrimitiveData implements Tagged, PrimitiveId {
18
19 // Useful?
20 //private boolean disabled;
21 //private boolean filtered;
22 //private boolean selected;
23 //private boolean highlighted;
24
25 public PrimitiveData() {
26 id = OsmPrimitive.generateUniqueId();
27 }
28
29 public PrimitiveData(PrimitiveData data) {
30 this.keys.putAll(data.keys);
31 this.modified = data.modified;
32 this.visible = data.visible;
33 this.deleted = data.deleted;
34 this.id = data.id;
35 this.user = data.user;
36 this.version = data.version;
37 this.timestamp = data.timestamp;
38 }
39
40 private final Map<String, String> keys = new HashMap<String, String>();
41 private boolean modified;
42 private boolean visible = true;
43 private boolean deleted;
44 private long id;
45 private User user;
46 private int version;
47 private int timestamp;
48
49 public boolean isModified() {
50 return modified;
51 }
52 public void setModified(boolean modified) {
53 this.modified = modified;
54 }
55 public boolean isVisible() {
56 return visible;
57 }
58 public void setVisible(boolean visible) {
59 this.visible = visible;
60 }
61 public boolean isDeleted() {
62 return deleted;
63 }
64 public void setDeleted(boolean deleted) {
65 this.deleted = deleted;
66 }
67 public long getId() {
68 return id;
69 }
70 public void setId(long id) {
71 this.id = id;
72 }
73 public User getUser() {
74 return user;
75 }
76 public void setUser(User user) {
77 this.user = user;
78 }
79 public int getVersion() {
80 return version;
81 }
82 public void setVersion(int version) {
83 this.version = version;
84 }
85 public int getTimestamp() {
86 return timestamp;
87 }
88 public void setTimestamp(int timestamp) {
89 this.timestamp = timestamp;
90 }
91 public Map<String, String> getKeys() {
92 return keys;
93 }
94
95 public void clearOsmId() {
96 id = OsmPrimitive.generateUniqueId();
97 }
98
99 public abstract PrimitiveData makeCopy();
100
101 public abstract OsmPrimitive makePrimitive(DataSet dataSet);
102
103 @Override
104 public String toString() {
105 StringBuilder builder = new StringBuilder();
106
107 builder.append(id).append(keys);
108 if (modified) {
109 builder.append("M");
110 }
111 if (visible) {
112 builder.append("V");
113 }
114 if (deleted) {
115 builder.append("D");
116 }
117
118 return builder.toString();
119 }
120
121 // Tagged implementation
122
123 public String get(String key) {
124 return keys.get(key);
125 }
126
127 public boolean hasKeys() {
128 return !keys.isEmpty();
129 }
130
131 public Collection<String> keySet() {
132 return keys.keySet();
133 }
134
135 public void put(String key, String value) {
136 keys.put(key, value);
137 }
138
139 public void remove(String key) {
140 keys.remove(key);
141 }
142
143 public void removeAll() {
144 keys.clear();
145 }
146
147 public void setKeys(Map<String, String> keys) {
148 this.keys.clear();
149 this.keys.putAll(keys);
150 }
151
152
153 @SuppressWarnings("unchecked")
154 static public <T extends PrimitiveData> List<T> getFilteredList(Collection<T> list, OsmPrimitiveType type) {
155 List<T> ret = new ArrayList<T>();
156 for(PrimitiveData p: list) {
157 if (type.getDataClass().isInstance(p)) {
158 ret.add((T)p);
159 }
160 }
161 return ret;
162 }
163
164 protected void setKeysAsList(String... keys) {
165 assert keys.length % 2 == 0;
166 for (int i=0; i<keys.length/2; i++) {
167 this.keys.put(keys[i * 2], keys[i * 2 + 1]);
168 }
169 }
170
171 /**
172 * PrimitiveId implementation. Returns the same value as getId()
173 */
174 public long getUniqueId() {
175 return id;
176 }
177
178
179
180}
Note: See TracBrowser for help on using the repository browser.