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.ArrayList;
009import java.util.Arrays;
010import java.util.Collection;
011import java.util.List;
012import java.util.Map;
013
014/**
015 * This class can be used to save properties of OsmPrimitive.
016 *
017 * The main difference between PrimitiveData
018 * and OsmPrimitive is that PrimitiveData is not part of the dataset and changes in PrimitiveData are not
019 * reported by events
020 */
021public abstract class PrimitiveData extends AbstractPrimitive implements Serializable {
022
023    private static final long serialVersionUID = -1044837092478109138L;
024
025    /**
026     * Constructs a new {@code PrimitiveData}.
027     */
028    public PrimitiveData() {
029        id = OsmPrimitive.generateUniqueId();
030    }
031
032    /**
033     * Constructs a new {@code PrimitiveData} from an existing one.
034     * @param data the data to copy
035     */
036    public PrimitiveData(PrimitiveData data) {
037        cloneFrom(data);
038    }
039
040    /**
041     * Sets the primitive identifier.
042     * @param id primitive identifier
043     */
044    public void setId(long id) {
045        this.id = id;
046    }
047
048    /**
049     * Sets the primitive version.
050     * @param version primitive version
051     */
052    public void setVersion(int version) {
053        this.version = version;
054    }
055
056    /**
057     * override to make it public
058     */
059    @Override
060    public void setIncomplete(boolean incomplete) {
061        super.setIncomplete(incomplete);
062    }
063
064    /**
065     * Returns a copy of this primitive data.
066     * @return a copy of this primitive data
067     */
068    public abstract PrimitiveData makeCopy();
069
070    @Override
071    public String toString() {
072        StringBuilder builder = new StringBuilder();
073        builder.append(id).append(' ').append(Arrays.toString(keys)).append(' ').append(getFlagsAsString());
074        return builder.toString();
075    }
076
077    @SuppressWarnings("unchecked")
078    public static <T extends PrimitiveData> List<T> getFilteredList(Collection<T> list, OsmPrimitiveType type) {
079        List<T> ret = new ArrayList<>();
080        for (PrimitiveData p: list) {
081            if (type.getDataClass().isInstance(p)) {
082                ret.add((T) p);
083            }
084        }
085        return ret;
086    }
087
088    @Override
089    protected final void keysChangedImpl(Map<String, String> originalKeys) {
090    }
091
092    private void writeObject(ObjectOutputStream oos) throws IOException {
093        // since super class is not Serializable
094        oos.writeLong(id);
095        oos.writeLong(user == null ? -1 : user.getId());
096        oos.writeInt(version);
097        oos.writeInt(changesetId);
098        oos.writeInt(timestamp);
099        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}