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

Last change on this file since 13908 was 13808, checked in by Don-vip, 6 years ago

add IPrimitive.getReferrers(boolean allowWithoutDataset)

  • Property svn:eol-style set to native
File size: 5.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.io.IOException;
5import java.io.ObjectInputStream;
6import java.io.ObjectOutputStream;
7import java.io.Serializable;
8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.Collection;
11import java.util.Collections;
12import java.util.List;
13import java.util.Map;
14
15import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
16import org.openstreetmap.josm.gui.mappaint.StyleCache;
17
18/**
19 * This class can be used to save properties of OsmPrimitive.
20 *
21 * The main difference between PrimitiveData
22 * and OsmPrimitive is that PrimitiveData is not part of the dataset and changes in PrimitiveData are not
23 * reported by events
24 */
25public abstract class PrimitiveData extends AbstractPrimitive implements Serializable {
26
27 private static final long serialVersionUID = -1044837092478109138L;
28
29 /**
30 * Constructs a new {@code PrimitiveData}.
31 */
32 public PrimitiveData() {
33 this(OsmPrimitive.generateUniqueId());
34 }
35
36 /**
37 * Constructs a new {@code PrimitiveData} with given id.
38 * @param id id
39 * @since 12017
40 */
41 public PrimitiveData(long id) {
42 this.id = id;
43 }
44
45 /**
46 * Constructs a new {@code PrimitiveData} from an existing one.
47 * @param data the data to copy
48 */
49 public PrimitiveData(PrimitiveData data) {
50 cloneFrom(data);
51 }
52
53 /**
54 * Sets the primitive identifier.
55 * @param id primitive identifier
56 */
57 public void setId(long id) {
58 this.id = id;
59 }
60
61 /**
62 * Sets the primitive version.
63 * @param version primitive version
64 */
65 public void setVersion(int version) {
66 this.version = version;
67 }
68
69 /**
70 * override to make it public
71 */
72 @Override
73 public void setIncomplete(boolean incomplete) {
74 super.setIncomplete(incomplete);
75 }
76
77 /**
78 * Returns a copy of this primitive data.
79 * @return a copy of this primitive data
80 */
81 public abstract PrimitiveData makeCopy();
82
83 @Override
84 public String toString() {
85 StringBuilder builder = new StringBuilder();
86 builder.append(id).append(' ').append(Arrays.toString(keys)).append(' ').append(getFlagsAsString());
87 return builder.toString();
88 }
89
90 /**
91 * Returns a filtered list for a given primitive type.
92 * @param <T> primitive type
93 * @param list list to filter
94 * @param type primitive type
95 * @return a filtered list for given primitive type
96 */
97 @SuppressWarnings("unchecked")
98 public static <T extends PrimitiveData> List<T> getFilteredList(Collection<T> list, OsmPrimitiveType type) {
99 List<T> ret = new ArrayList<>();
100 for (PrimitiveData p: list) {
101 if (type.getDataClass().isInstance(p)) {
102 ret.add((T) p);
103 }
104 }
105 return ret;
106 }
107
108 @Override
109 protected final void keysChangedImpl(Map<String, String> originalKeys) {
110 }
111
112 private void writeObject(ObjectOutputStream oos) throws IOException {
113 // since super class is not Serializable
114 oos.writeLong(id);
115 oos.writeLong(user == null ? -1 : user.getId());
116 oos.writeInt(version);
117 oos.writeInt(changesetId);
118 oos.writeInt(timestamp);
119 oos.writeObject(keys);
120 oos.writeShort(flags);
121 oos.defaultWriteObject();
122 }
123
124 private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
125 // since super class is not Serializable
126 id = ois.readLong();
127 final long userId = ois.readLong();
128 user = userId == -1 ? null : User.getById(userId);
129 version = ois.readInt();
130 changesetId = ois.readInt();
131 timestamp = ois.readInt();
132 keys = (String[]) ois.readObject();
133 flags = ois.readShort();
134 ois.defaultReadObject();
135 }
136
137 @Override
138 public boolean isTagged() {
139 return hasKeys();
140 }
141
142 @Override
143 public boolean isAnnotated() {
144 return false;
145 }
146
147 @Override
148 public boolean hasDirectionKeys() {
149 return false;
150 }
151
152 @Override
153 public boolean reversedDirection() {
154 return false;
155 }
156
157 @Override
158 public void setHighlighted(boolean highlighted) {
159 // Override if needed
160 }
161
162 @Override
163 public boolean isHighlighted() {
164 return false;
165 }
166
167 @Override
168 public final List<PrimitiveData> getReferrers(boolean allowWithoutDataset) {
169 return Collections.emptyList();
170 }
171
172 @Override
173 public void visitReferrers(PrimitiveVisitor visitor) {
174 // Override if needed
175 }
176
177 @Override
178 public OsmData<?, ?, ?, ?> getDataSet() {
179 return null;
180 }
181
182 @Override
183 public StyleCache getCachedStyle() {
184 return null;
185 }
186
187 @Override
188 public void setCachedStyle(StyleCache mappaintStyle) {
189 // Override if needed
190 }
191
192 @Override
193 public boolean isCachedStyleUpToDate() {
194 return false;
195 }
196
197 @Override
198 public void declareCachedStyleUpToDate() {
199 // Override if needed
200 }
201}
Note: See TracBrowser for help on using the repository browser.