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