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

Last change on this file since 1709 was 1709, checked in by Gubaer, 15 years ago

new: history feature implemented

File size: 2.3 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.util.ArrayList;
7import java.util.HashMap;
8import java.util.NoSuchElementException;
9
10public class HistoryDataSet {
11
12 private static HistoryDataSet historyDataSet;
13
14 public static HistoryDataSet getInstance() {
15 if (historyDataSet == null) {
16 historyDataSet = new HistoryDataSet();
17 }
18 return historyDataSet;
19 }
20
21 private HashMap<Long, ArrayList<HistoryOsmPrimitive>> data;
22
23 public HistoryDataSet() {
24 data = new HashMap<Long, ArrayList<HistoryOsmPrimitive>>();
25 }
26
27 public HistoryOsmPrimitive get(long id, long version) {
28 ArrayList<HistoryOsmPrimitive> versions = data.get(id);
29 if (versions == null)
30 throw new NoSuchElementException(tr("Didn't find an primitive with id {0} in this dataset", id));
31
32 for (HistoryOsmPrimitive primitive: versions) {
33 if (primitive.matches(id, version))
34 return primitive;
35 }
36 throw new NoSuchElementException(tr("Didn't find an primitive with id {0} and version {1} in this dataset", id, version));
37 }
38
39 public void put(HistoryOsmPrimitive primitive) {
40 if (data.get(primitive.getId()) == null) {
41 data.put(primitive.getId(), new ArrayList<HistoryOsmPrimitive>());
42 }
43 data.get(primitive.getId()).add(primitive);
44 }
45
46 /**
47 * Replies the history for a given primitive with id <code>id</code>
48 *
49 * @param id the id
50 * @return the history
51 */
52 public History getHistory(long id) {
53 ArrayList<HistoryOsmPrimitive> versions = data.get(id);
54 if (versions == null)
55 return null;
56 return new History(id, versions);
57 }
58
59 /**
60 * merges the histories from the {@see HistoryDataSet} other in this history data set
61 *
62 * @param other the other history data set. Ignored if null.
63 */
64 public void mergeInto(HistoryDataSet other) {
65 if (other == null)
66 return;
67 for (Long id : other.data.keySet()) {
68 if (!this.data.keySet().contains(id)) {
69 this.data.put(id, other.data.get(id));
70 }
71 }
72 }
73}
Note: See TracBrowser for help on using the repository browser.