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