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

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

see #8465 - use diamond operator where applicable

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