1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm.history;
|
---|
3 |
|
---|
4 | import java.text.MessageFormat;
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import java.util.HashSet;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Map;
|
---|
11 | import java.util.Set;
|
---|
12 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
16 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
18 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
19 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
20 | import org.openstreetmap.josm.gui.MapView;
|
---|
21 | import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
|
---|
22 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
23 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * A data set holding histories of OSM primitives.
|
---|
27 | * @since 1670
|
---|
28 | */
|
---|
29 | public class HistoryDataSet implements LayerChangeListener{
|
---|
30 | /** the unique instance */
|
---|
31 | private static HistoryDataSet historyDataSet;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Replies the unique instance of the history data set
|
---|
35 | *
|
---|
36 | * @return the unique instance of the history data set
|
---|
37 | */
|
---|
38 | public static synchronized HistoryDataSet getInstance() {
|
---|
39 | if (historyDataSet == null) {
|
---|
40 | historyDataSet = new HistoryDataSet();
|
---|
41 | MapView.addLayerChangeListener(historyDataSet);
|
---|
42 | }
|
---|
43 | return historyDataSet;
|
---|
44 | }
|
---|
45 |
|
---|
46 | /** the history data */
|
---|
47 | private Map<PrimitiveId, ArrayList<HistoryOsmPrimitive>> data;
|
---|
48 | private CopyOnWriteArrayList<HistoryDataSetListener> listeners;
|
---|
49 | private Map<Long, Changeset> changesets;
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Constructs a new {@code HistoryDataSet}.
|
---|
53 | */
|
---|
54 | public HistoryDataSet() {
|
---|
55 | data = new HashMap<>();
|
---|
56 | listeners = new CopyOnWriteArrayList<>();
|
---|
57 | changesets = new HashMap<>();
|
---|
58 | }
|
---|
59 |
|
---|
60 | public void addHistoryDataSetListener(HistoryDataSetListener listener) {
|
---|
61 | if (listener != null) {
|
---|
62 | listeners.addIfAbsent(listener);
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public void removeHistoryDataSetListener(HistoryDataSetListener listener) {
|
---|
67 | listeners.remove(listener);
|
---|
68 | }
|
---|
69 |
|
---|
70 | protected void fireHistoryUpdated(PrimitiveId id) {
|
---|
71 | for (HistoryDataSetListener l : listeners) {
|
---|
72 | l.historyUpdated(this, id);
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | protected void fireCacheCleared() {
|
---|
77 | for (HistoryDataSetListener l : listeners) {
|
---|
78 | l.historyDataSetCleared(this);
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Replies the history primitive for the primitive with id <code>id</code>
|
---|
84 | * and version <code>version</code>. null, if no such primitive exists.
|
---|
85 | *
|
---|
86 | * @param id the id of the primitive. > 0 required.
|
---|
87 | * @param type the primitive type. Must not be null.
|
---|
88 | * @param version the version of the primitive. > 0 required
|
---|
89 | * @return the history primitive for the primitive with id <code>id</code>,
|
---|
90 | * type <code>type</code>, and version <code>version</code>
|
---|
91 | */
|
---|
92 | public HistoryOsmPrimitive get(long id, OsmPrimitiveType type, long version){
|
---|
93 | if (id <= 0)
|
---|
94 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected, got {1}", "id", id));
|
---|
95 | CheckParameterUtil.ensureParameterNotNull(type, "type");
|
---|
96 | if (version <= 0)
|
---|
97 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected, got {1}", "version", version));
|
---|
98 |
|
---|
99 | SimplePrimitiveId pid = new SimplePrimitiveId(id, type);
|
---|
100 | List<HistoryOsmPrimitive> versions = data.get(pid);
|
---|
101 | if (versions == null)
|
---|
102 | return null;
|
---|
103 | for (HistoryOsmPrimitive primitive: versions) {
|
---|
104 | if (primitive.matches(id, version))
|
---|
105 | return primitive;
|
---|
106 | }
|
---|
107 | return null;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Adds a history primitive to the data set
|
---|
112 | *
|
---|
113 | * @param primitive the history primitive to add
|
---|
114 | */
|
---|
115 | public void put(HistoryOsmPrimitive primitive) {
|
---|
116 | PrimitiveId id = new SimplePrimitiveId(primitive.getId(), primitive.getType());
|
---|
117 | if (data.get(id) == null) {
|
---|
118 | data.put(id, new ArrayList<HistoryOsmPrimitive>());
|
---|
119 | }
|
---|
120 | data.get(id).add(primitive);
|
---|
121 | fireHistoryUpdated(id);
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Adds a changeset to the data set
|
---|
126 | *
|
---|
127 | * @param changeset the changeset to add
|
---|
128 | */
|
---|
129 | public void putChangeset(Changeset changeset) {
|
---|
130 | changesets.put((long) changeset.getId(), changeset);
|
---|
131 | fireHistoryUpdated(null);
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Replies the history for a given primitive with id <code>id</code>
|
---|
136 | * and type <code>type</code>.
|
---|
137 | *
|
---|
138 | * @param id the id the if of the primitive. > 0 required
|
---|
139 | * @param type the type of the primitive. Must not be null.
|
---|
140 | * @return the history. null, if there isn't a history for <code>id</code> and
|
---|
141 | * <code>type</code>.
|
---|
142 | * @throws IllegalArgumentException if id <= 0
|
---|
143 | * @throws IllegalArgumentException if type is null
|
---|
144 | */
|
---|
145 | public History getHistory(long id, OsmPrimitiveType type) {
|
---|
146 | if (id <= 0)
|
---|
147 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected, got {1}", "id", id));
|
---|
148 | CheckParameterUtil.ensureParameterNotNull(type, "type");
|
---|
149 | SimplePrimitiveId pid = new SimplePrimitiveId(id, type);
|
---|
150 | return getHistory(pid);
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Replies the history for a primitive with id <code>id</code>. null, if no
|
---|
155 | * such history exists.
|
---|
156 | *
|
---|
157 | * @param pid the primitive id. Must not be null.
|
---|
158 | * @return the history for a primitive with id <code>id</code>. null, if no
|
---|
159 | * such history exists
|
---|
160 | * @throws IllegalArgumentException if pid is null
|
---|
161 | */
|
---|
162 | public History getHistory(PrimitiveId pid) {
|
---|
163 | CheckParameterUtil.ensureParameterNotNull(pid, "pid");
|
---|
164 | List<HistoryOsmPrimitive> versions = data.get(pid);
|
---|
165 | if (versions == null && pid instanceof IPrimitive) {
|
---|
166 | versions = data.get(((IPrimitive) pid).getPrimitiveId());
|
---|
167 | }
|
---|
168 | if (versions == null)
|
---|
169 | return null;
|
---|
170 | for (HistoryOsmPrimitive i : versions) {
|
---|
171 | i.setChangeset(changesets.get(i.getChangesetId()));
|
---|
172 | }
|
---|
173 | return new History(pid.getUniqueId(), pid.getType(), versions);
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * merges the histories from the {@link HistoryDataSet} other in this history data set
|
---|
178 | *
|
---|
179 | * @param other the other history data set. Ignored if null.
|
---|
180 | */
|
---|
181 | public void mergeInto(HistoryDataSet other) {
|
---|
182 | if (other == null)
|
---|
183 | return;
|
---|
184 | this.data.putAll(other.data);
|
---|
185 | this.changesets.putAll(other.changesets);
|
---|
186 | fireHistoryUpdated(null);
|
---|
187 | }
|
---|
188 |
|
---|
189 | public Collection<Long> getChangesetIds() {
|
---|
190 | final Set<Long> ids = new HashSet<>();
|
---|
191 | for (Collection<HistoryOsmPrimitive> i : data.values()) {
|
---|
192 | for (HistoryOsmPrimitive j : i) {
|
---|
193 | ids.add(j.getChangesetId());
|
---|
194 | }
|
---|
195 | }
|
---|
196 | return ids;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /* ------------------------------------------------------------------------------ */
|
---|
200 | /* interface LayerChangeListener */
|
---|
201 | /* ------------------------------------------------------------------------------ */
|
---|
202 | @Override
|
---|
203 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {/* irrelevant in this context */}
|
---|
204 | @Override
|
---|
205 | public void layerAdded(Layer newLayer) {/* irrelevant in this context */}
|
---|
206 | @Override
|
---|
207 | public void layerRemoved(Layer oldLayer) {
|
---|
208 | if (!Main.isDisplayingMapView()) return;
|
---|
209 | if (Main.map.mapView.getNumLayers() == 0) {
|
---|
210 | data.clear();
|
---|
211 | fireCacheCleared();
|
---|
212 | }
|
---|
213 | }
|
---|
214 | }
|
---|