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

Last change on this file since 3083 was 3083, checked in by bastiK, 14 years ago

added svn:eol-style=native to source files

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