source: josm/trunk/src/org/openstreetmap/josm/data/osm/history/HistoryDataSet.java@ 8338

Last change on this file since 8338 was 8338, checked in by Don-vip, 9 years ago

fix squid:S1319 - Declarations should use Java collection interfaces rather than specific implementation classes

  • Property svn:eol-style set to native
File size: 7.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm.history;
3
4import java.text.MessageFormat;
5import java.util.ArrayList;
6import java.util.Collection;
7import java.util.HashMap;
8import java.util.HashSet;
9import java.util.List;
10import java.util.Map;
11import java.util.Set;
12import java.util.concurrent.CopyOnWriteArrayList;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.data.osm.Changeset;
16import org.openstreetmap.josm.data.osm.IPrimitive;
17import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
18import org.openstreetmap.josm.data.osm.PrimitiveId;
19import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
20import org.openstreetmap.josm.gui.MapView;
21import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
22import org.openstreetmap.josm.gui.layer.Layer;
23import org.openstreetmap.josm.tools.CheckParameterUtil;
24
25/**
26 * A data set holding histories of OSM primitives.
27 * @since 1670
28 */
29public 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. &gt; 0 required.
87 * @param type the primitive type. Must not be null.
88 * @param version the version of the primitive. &gt; 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. &gt; 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 &lt;= 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}
Note: See TracBrowser for help on using the repository browser.