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

Last change on this file since 11993 was 10386, checked in by Don-vip, 8 years ago

gsoc-core - remove more deprecation warnings

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