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

Last change on this file since 11632 was 11115, checked in by simon04, 8 years ago

fix #13785 - Use streams, add unit tests (patch by alno, modified)

  • Property svn:eol-style set to native
File size: 6.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.util.HashMap;
5import java.util.Iterator;
6import java.util.Map;
7import java.util.Map.Entry;
8import java.util.Set;
9import java.util.stream.Collectors;
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 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 return modificationTypes.entrySet().stream()
120 .filter(entry -> entry.getValue().equals(cmt))
121 .map(entry -> primitives.get(entry.getKey()))
122 .collect(Collectors.toSet());
123 }
124
125 /**
126 * Replies the number of objects in the dataset
127 *
128 * @return the number of objects in the dataset
129 */
130 public int size() {
131 return primitives.size();
132 }
133
134 /**
135 * Replies the {@link HistoryOsmPrimitive} with id <code>id</code> from this
136 * dataset. null, if there is no such primitive in the data set.
137 *
138 * @param id the id
139 * @return the {@link HistoryOsmPrimitive} with id <code>id</code> from this
140 * dataset
141 */
142 public HistoryOsmPrimitive getPrimitive(PrimitiveId id) {
143 if (id == null) return null;
144 return primitives.get(id);
145 }
146
147 public Iterator<ChangesetDataSetEntry> iterator() {
148 return new DefaultIterator();
149 }
150
151 private static class DefaultChangesetDataSetEntry implements ChangesetDataSetEntry {
152 private final ChangesetModificationType modificationType;
153 private final HistoryOsmPrimitive primitive;
154
155 DefaultChangesetDataSetEntry(ChangesetModificationType modificationType, HistoryOsmPrimitive primitive) {
156 this.modificationType = modificationType;
157 this.primitive = primitive;
158 }
159
160 @Override
161 public ChangesetModificationType getModificationType() {
162 return modificationType;
163 }
164
165 @Override
166 public HistoryOsmPrimitive getPrimitive() {
167 return primitive;
168 }
169 }
170
171 private class DefaultIterator implements Iterator<ChangesetDataSetEntry> {
172 private final Iterator<Entry<PrimitiveId, ChangesetModificationType>> typeIterator;
173
174 DefaultIterator() {
175 typeIterator = modificationTypes.entrySet().iterator();
176 }
177
178 @Override
179 public boolean hasNext() {
180 return typeIterator.hasNext();
181 }
182
183 @Override
184 public ChangesetDataSetEntry next() {
185 Entry<PrimitiveId, ChangesetModificationType> next = typeIterator.next();
186 ChangesetModificationType type = next.getValue();
187 HistoryOsmPrimitive primitive = primitives.get(next.getKey());
188 return new DefaultChangesetDataSetEntry(type, primitive);
189 }
190
191 @Override
192 public void remove() {
193 throw new UnsupportedOperationException();
194 }
195 }
196}
Note: See TracBrowser for help on using the repository browser.