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

Last change on this file since 2845 was 2845, checked in by mjulius, 14 years ago

fix messages for data

File size: 5.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm.history;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.text.MessageFormat;
7import java.util.ArrayList;
8import java.util.HashMap;
9import java.util.concurrent.CopyOnWriteArrayList;
10
11import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
12import org.openstreetmap.josm.data.osm.PrimitiveId;
13import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
14import org.openstreetmap.josm.tools.CheckParameterUtil;
15
16/**
17 * A data set holding histories of OSM primitives.
18 *
19 *
20 */
21public 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}
Note: See TracBrowser for help on using the repository browser.