| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.data.osm.history;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.time.Instant;
|
|---|
| 7 | import java.util.ArrayList;
|
|---|
| 8 | import java.util.Collections;
|
|---|
| 9 | import java.util.List;
|
|---|
| 10 | import java.util.Objects;
|
|---|
| 11 |
|
|---|
| 12 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
|---|
| 13 | import org.openstreetmap.josm.data.osm.User;
|
|---|
| 14 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 15 | import org.openstreetmap.josm.data.osm.WayData;
|
|---|
| 16 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * Represents an immutable OSM way in the context of a historical view on OSM data.
|
|---|
| 20 | * @since 1670
|
|---|
| 21 | */
|
|---|
| 22 | public class HistoryWay extends HistoryOsmPrimitive {
|
|---|
| 23 |
|
|---|
| 24 | private final List<Long> nodeIds = new ArrayList<>();
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * Constructs a new {@code HistoryWay}.
|
|---|
| 28 | *
|
|---|
| 29 | * @param id the id (> 0 required)
|
|---|
| 30 | * @param version the version (> 0 required)
|
|---|
| 31 | * @param visible whether the node is still visible
|
|---|
| 32 | * @param user the user (!= null required)
|
|---|
| 33 | * @param changesetId the changeset id (> 0 required if {@code checkHistoricParams} is true)
|
|---|
| 34 | * @param timestamp the timestamp (!= null required if {@code checkHistoricParams} is true)
|
|---|
| 35 | * @throws IllegalArgumentException if preconditions are violated
|
|---|
| 36 | */
|
|---|
| 37 | public HistoryWay(long id, long version, boolean visible, User user, long changesetId, Instant timestamp) {
|
|---|
| 38 | super(id, version, visible, user, changesetId, timestamp);
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * Constructs a new {@code HistoryWay} with a configurable checking of historic parameters.
|
|---|
| 43 | * This is needed to build virtual HistoryWays for modified ways, which do not have a timestamp and a changeset id.
|
|---|
| 44 | *
|
|---|
| 45 | * @param id the id (> 0 required)
|
|---|
| 46 | * @param version the version (> 0 required)
|
|---|
| 47 | * @param visible whether the node is still visible
|
|---|
| 48 | * @param user the user (!= null required)
|
|---|
| 49 | * @param changesetId the changeset id (> 0 required if {@code checkHistoricParams} is true)
|
|---|
| 50 | * @param timestamp the timestamp (!= null required if {@code checkHistoricParams} is true)
|
|---|
| 51 | * @param checkHistoricParams if true, checks values of {@code changesetId} and {@code timestamp}
|
|---|
| 52 | * @throws IllegalArgumentException if preconditions are violated
|
|---|
| 53 | * @since 5440
|
|---|
| 54 | */
|
|---|
| 55 | public HistoryWay(long id, long version, boolean visible, User user, long changesetId, Instant timestamp, boolean checkHistoricParams) {
|
|---|
| 56 | super(id, version, visible, user, changesetId, timestamp, checkHistoricParams);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | /**
|
|---|
| 60 | * Constructs a new {@code HistoryWay} with a given list of node ids.
|
|---|
| 61 | *
|
|---|
| 62 | * @param id the id (> 0 required)
|
|---|
| 63 | * @param version the version (> 0 required)
|
|---|
| 64 | * @param visible whether the node is still visible
|
|---|
| 65 | * @param user the user (!= null required)
|
|---|
| 66 | * @param changesetId the changeset id (> 0 required if {@code checkHistoricParams} is true)
|
|---|
| 67 | * @param timestamp the timestamp (!= null required if {@code checkHistoricParams} is true)
|
|---|
| 68 | * @param nodeIdList the node ids (!= null required)
|
|---|
| 69 | * @throws IllegalArgumentException if preconditions are violated
|
|---|
| 70 | */
|
|---|
| 71 | public HistoryWay(long id, long version, boolean visible, User user, long changesetId, Instant timestamp, List<Long> nodeIdList) {
|
|---|
| 72 | this(id, version, visible, user, changesetId, timestamp);
|
|---|
| 73 | CheckParameterUtil.ensureParameterNotNull(nodeIdList, "nodeIdList");
|
|---|
| 74 | this.nodeIds.addAll(nodeIdList);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /**
|
|---|
| 78 | * Constructs a new {@code HistoryWay} from an existing {@link Way}.
|
|---|
| 79 | * @param w the way
|
|---|
| 80 | */
|
|---|
| 81 | public HistoryWay(Way w) {
|
|---|
| 82 | super(w);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * replies the number of nodes in this way
|
|---|
| 87 | * @return the number of nodes
|
|---|
| 88 | */
|
|---|
| 89 | public int getNumNodes() {
|
|---|
| 90 | return nodeIds.size();
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * replies the idx-th node id in the list of node ids of this way
|
|---|
| 95 | *
|
|---|
| 96 | * @param idx the index
|
|---|
| 97 | * @return the idx-th node id
|
|---|
| 98 | * @throws IndexOutOfBoundsException if idx < 0 || idx >= {#see {@link #getNumNodes()}
|
|---|
| 99 | */
|
|---|
| 100 | public long getNodeId(int idx) {
|
|---|
| 101 | if (idx < 0 || idx >= nodeIds.size())
|
|---|
| 102 | throw new IndexOutOfBoundsException(tr("Parameter {0} not in range 0..{1}. Got ''{2}''.", "idx", nodeIds.size(), idx));
|
|---|
| 103 | return nodeIds.get(idx);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /**
|
|---|
| 107 | * replies an immutable list of the ways node ids
|
|---|
| 108 | *
|
|---|
| 109 | * @return the ways node ids
|
|---|
| 110 | */
|
|---|
| 111 | public List<Long> getNodes() {
|
|---|
| 112 | return Collections.unmodifiableList(nodeIds);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /**
|
|---|
| 116 | * replies the ways type, i.e. {@link OsmPrimitiveType#WAY}
|
|---|
| 117 | *
|
|---|
| 118 | * @return the ways type
|
|---|
| 119 | */
|
|---|
| 120 | @Override
|
|---|
| 121 | public OsmPrimitiveType getType() {
|
|---|
| 122 | return OsmPrimitiveType.WAY;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /**
|
|---|
| 126 | * adds a node id to the list nodes of this way
|
|---|
| 127 | *
|
|---|
| 128 | * @param ref the node id to add
|
|---|
| 129 | */
|
|---|
| 130 | public void addNode(long ref) {
|
|---|
| 131 | nodeIds.add(ref);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /**
|
|---|
| 135 | * Replies true if this way is closed.
|
|---|
| 136 | *
|
|---|
| 137 | * @return true if this way is closed.
|
|---|
| 138 | */
|
|---|
| 139 | public boolean isClosed() {
|
|---|
| 140 | return getNumNodes() >= 3 && Objects.equals(nodeIds.get(0), nodeIds.get(nodeIds.size()-1));
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | @Override
|
|---|
| 144 | public String getDisplayName(HistoryNameFormatter formatter) {
|
|---|
| 145 | return formatter.format(this);
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | /**
|
|---|
| 149 | * Fills the way attributes with values from this history.
|
|---|
| 150 | * @param data way data to fill
|
|---|
| 151 | * @return filled way data
|
|---|
| 152 | * @since 11878
|
|---|
| 153 | */
|
|---|
| 154 | public WayData fillPrimitiveData(WayData data) {
|
|---|
| 155 | super.fillPrimitiveCommonData(data);
|
|---|
| 156 | data.setNodeIds(nodeIds);
|
|---|
| 157 | return data;
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|