001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.osm;
003
004import java.io.IOException;
005import java.io.ObjectInputStream;
006import java.io.ObjectOutputStream;
007import java.io.Serializable;
008import java.util.Arrays;
009import java.util.Collections;
010import java.util.List;
011import java.util.Map;
012
013import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
014import org.openstreetmap.josm.gui.mappaint.StyleCache;
015
016/**
017 * This class can be used to save properties of OsmPrimitive.
018 *
019 * The main difference between PrimitiveData
020 * and OsmPrimitive is that PrimitiveData is not part of the dataset and changes in PrimitiveData are not
021 * reported by events
022 */
023public abstract class PrimitiveData extends AbstractPrimitive implements Serializable {
024
025    private static final long serialVersionUID = -1044837092478109138L;
026
027    /**
028     * Constructs a new {@code PrimitiveData} with given id.
029     * @param id id
030     * @since 12017
031     */
032    protected PrimitiveData(long id) {
033        this.id = id;
034    }
035
036    /**
037     * Constructs a new {@code PrimitiveData} from an existing one.
038     * @param data the data to copy
039     */
040    protected PrimitiveData(PrimitiveData data) {
041        cloneFrom(data);
042    }
043
044    /**
045     * Sets the primitive identifier.
046     * @param id primitive identifier
047     */
048    public void setId(long id) {
049        this.id = id;
050    }
051
052    /**
053     * Sets the primitive version.
054     * @param version primitive version
055     */
056    public void setVersion(int version) {
057        this.version = version;
058    }
059
060    /**
061     * override to make it public
062     */
063    @Override
064    public void setIncomplete(boolean incomplete) {
065        super.setIncomplete(incomplete);
066    }
067
068    /**
069     * Returns a copy of this primitive data.
070     * @return a copy of this primitive data
071     */
072    public abstract PrimitiveData makeCopy();
073
074    @Override
075    public String toString() {
076        StringBuilder builder = new StringBuilder();
077        builder.append(id).append(' ').append(Arrays.toString(keys)).append(' ').append(getFlagsAsString());
078        return builder.toString();
079    }
080
081    @Override
082    protected final void keysChangedImpl(Map<String, String> originalKeys) {
083    }
084
085    private void writeObject(ObjectOutputStream oos) throws IOException {
086        // since super class is not Serializable
087        oos.writeLong(id);
088        oos.writeLong(user == null ? -1 : user.getId());
089        oos.writeInt(version);
090        oos.writeInt(changesetId);
091        oos.writeInt(timestamp);
092        oos.writeObject(keys);
093        oos.writeShort(flags);
094        oos.defaultWriteObject();
095    }
096
097    private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
098        // since super class is not Serializable
099        id = ois.readLong();
100        final long userId = ois.readLong();
101        user = userId == -1 ? null : User.getById(userId);
102        version = ois.readInt();
103        changesetId = ois.readInt();
104        timestamp = ois.readInt();
105        keys = (String[]) ois.readObject();
106        flags = ois.readShort();
107        ois.defaultReadObject();
108    }
109
110    @Override
111    public boolean isTagged() {
112        return hasKeys();
113    }
114
115    @Override
116    public boolean isAnnotated() {
117        return false;
118    }
119
120    @Override
121    public boolean hasDirectionKeys() {
122        return false;
123    }
124
125    @Override
126    public boolean reversedDirection() {
127        return false;
128    }
129
130    @Override
131    public void setHighlighted(boolean highlighted) {
132        // Override if needed
133    }
134
135    @Override
136    public boolean isHighlighted() {
137        return false;
138    }
139
140    @Override
141    public final List<PrimitiveData> getReferrers(boolean allowWithoutDataset) {
142        return Collections.emptyList();
143    }
144
145    @Override
146    public void visitReferrers(PrimitiveVisitor visitor) {
147        // Override if needed
148    }
149
150    @Override
151    public OsmData<?, ?, ?, ?> getDataSet() {
152        return null;
153    }
154
155    @Override
156    public StyleCache getCachedStyle() {
157        return null;
158    }
159
160    @Override
161    public void setCachedStyle(StyleCache mappaintStyle) {
162        // Override if needed
163    }
164
165    @Override
166    public boolean isCachedStyleUpToDate() {
167        return false;
168    }
169
170    @Override
171    public void declareCachedStyleUpToDate() {
172        // Override if needed
173    }
174}