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

Last change on this file since 15820 was 15820, checked in by Don-vip, 4 years ago

fix #18654 - Separate unique identifiers per primitive type

This allows to easily update .osm files with negative ids across multiple sessions, such as internal JOSM boundaries file.

  • Property svn:eol-style set to native
File size: 4.9 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} with given id.
31 * @param id id
32 * @since 12017
33 */
34 public PrimitiveData(long id) {
35 this.id = id;
36 }
37
38 /**
39 * Constructs a new {@code PrimitiveData} from an existing one.
40 * @param data the data to copy
41 */
42 public PrimitiveData(PrimitiveData data) {
43 cloneFrom(data);
44 }
45
46 /**
47 * Sets the primitive identifier.
48 * @param id primitive identifier
49 */
50 public void setId(long id) {
51 this.id = id;
52 }
53
54 /**
55 * Sets the primitive version.
56 * @param version primitive version
57 */
58 public void setVersion(int version) {
59 this.version = version;
60 }
61
62 /**
63 * override to make it public
64 */
65 @Override
66 public void setIncomplete(boolean incomplete) {
67 super.setIncomplete(incomplete);
68 }
69
70 /**
71 * Returns a copy of this primitive data.
72 * @return a copy of this primitive data
73 */
74 public abstract PrimitiveData makeCopy();
75
76 @Override
77 public String toString() {
78 StringBuilder builder = new StringBuilder();
79 builder.append(id).append(' ').append(Arrays.toString(keys)).append(' ').append(getFlagsAsString());
80 return builder.toString();
81 }
82
83 /**
84 * Returns a filtered list for a given primitive type.
85 * @param <T> primitive type
86 * @param list list to filter
87 * @param type primitive type
88 * @return a filtered list for given primitive type
89 */
90 @SuppressWarnings("unchecked")
91 public static <T extends PrimitiveData> List<T> getFilteredList(Collection<T> list, OsmPrimitiveType type) {
92 List<T> ret = new ArrayList<>();
93 for (PrimitiveData p: list) {
94 if (type.getDataClass().isInstance(p)) {
95 ret.add((T) p);
96 }
97 }
98 return ret;
99 }
100
101 @Override
102 protected final void keysChangedImpl(Map<String, String> originalKeys) {
103 }
104
105 private void writeObject(ObjectOutputStream oos) throws IOException {
106 // since super class is not Serializable
107 oos.writeLong(id);
108 oos.writeLong(user == null ? -1 : user.getId());
109 oos.writeInt(version);
110 oos.writeInt(changesetId);
111 oos.writeInt(timestamp);
112 oos.writeObject(keys);
113 oos.writeShort(flags);
114 oos.defaultWriteObject();
115 }
116
117 private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
118 // since super class is not Serializable
119 id = ois.readLong();
120 final long userId = ois.readLong();
121 user = userId == -1 ? null : User.getById(userId);
122 version = ois.readInt();
123 changesetId = ois.readInt();
124 timestamp = ois.readInt();
125 keys = (String[]) ois.readObject();
126 flags = ois.readShort();
127 ois.defaultReadObject();
128 }
129
130 @Override
131 public boolean isTagged() {
132 return hasKeys();
133 }
134
135 @Override
136 public boolean isAnnotated() {
137 return false;
138 }
139
140 @Override
141 public boolean hasDirectionKeys() {
142 return false;
143 }
144
145 @Override
146 public boolean reversedDirection() {
147 return false;
148 }
149
150 @Override
151 public void setHighlighted(boolean highlighted) {
152 // Override if needed
153 }
154
155 @Override
156 public boolean isHighlighted() {
157 return false;
158 }
159
160 @Override
161 public final List<PrimitiveData> getReferrers(boolean allowWithoutDataset) {
162 return Collections.emptyList();
163 }
164
165 @Override
166 public void visitReferrers(PrimitiveVisitor visitor) {
167 // Override if needed
168 }
169
170 @Override
171 public OsmData<?, ?, ?, ?> getDataSet() {
172 return null;
173 }
174
175 @Override
176 public StyleCache getCachedStyle() {
177 return null;
178 }
179
180 @Override
181 public void setCachedStyle(StyleCache mappaintStyle) {
182 // Override if needed
183 }
184
185 @Override
186 public boolean isCachedStyleUpToDate() {
187 return false;
188 }
189
190 @Override
191 public void declareCachedStyleUpToDate() {
192 // Override if needed
193 }
194}
Note: See TracBrowser for help on using the repository browser.