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