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