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 | /**
|
---|
15 | * This class can be used to save properties of OsmPrimitive.
|
---|
16 | *
|
---|
17 | * The main difference between PrimitiveData
|
---|
18 | * and OsmPrimitive is that PrimitiveData is not part of the dataset and changes in PrimitiveData are not
|
---|
19 | * reported by events
|
---|
20 | */
|
---|
21 | public abstract class PrimitiveData extends AbstractPrimitive implements Serializable {
|
---|
22 |
|
---|
23 | private static final long serialVersionUID = -1044837092478109138L;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Constructs a new {@code PrimitiveData}.
|
---|
27 | */
|
---|
28 | public PrimitiveData() {
|
---|
29 | id = OsmPrimitive.generateUniqueId();
|
---|
30 | }
|
---|
31 |
|
---|
32 | public PrimitiveData(PrimitiveData data) {
|
---|
33 | cloneFrom(data);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public void setId(long id) {
|
---|
37 | this.id = id;
|
---|
38 | }
|
---|
39 |
|
---|
40 | public void setVersion(int version) {
|
---|
41 | this.version = version;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * override to make it public
|
---|
46 | */
|
---|
47 | @Override
|
---|
48 | public void setIncomplete(boolean incomplete) {
|
---|
49 | super.setIncomplete(incomplete);
|
---|
50 | }
|
---|
51 |
|
---|
52 | public abstract PrimitiveData makeCopy();
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public String toString() {
|
---|
56 | StringBuilder builder = new StringBuilder();
|
---|
57 | builder.append(id).append(Arrays.toString(keys)).append(getFlagsAsString());
|
---|
58 | return builder.toString();
|
---|
59 | }
|
---|
60 |
|
---|
61 | @SuppressWarnings("unchecked")
|
---|
62 | public static <T extends PrimitiveData> List<T> getFilteredList(Collection<T> list, OsmPrimitiveType type) {
|
---|
63 | List<T> ret = new ArrayList<>();
|
---|
64 | for (PrimitiveData p: list) {
|
---|
65 | if (type.getDataClass().isInstance(p)) {
|
---|
66 | ret.add((T) p);
|
---|
67 | }
|
---|
68 | }
|
---|
69 | return ret;
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | protected final void keysChangedImpl(Map<String, String> originalKeys) {
|
---|
74 | }
|
---|
75 |
|
---|
76 | private void writeObject(ObjectOutputStream oos) throws IOException {
|
---|
77 | // since super class is not Serializable
|
---|
78 | oos.writeLong(id);
|
---|
79 | oos.writeLong(user == null ? -1 : user.getId());
|
---|
80 | oos.writeInt(version);
|
---|
81 | oos.writeInt(changesetId);
|
---|
82 | oos.writeInt(timestamp);
|
---|
83 | oos.defaultWriteObject();
|
---|
84 | }
|
---|
85 |
|
---|
86 | private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
|
---|
87 | // since super class is not Serializable
|
---|
88 | id = ois.readLong();
|
---|
89 | final long userId = ois.readLong();
|
---|
90 | user = userId == -1 ? null : User.getById(userId);
|
---|
91 | version = ois.readInt();
|
---|
92 | changesetId = ois.readInt();
|
---|
93 | timestamp = ois.readInt();
|
---|
94 | ois.defaultReadObject();
|
---|
95 | }
|
---|
96 | }
|
---|