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

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

*Data: new constructors with a given id

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