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

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

findbugs - EI_EXPOSE_REP2 + javadoc

  • Property svn:eol-style set to native
File size: 3.5 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 /**
78 * Returns a filtered list for a given primitive type.
79 * @param <T> primitive type
80 * @param list list to filter
81 * @param type primitive type
82 * @return a filtered list for given primitive type
83 */
84 @SuppressWarnings("unchecked")
85 public static <T extends PrimitiveData> List<T> getFilteredList(Collection<T> list, OsmPrimitiveType type) {
86 List<T> ret = new ArrayList<>();
87 for (PrimitiveData p: list) {
88 if (type.getDataClass().isInstance(p)) {
89 ret.add((T) p);
90 }
91 }
92 return ret;
93 }
94
95 @Override
96 protected final void keysChangedImpl(Map<String, String> originalKeys) {
97 }
98
99 private void writeObject(ObjectOutputStream oos) throws IOException {
100 // since super class is not Serializable
101 oos.writeLong(id);
102 oos.writeLong(user == null ? -1 : user.getId());
103 oos.writeInt(version);
104 oos.writeInt(changesetId);
105 oos.writeInt(timestamp);
106 oos.writeObject(keys);
107 oos.writeShort(flags);
108 oos.defaultWriteObject();
109 }
110
111 private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
112 // since super class is not Serializable
113 id = ois.readLong();
114 final long userId = ois.readLong();
115 user = userId == -1 ? null : User.getById(userId);
116 version = ois.readInt();
117 changesetId = ois.readInt();
118 timestamp = ois.readInt();
119 keys = (String[]) ois.readObject();
120 flags = ois.readShort();
121 ois.defaultReadObject();
122 }
123}
Note: See TracBrowser for help on using the repository browser.