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