source: josm/trunk/src/org/openstreetmap/josm/data/osm/ChangesetDataSet.java@ 2686

Last change on this file since 2686 was 2686, checked in by Gubaer, 14 years ago

Partial commit due to issue described in #4137
Breaks the build

File size: 6.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.util.HashMap;
5import java.util.HashSet;
6import java.util.Iterator;
7import java.util.Map;
8import java.util.Set;
9import java.util.Map.Entry;
10
11import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
12import org.openstreetmap.josm.tools.CheckParameterUtil;
13
14/**
15 * A ChangesetDataSet holds the content of a changeset.
16 */
17public class ChangesetDataSet {
18
19 public static enum ChangesetModificationType {
20 CREATED,
21 UPDATED,
22 DELETED
23 }
24
25 public static interface ChangesetDataSetEntry {
26 public ChangesetModificationType getModificationType();
27 public HistoryOsmPrimitive getPrimitive();
28 }
29
30 final private Map<PrimitiveId, HistoryOsmPrimitive> primitives = new HashMap<PrimitiveId, HistoryOsmPrimitive>();
31 final private Map<PrimitiveId, ChangesetModificationType> modificationTypes = new HashMap<PrimitiveId, ChangesetModificationType>();
32
33 /**
34 * Remembers a history primitive with the given modification type
35 *
36 * @param primitive the primitive. Must not be null.
37 * @param cmt the modification type. Must not be null.
38 * @throws IllegalArgumentException thrown if primitive is null
39 * @throws IllegalArgumentException thrown if cmt is null
40 */
41 public void put(HistoryOsmPrimitive primitive, ChangesetModificationType cmt) throws IllegalArgumentException{
42 CheckParameterUtil.ensureParameterNotNull(primitive,"primitive");
43 CheckParameterUtil.ensureParameterNotNull(cmt,"cmt");
44 primitives.put(primitive.getPrimitiveId(), primitive);
45 modificationTypes.put(primitive.getPrimitiveId(), cmt);
46 }
47
48 /**
49 * Replies true if the changeset content contains the object with primitive <code>id</code>.
50 * @param id the id.
51 * @return true if the changeset content contains the object with primitive <code>id</code>
52 */
53 public boolean contains(PrimitiveId id) {
54 if (id == null) return false;
55 return primitives.containsKey(id);
56 }
57
58 /**
59 * Replies the modification type for the object with id <code>id</code>. Replies null, if id is null or
60 * if the object with id <code>id</code> isn't in the changeset content.
61 *
62 * @param id the id
63 * @return the modification type
64 */
65 public ChangesetModificationType getModificationType(PrimitiveId id) {
66 if (!contains(id)) return null;
67 return modificationTypes.get(id);
68 }
69
70 /**
71 * Replies true if the primitive with id <code>id</code> was created in this
72 * changeset. Replies false, if id is null.
73 *
74 * @param id the id
75 * @return true if the primitive with id <code>id</code> was created in this
76 * changeset.
77 */
78 public boolean isCreated(PrimitiveId id) {
79 if (!contains(id)) return false;
80 return ChangesetModificationType.CREATED.equals(getModificationType(id));
81 }
82
83 /**
84 * Replies true if the primitive with id <code>id</code> was updated in this
85 * changeset. Replies false, if id is null.
86 *
87 * @param id the id
88 * @return true if the primitive with id <code>id</code> was updated in this
89 * changeset.
90 */
91 public boolean isUpdated(PrimitiveId id) {
92 if (!contains(id)) return false;
93 return ChangesetModificationType.UPDATED.equals(getModificationType(id));
94 }
95
96 /**
97 * Replies true if the primitive with id <code>id</code> was deleted in this
98 * changeset. Replies false, if id is null.
99 *
100 * @param id the id
101 * @return true if the primitive with id <code>id</code> was deleted in this
102 * changeset.
103 */
104 public boolean isDeleted(PrimitiveId id) {
105 if (!contains(id)) return false;
106 return ChangesetModificationType.DELETED.equals(getModificationType(id));
107 }
108
109 /**
110 * Replies the set of primitives with a specific modification type
111 *
112 * @param cmt the modification type. Must not be null.
113 * @return the set of primitives
114 * @throws IllegalArgumentException thrown if cmt is null
115 */
116 public Set<HistoryOsmPrimitive> getPrimitivesByModificationType(ChangesetModificationType cmt) throws IllegalArgumentException {
117 CheckParameterUtil.ensureParameterNotNull(cmt,"cmt");
118 HashSet<HistoryOsmPrimitive> ret = new HashSet<HistoryOsmPrimitive>();
119 for (Entry<PrimitiveId, ChangesetModificationType> entry: modificationTypes.entrySet()) {
120 if (entry.getValue().equals(cmt)) {
121 ret.add(primitives.get(entry.getKey()));
122 }
123 }
124 return ret;
125 }
126
127 /**
128 * Replies the number of objects in the dataset
129 *
130 * @return the number of objects in the dataset
131 */
132 public int size() {
133 return primitives.size();
134 }
135
136 /**
137 * Replies the {@see HistoryOsmPrimitive} with id <code>id</code> from this
138 * dataset. null, if there is no such primitive in the data set.
139 *
140 * @param id the id
141 * @return the {@see HistoryOsmPrimitive} with id <code>id</code> from this
142 * dataset
143 */
144 public HistoryOsmPrimitive getPrimitive(PrimitiveId id) {
145 if (id == null) return null;
146 return primitives.get(id);
147 }
148
149
150 public Iterator<ChangesetDataSetEntry> iterator() {
151 return new DefaultIterator();
152 }
153
154 private static class DefaultChangesetDataSetEntry implements ChangesetDataSetEntry {
155 private ChangesetModificationType modificationType;
156 private HistoryOsmPrimitive primitive;
157
158 public DefaultChangesetDataSetEntry(ChangesetModificationType modificationType, HistoryOsmPrimitive primitive) {
159 this.modificationType = modificationType;
160 this.primitive = primitive;
161 }
162
163 public ChangesetModificationType getModificationType() {
164 return modificationType;
165 }
166
167 public HistoryOsmPrimitive getPrimitive() {
168 return primitive;
169 }
170 }
171
172 private class DefaultIterator implements Iterator<ChangesetDataSetEntry> {
173 private Iterator<Entry<PrimitiveId, ChangesetModificationType>> typeIterator;
174
175 public DefaultIterator() {
176 typeIterator = modificationTypes.entrySet().iterator();
177 }
178
179 public boolean hasNext() {
180 return typeIterator.hasNext();
181 }
182
183 public ChangesetDataSetEntry next() {
184 Entry<PrimitiveId, ChangesetModificationType> next = typeIterator.next();
185 ChangesetModificationType type = next.getValue();
186 HistoryOsmPrimitive primitive = primitives.get(next.getKey());
187 return new DefaultChangesetDataSetEntry(type, primitive);
188 }
189
190 public void remove() {
191 throw new UnsupportedOperationException();
192 }
193 }
194}
Note: See TracBrowser for help on using the repository browser.