001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.osm.history;
003
004import java.time.Instant;
005
006import org.openstreetmap.josm.data.coor.LatLon;
007import org.openstreetmap.josm.data.osm.Node;
008import org.openstreetmap.josm.data.osm.NodeData;
009import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
010import org.openstreetmap.josm.data.osm.User;
011
012/**
013 * Represents an immutable OSM node in the context of a historical view on OSM data.
014 * @since 1670
015 */
016public class HistoryNode extends HistoryOsmPrimitive {
017
018    /** the coordinates. May be null for deleted nodes */
019    private LatLon coords;
020
021    /**
022     * Constructs a new {@code HistoryNode}.
023     *
024     * @param id the id (> 0 required)
025     * @param version the version (> 0 required)
026     * @param visible whether the node is still visible
027     * @param user the user (!= null required)
028     * @param changesetId the changeset id (> 0 required)
029     * @param timestamp the timestamp (!= null required)
030     * @param coords the coordinates
031     * @throws IllegalArgumentException if preconditions are violated
032     */
033    public HistoryNode(long id, long version, boolean visible, User user, long changesetId, Instant timestamp, LatLon coords) {
034        this(id, version, visible, user, changesetId, timestamp, coords, true);
035    }
036
037    /**
038     * Constructs a new {@code HistoryNode} with a configurable checking of historic parameters.
039     * This is needed to build virtual HistoryNodes for modified nodes, which do not have a timestamp and a changeset id.
040     *
041     * @param id the id (> 0 required)
042     * @param version the version (> 0 required)
043     * @param visible whether the node is still visible
044     * @param user the user (!= null required)
045     * @param changesetId the changeset id (> 0 required if {@code checkHistoricParams} is true)
046     * @param timestamp the timestamp (!= null required if {@code checkHistoricParams} is true)
047     * @param coords the coordinates
048     * @param checkHistoricParams if true, checks values of {@code changesetId} and {@code timestamp}
049     * @throws IllegalArgumentException if preconditions are violated
050     * @since 5440
051     */
052    public HistoryNode(long id, long version, boolean visible, User user, long changesetId, Instant timestamp, LatLon coords,
053            boolean checkHistoricParams) {
054        super(id, version, visible, user, changesetId, timestamp, checkHistoricParams);
055        setCoords(coords);
056    }
057
058    /**
059     * Constructs a new {@code HistoryNode} from an existing {@link Node}.
060     * @param n the node
061     */
062    public HistoryNode(Node n) {
063        super(n);
064        setCoords(n.getCoor());
065    }
066
067    @Override
068    public OsmPrimitiveType getType() {
069        return OsmPrimitiveType.NODE;
070    }
071
072    /**
073     * Replies the coordinates. May be null.
074     * @return the coordinates. May be null.
075     */
076    public final LatLon getCoords() {
077        return coords;
078    }
079
080    /**
081     * Sets the coordinates. Can be null.
082     * @param coords the coordinates. Can be null.
083     */
084    public final void setCoords(LatLon coords) {
085        this.coords = coords;
086    }
087
088    @Override
089    public String getDisplayName(HistoryNameFormatter formatter) {
090        return formatter.format(this);
091    }
092
093    /**
094     * Fills the node attributes with values from this history.
095     * @param data node data to fill
096     * @return filled node data
097     * @since 11878
098     */
099    public NodeData fillPrimitiveData(NodeData data) {
100        super.fillPrimitiveCommonData(data);
101        data.setCoor(coords);
102        return data;
103    }
104}