1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm.history;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.text.MessageFormat;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
12 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
13 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
14 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * A data set holding histories of OSM primitives.
|
---|
18 | *
|
---|
19 | *
|
---|
20 | */
|
---|
21 | public class HistoryDataSet {
|
---|
22 | //private final static Logger logger = Logger.getLogger(HistoryDataSet.class.getName());
|
---|
23 |
|
---|
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 | }
|
---|
36 | return historyDataSet;
|
---|
37 | }
|
---|
38 |
|
---|
39 | /** the history data */
|
---|
40 | private HashMap<PrimitiveId, ArrayList<HistoryOsmPrimitive>> data;
|
---|
41 | private CopyOnWriteArrayList<HistoryDataSetListener> listeners;
|
---|
42 |
|
---|
43 | public HistoryDataSet() {
|
---|
44 | data = new HashMap<PrimitiveId, ArrayList<HistoryOsmPrimitive>>();
|
---|
45 | listeners = new CopyOnWriteArrayList<HistoryDataSetListener>();
|
---|
46 | }
|
---|
47 |
|
---|
48 | public void addHistoryDataSetListener(HistoryDataSetListener listener) {
|
---|
49 | if (listener != null) {
|
---|
50 | listeners.addIfAbsent(listener);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | public void removeHistoryDataSetListener(HistoryDataSetListener listener) {
|
---|
55 | listeners.remove(listener);
|
---|
56 | }
|
---|
57 |
|
---|
58 | protected void fireHistoryUpdated(SimplePrimitiveId id) {
|
---|
59 | for (HistoryDataSetListener l : listeners) {
|
---|
60 | l.historyUpdated(this, id);
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Replies the history primitive for the primitive with id <code>id</code>
|
---|
66 | * and version <code>version</code>. null, if no such primitive exists.
|
---|
67 | *
|
---|
68 | * @param id the id of the primitive. > 0 required.
|
---|
69 | * @param type the primitive type. Must not be null.
|
---|
70 | * @param version the version of the primitive. > 0 required
|
---|
71 | * @return the history primitive for the primitive with id <code>id</code>,
|
---|
72 | * type <code>type</code>, and version <code>version</code>
|
---|
73 | */
|
---|
74 | public HistoryOsmPrimitive get(long id, OsmPrimitiveType type, long version){
|
---|
75 | if (id <= 0)
|
---|
76 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected, got {1}", "id", id));
|
---|
77 | CheckParameterUtil.ensureParameterNotNull(type, "type");
|
---|
78 | if (version <= 0)
|
---|
79 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected, got {1}", "version", version));
|
---|
80 |
|
---|
81 | SimplePrimitiveId pid = new SimplePrimitiveId(id, type);
|
---|
82 | ArrayList<HistoryOsmPrimitive> versions = data.get(pid);
|
---|
83 | if (versions == null)
|
---|
84 | return null;
|
---|
85 | for (HistoryOsmPrimitive primitive: versions) {
|
---|
86 | if (primitive.matches(id, version))
|
---|
87 | return primitive;
|
---|
88 | }
|
---|
89 | return null;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Adds a history primitive to the data set
|
---|
94 | *
|
---|
95 | * @param primitive the history primitive to add
|
---|
96 | */
|
---|
97 | public void put(HistoryOsmPrimitive primitive) {
|
---|
98 | SimplePrimitiveId id = new SimplePrimitiveId(primitive.getId(), primitive.getType());
|
---|
99 | if (data.get(id) == null) {
|
---|
100 | data.put(id, new ArrayList<HistoryOsmPrimitive>());
|
---|
101 | }
|
---|
102 | data.get(id).add(primitive);
|
---|
103 | fireHistoryUpdated(id);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Replies the history for a given primitive with id <code>id</code>
|
---|
108 | * and type <code>type</code>.
|
---|
109 | *
|
---|
110 | * @param id the id the if of the primitive. > 0 required
|
---|
111 | * @param type the type of the primitive. Must not be null.
|
---|
112 | * @return the history. null, if there isn't a history for <code>id</code> and
|
---|
113 | * <code>type</code>.
|
---|
114 | * @throws IllegalArgumentException thrown if id <= 0
|
---|
115 | * @throws IllegalArgumentException thrown if type is null
|
---|
116 | */
|
---|
117 | public History getHistory(long id, OsmPrimitiveType type) throws IllegalArgumentException{
|
---|
118 | if (id <= 0)
|
---|
119 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected, got {1}", "id", id));
|
---|
120 | CheckParameterUtil.ensureParameterNotNull(type, "type");
|
---|
121 | SimplePrimitiveId pid = new SimplePrimitiveId(id, type);
|
---|
122 | return getHistory(pid);
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Replies the history for a primitive with id <code>id</code>. null, if no
|
---|
127 | * such history exists.
|
---|
128 | *
|
---|
129 | * @param pid the primitive id. Must not be null.
|
---|
130 | * @return the history for a primitive with id <code>id</code>. null, if no
|
---|
131 | * such history exists
|
---|
132 | * @throws IllegalArgumentException thrown if pid is null
|
---|
133 | */
|
---|
134 | public History getHistory(PrimitiveId pid) throws IllegalArgumentException{
|
---|
135 | CheckParameterUtil.ensureParameterNotNull(pid, "pid");
|
---|
136 | ArrayList<HistoryOsmPrimitive> versions = data.get(pid);
|
---|
137 | if (versions == null)
|
---|
138 | return null;
|
---|
139 | return new History(pid.getUniqueId(), pid.getType(), versions);
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * merges the histories from the {@see HistoryDataSet} other in this history data set
|
---|
144 | *
|
---|
145 | * @param other the other history data set. Ignored if null.
|
---|
146 | */
|
---|
147 | public void mergeInto(HistoryDataSet other) {
|
---|
148 | if (other == null)
|
---|
149 | return;
|
---|
150 | for (PrimitiveId id : other.data.keySet()) {
|
---|
151 | this.data.put(id, other.data.get(id));
|
---|
152 | }
|
---|
153 | fireHistoryUpdated(null);
|
---|
154 | }
|
---|
155 | }
|
---|