source: josm/trunk/src/org/openstreetmap/josm/data/APIDataSet.java@ 8851

Last change on this file since 8851 was 8851, checked in by Don-vip, 9 years ago

sonar - squid:S1149 - Synchronized classes Vector, Hashtable, Stack and StringBuffer should not be used

  • Property svn:eol-style set to native
File size: 12.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data;
3
4import java.util.ArrayDeque;
5import java.util.ArrayList;
6import java.util.Collection;
7import java.util.Collections;
8import java.util.Comparator;
9import java.util.Deque;
10import java.util.HashMap;
11import java.util.HashSet;
12import java.util.LinkedList;
13import java.util.List;
14import java.util.Map;
15import java.util.Set;
16
17import org.openstreetmap.josm.actions.upload.CyclicUploadDependencyException;
18import org.openstreetmap.josm.data.conflict.Conflict;
19import org.openstreetmap.josm.data.conflict.ConflictCollection;
20import org.openstreetmap.josm.data.osm.DataSet;
21import org.openstreetmap.josm.data.osm.IPrimitive;
22import org.openstreetmap.josm.data.osm.Node;
23import org.openstreetmap.josm.data.osm.OsmPrimitive;
24import org.openstreetmap.josm.data.osm.OsmPrimitiveComparator;
25import org.openstreetmap.josm.data.osm.PrimitiveId;
26import org.openstreetmap.josm.data.osm.Relation;
27import org.openstreetmap.josm.data.osm.RelationMember;
28import org.openstreetmap.josm.data.osm.Way;
29import org.openstreetmap.josm.tools.Utils;
30
31/**
32 * Represents a collection of {@link OsmPrimitive}s which should be uploaded to the API.
33 * The collection is derived from the modified primitives of an {@link DataSet} and it provides methods
34 * for sorting the objects in upload order.
35 * @since 2025
36 */
37public class APIDataSet {
38 private List<OsmPrimitive> toAdd;
39 private List<OsmPrimitive> toUpdate;
40 private List<OsmPrimitive> toDelete;
41
42 /**
43 * creates a new empty data set
44 */
45 public APIDataSet() {
46 toAdd = new LinkedList<>();
47 toUpdate = new LinkedList<>();
48 toDelete = new LinkedList<>();
49 }
50
51 /**
52 * initializes the API data set with the modified primitives in <code>ds</code>
53 *
54 * @param ds the data set. Ignored, if null.
55 */
56 public void init(DataSet ds) {
57 if (ds == null) return;
58 init(ds.allPrimitives());
59 }
60
61 public final void init(Collection<OsmPrimitive> primitives) {
62 toAdd.clear();
63 toUpdate.clear();
64 toDelete.clear();
65
66 for (OsmPrimitive osm :primitives) {
67 if (osm.isNewOrUndeleted() && !osm.isDeleted()) {
68 toAdd.add(osm);
69 } else if (osm.isModified() && !osm.isDeleted()) {
70 toUpdate.add(osm);
71 } else if (osm.isDeleted() && !osm.isNew() && osm.isModified() && osm.isVisible()) {
72 toDelete.add(osm);
73 }
74 }
75 OsmPrimitiveComparator c = new OsmPrimitiveComparator(false, true);
76 Collections.sort(toDelete, c);
77 Collections.sort(toAdd, c);
78 Collections.sort(toUpdate, c);
79 }
80
81 /**
82 * initializes the API data set with the modified primitives in <code>ds</code>
83 *
84 * @param ds the data set. Ignored, if null.
85 */
86 public APIDataSet(DataSet ds) {
87 this();
88 init(ds);
89 }
90
91 /**
92 * Replies true if one of the primitives to be updated or to be deleted
93 * participates in the conflict <code>conflict</code>
94 *
95 * @param conflict the conflict
96 * @return true if one of the primitives to be updated or to be deleted
97 * participates in the conflict <code>conflict</code>
98 */
99 public boolean participatesInConflict(Conflict<?> conflict) {
100 if (conflict == null) return false;
101 for (OsmPrimitive p: toUpdate) {
102 if (conflict.isParticipating(p)) return true;
103 }
104 for (OsmPrimitive p: toDelete) {
105 if (conflict.isParticipating(p)) return true;
106 }
107 return false;
108 }
109
110 /**
111 * Replies true if one of the primitives to be updated or to be deleted
112 * participates in at least one conflict in <code>conflicts</code>
113 *
114 * @param conflicts the collection of conflicts
115 * @return true if one of the primitives to be updated or to be deleted
116 * participates in at least one conflict in <code>conflicts</code>
117 */
118 public boolean participatesInConflict(ConflictCollection conflicts) {
119 if (conflicts == null || conflicts.isEmpty()) return false;
120 Set<PrimitiveId> idsParticipatingInConflicts = new HashSet<>();
121 for (OsmPrimitive p: conflicts.getMyConflictParties()) {
122 idsParticipatingInConflicts.add(p.getPrimitiveId());
123 }
124 for (OsmPrimitive p: conflicts.getTheirConflictParties()) {
125 idsParticipatingInConflicts.add(p.getPrimitiveId());
126 }
127 for (OsmPrimitive p: toUpdate) {
128 if (idsParticipatingInConflicts.contains(p.getPrimitiveId())) return true;
129 }
130 for (OsmPrimitive p: toDelete) {
131 if (idsParticipatingInConflicts.contains(p.getPrimitiveId())) return true;
132 }
133 return false;
134 }
135
136 /**
137 * initializes the API data set with the primitives in <code>primitives</code>
138 *
139 * @param primitives the collection of primitives
140 */
141 public APIDataSet(Collection<OsmPrimitive> primitives) {
142 this();
143 init(primitives);
144 }
145
146 /**
147 * Replies true if there are no primitives to upload
148 *
149 * @return true if there are no primitives to upload
150 */
151 public boolean isEmpty() {
152 return toAdd.isEmpty() && toUpdate.isEmpty() && toDelete.isEmpty();
153 }
154
155 /**
156 * Replies the primitives which should be added to the OSM database
157 *
158 * @return the primitives which should be added to the OSM database
159 */
160 public List<OsmPrimitive> getPrimitivesToAdd() {
161 return toAdd;
162 }
163
164 /**
165 * Replies the primitives which should be updated in the OSM database
166 *
167 * @return the primitives which should be updated in the OSM database
168 */
169 public List<OsmPrimitive> getPrimitivesToUpdate() {
170 return toUpdate;
171 }
172
173 /**
174 * Replies the primitives which should be deleted in the OSM database
175 *
176 * @return the primitives which should be deleted in the OSM database
177 */
178 public List<OsmPrimitive> getPrimitivesToDelete() {
179 return toDelete;
180 }
181
182 /**
183 * Replies all primitives
184 *
185 * @return all primitives
186 */
187 public List<OsmPrimitive> getPrimitives() {
188 List<OsmPrimitive> ret = new LinkedList<>();
189 ret.addAll(toAdd);
190 ret.addAll(toUpdate);
191 ret.addAll(toDelete);
192 return ret;
193 }
194
195 /**
196 * Replies the number of objects to upload
197 *
198 * @return the number of objects to upload
199 */
200 public int getSize() {
201 return toAdd.size() + toUpdate.size() + toDelete.size();
202 }
203
204 public void removeProcessed(Collection<IPrimitive> processed) {
205 if (processed == null) return;
206 toAdd.removeAll(processed);
207 toUpdate.removeAll(processed);
208 toDelete.removeAll(processed);
209 }
210
211 /**
212 * Adjusts the upload order for new relations. Child relations are uploaded first,
213 * parent relations second.
214 *
215 * This method detects cyclic dependencies in new relation. Relations with cyclic
216 * dependencies can't be uploaded.
217 *
218 * @throws CyclicUploadDependencyException if a cyclic dependency is detected
219 */
220 public void adjustRelationUploadOrder() throws CyclicUploadDependencyException {
221 List<OsmPrimitive> newToAdd = new LinkedList<>();
222 newToAdd.addAll(Utils.filteredCollection(toAdd, Node.class));
223 newToAdd.addAll(Utils.filteredCollection(toAdd, Way.class));
224
225 List<Relation> relationsToAdd = new ArrayList<>(Utils.filteredCollection(toAdd, Relation.class));
226 List<Relation> noProblemRelations = filterRelationsNotReferringToNewRelations(relationsToAdd);
227 newToAdd.addAll(noProblemRelations);
228 relationsToAdd.removeAll(noProblemRelations);
229
230 RelationUploadDependencyGraph graph = new RelationUploadDependencyGraph(relationsToAdd, true);
231 newToAdd.addAll(graph.computeUploadOrder());
232 toAdd = newToAdd;
233
234 List<OsmPrimitive> newToDelete = new LinkedList<>();
235 graph = new RelationUploadDependencyGraph(Utils.filteredCollection(toDelete, Relation.class), false);
236 newToDelete.addAll(graph.computeUploadOrder());
237 newToDelete.addAll(Utils.filteredCollection(toDelete, Way.class));
238 newToDelete.addAll(Utils.filteredCollection(toDelete, Node.class));
239 toDelete = newToDelete;
240 }
241
242 /**
243 * Replies the subset of relations in <code>relations</code> which are not referring to any
244 * new relation
245 *
246 * @param relations a list of relations
247 * @return the subset of relations in <code>relations</code> which are not referring to any
248 * new relation
249 */
250 protected List<Relation> filterRelationsNotReferringToNewRelations(Collection<Relation> relations) {
251 List<Relation> ret = new LinkedList<>();
252 for (Relation relation: relations) {
253 boolean refersToNewRelation = false;
254 for (RelationMember m : relation.getMembers()) {
255 if (m.isRelation() && m.getMember().isNewOrUndeleted()) {
256 refersToNewRelation = true;
257 break;
258 }
259 }
260 if (!refersToNewRelation) {
261 ret.add(relation);
262 }
263 }
264 return ret;
265 }
266
267 /**
268 * Utility class to sort a collection of new relations with their dependencies
269 * topologically.
270 *
271 */
272 private static class RelationUploadDependencyGraph {
273 private Map<Relation, Set<Relation>> children = new HashMap<>();
274 private Collection<Relation> relations;
275 private Set<Relation> visited = new HashSet<>();
276 private List<Relation> uploadOrder;
277 private final boolean newOrUndeleted;
278
279 RelationUploadDependencyGraph(Collection<Relation> relations, boolean newOrUndeleted) {
280 this.newOrUndeleted = newOrUndeleted;
281 build(relations);
282 }
283
284 public final void build(Collection<Relation> relations) {
285 this.relations = new HashSet<>();
286 for (Relation relation: relations) {
287 if (newOrUndeleted ? !relation.isNewOrUndeleted() : !relation.isDeleted()) {
288 continue;
289 }
290 this.relations.add(relation);
291 for (RelationMember m: relation.getMembers()) {
292 if (m.isRelation() && (newOrUndeleted ? m.getMember().isNewOrUndeleted() : m.getMember().isDeleted())) {
293 addDependency(relation, (Relation) m.getMember());
294 }
295 }
296 }
297 }
298
299 public Set<Relation> getChildren(Relation relation) {
300 Set<Relation> p = children.get(relation);
301 if (p == null) {
302 p = new HashSet<>();
303 children.put(relation, p);
304 }
305 return p;
306 }
307
308 public void addDependency(Relation relation, Relation child) {
309 getChildren(relation).add(child);
310 }
311
312 protected void visit(Deque<Relation> path, Relation current) throws CyclicUploadDependencyException {
313 if (path.contains(current)) {
314 path.push(current);
315 throw new CyclicUploadDependencyException(path);
316 }
317 if (!visited.contains(current)) {
318 path.push(current);
319 visited.add(current);
320 for (Relation dependent : getChildren(current)) {
321 visit(path, dependent);
322 }
323 uploadOrder.add(current);
324 path.pop();
325 }
326 }
327
328 public List<Relation> computeUploadOrder() throws CyclicUploadDependencyException {
329 visited = new HashSet<>();
330 uploadOrder = new LinkedList<>();
331 Deque<Relation> path = new ArrayDeque<>();
332 for (Relation relation: relations) {
333 visit(path, relation);
334 }
335 List<Relation> ret = new ArrayList<>(relations);
336 Collections.sort(
337 ret,
338 new Comparator<Relation>() {
339 @Override
340 public int compare(Relation o1, Relation o2) {
341 return Integer.compare(uploadOrder.indexOf(o1), uploadOrder.indexOf(o2));
342 }
343 }
344 );
345 return ret;
346 }
347 }
348}
Note: See TracBrowser for help on using the repository browser.