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

Last change on this file since 11294 was 10946, checked in by Don-vip, 8 years ago

fix #13395 - fix serialization issue causing bugs in copy/paste

  • Property svn:eol-style set to native
File size: 3.3 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.List;
12import 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 */
21public 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 /**
33 * Constructs a new {@code PrimitiveData} from an existing one.
34 * @param data the data to copy
35 */
36 public PrimitiveData(PrimitiveData data) {
37 cloneFrom(data);
38 }
39
40 /**
41 * Sets the primitive identifier.
42 * @param id primitive identifier
43 */
44 public void setId(long id) {
45 this.id = id;
46 }
47
48 /**
49 * Sets the primitive version.
50 * @param version primitive version
51 */
52 public void setVersion(int version) {
53 this.version = version;
54 }
55
56 /**
57 * override to make it public
58 */
59 @Override
60 public void setIncomplete(boolean incomplete) {
61 super.setIncomplete(incomplete);
62 }
63
64 /**
65 * Returns a copy of this primitive data.
66 * @return a copy of this primitive data
67 */
68 public abstract PrimitiveData makeCopy();
69
70 @Override
71 public String toString() {
72 StringBuilder builder = new StringBuilder();
73 builder.append(id).append(' ').append(Arrays.toString(keys)).append(' ').append(getFlagsAsString());
74 return builder.toString();
75 }
76
77 @SuppressWarnings("unchecked")
78 public static <T extends PrimitiveData> List<T> getFilteredList(Collection<T> list, OsmPrimitiveType type) {
79 List<T> ret = new ArrayList<>();
80 for (PrimitiveData p: list) {
81 if (type.getDataClass().isInstance(p)) {
82 ret.add((T) p);
83 }
84 }
85 return ret;
86 }
87
88 @Override
89 protected final void keysChangedImpl(Map<String, String> originalKeys) {
90 }
91
92 private void writeObject(ObjectOutputStream oos) throws IOException {
93 // since super class is not Serializable
94 oos.writeLong(id);
95 oos.writeLong(user == null ? -1 : user.getId());
96 oos.writeInt(version);
97 oos.writeInt(changesetId);
98 oos.writeInt(timestamp);
99 oos.writeObject(keys);
100 oos.writeShort(flags);
101 oos.defaultWriteObject();
102 }
103
104 private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
105 // since super class is not Serializable
106 id = ois.readLong();
107 final long userId = ois.readLong();
108 user = userId == -1 ? null : User.getById(userId);
109 version = ois.readInt();
110 changesetId = ois.readInt();
111 timestamp = ois.readInt();
112 keys = (String[]) ois.readObject();
113 flags = ois.readShort();
114 ois.defaultReadObject();
115 }
116}
Note: See TracBrowser for help on using the repository browser.