source: josm/trunk/src/org/openstreetmap/josm/command/PurgeCommand.java@ 8914

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

checkstyle: enable relevant whitespace checks and fix them

  • Property svn:eol-style set to native
File size: 11.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.trn;
5
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.HashMap;
9import java.util.HashSet;
10import java.util.Iterator;
11import java.util.List;
12import java.util.Map;
13import java.util.Set;
14
15import javax.swing.Icon;
16
17import org.openstreetmap.josm.data.conflict.Conflict;
18import org.openstreetmap.josm.data.conflict.ConflictCollection;
19import org.openstreetmap.josm.data.osm.DataSet;
20import org.openstreetmap.josm.data.osm.Node;
21import org.openstreetmap.josm.data.osm.NodeData;
22import org.openstreetmap.josm.data.osm.OsmPrimitive;
23import org.openstreetmap.josm.data.osm.PrimitiveData;
24import org.openstreetmap.josm.data.osm.PrimitiveId;
25import org.openstreetmap.josm.data.osm.Relation;
26import org.openstreetmap.josm.data.osm.RelationData;
27import org.openstreetmap.josm.data.osm.Storage;
28import org.openstreetmap.josm.data.osm.Way;
29import org.openstreetmap.josm.data.osm.WayData;
30import org.openstreetmap.josm.gui.layer.OsmDataLayer;
31import org.openstreetmap.josm.tools.ImageProvider;
32
33/**
34 * Command, to purge a list of primitives.
35 */
36public class PurgeCommand extends Command {
37 protected List<OsmPrimitive> toPurge;
38 protected Storage<PrimitiveData> makeIncompleteData;
39
40 protected Map<PrimitiveId, PrimitiveData> makeIncompleteDataByPrimId;
41
42 protected final ConflictCollection purgedConflicts = new ConflictCollection();
43
44 protected final DataSet ds;
45
46 /**
47 * This command relies on a number of consistency conditions:
48 * - makeIncomplete must be a subset of toPurge.
49 * - Each primitive, that is in toPurge but not in makeIncomplete, must
50 * have all its referrers in toPurge.
51 * - Each element of makeIncomplete must not be new and must have only
52 * referrers that are either a relation or included in toPurge.
53 */
54 public PurgeCommand(OsmDataLayer layer, Collection<OsmPrimitive> toPurge, Collection<OsmPrimitive> makeIncomplete) {
55 super(layer);
56 this.ds = layer.data;
57 /**
58 * The topological sort is to avoid missing way nodes and missing
59 * relation members when adding primitives back to the dataset on undo.
60 *
61 * The same should hold for normal execution, but at time of writing
62 * there seem to be no such consistency checks when removing primitives.
63 * (It is done in a save manner, anyway.)
64 */
65 this.toPurge = topoSort(toPurge);
66 saveIncomplete(makeIncomplete);
67 }
68
69 protected final void saveIncomplete(Collection<OsmPrimitive> makeIncomplete) {
70 makeIncompleteData = new Storage<>(new Storage.PrimitiveIdHash());
71 makeIncompleteDataByPrimId = makeIncompleteData.foreignKey(new Storage.PrimitiveIdHash());
72
73 for (OsmPrimitive osm : makeIncomplete) {
74 makeIncompleteData.add(osm.save());
75 }
76 }
77
78 @Override
79 public boolean executeCommand() {
80 ds.beginUpdate();
81 try {
82 purgedConflicts.get().clear();
83 /**
84 * Loop from back to front to keep referential integrity.
85 */
86 for (int i = toPurge.size()-1; i >= 0; --i) {
87 OsmPrimitive osm = toPurge.get(i);
88 if (makeIncompleteDataByPrimId.containsKey(osm)) {
89 // we could simply set the incomplete flag
90 // but that would not free memory in case the
91 // user clears undo/redo buffer after purge
92 PrimitiveData empty;
93 switch(osm.getType()) {
94 case NODE: empty = new NodeData(); break;
95 case WAY: empty = new WayData(); break;
96 case RELATION: empty = new RelationData(); break;
97 default: throw new AssertionError();
98 }
99 empty.setId(osm.getUniqueId());
100 empty.setIncomplete(true);
101 osm.load(empty);
102 } else {
103 ds.removePrimitive(osm);
104 Conflict<?> conflict = getLayer().getConflicts().getConflictForMy(osm);
105 if (conflict != null) {
106 purgedConflicts.add(conflict);
107 getLayer().getConflicts().remove(conflict);
108 }
109 }
110 }
111 } finally {
112 ds.endUpdate();
113 }
114 return true;
115 }
116
117 @Override
118 public void undoCommand() {
119 if (ds == null)
120 return;
121
122 for (OsmPrimitive osm : toPurge) {
123 PrimitiveData data = makeIncompleteDataByPrimId.get(osm);
124 if (data != null) {
125 if (ds.getPrimitiveById(osm) != osm)
126 throw new AssertionError(
127 String.format("Primitive %s has been made incomplete when purging, but it cannot be found on undo.", osm));
128 osm.load(data);
129 } else {
130 if (ds.getPrimitiveById(osm) != null)
131 throw new AssertionError(String.format("Primitive %s was removed when purging, but is still there on undo", osm));
132 ds.addPrimitive(osm);
133 }
134 }
135
136 for (Conflict<?> conflict : purgedConflicts) {
137 getLayer().getConflicts().add(conflict);
138 }
139 }
140
141 /**
142 * Sorts a collection of primitives such that for each object
143 * its referrers come later in the sorted collection.
144 */
145 public static List<OsmPrimitive> topoSort(Collection<OsmPrimitive> sel) {
146 Set<OsmPrimitive> in = new HashSet<>(sel);
147
148 List<OsmPrimitive> out = new ArrayList<>(in.size());
149
150 // Nodes not deleted in the first pass
151 Set<OsmPrimitive> remainingNodes = new HashSet<>(in.size());
152
153 /**
154 * First add nodes that have no way referrer.
155 */
156 outer:
157 for (Iterator<OsmPrimitive> it = in.iterator(); it.hasNext();) {
158 OsmPrimitive u = it.next();
159 if (u instanceof Node) {
160 Node n = (Node) u;
161 for (OsmPrimitive ref : n.getReferrers()) {
162 if (ref instanceof Way && in.contains(ref)) {
163 it.remove();
164 remainingNodes.add(n);
165 continue outer;
166 }
167 }
168 it.remove();
169 out.add(n);
170 }
171 }
172
173 /**
174 * Then add all ways, each preceded by its (remaining) nodes.
175 */
176 for (Iterator<OsmPrimitive> it = in.iterator(); it.hasNext();) {
177 OsmPrimitive u = it.next();
178 if (u instanceof Way) {
179 Way w = (Way) u;
180 it.remove();
181 for (Node n : w.getNodes()) {
182 if (remainingNodes.contains(n)) {
183 remainingNodes.remove(n);
184 out.add(n);
185 }
186 }
187 out.add(w);
188 }
189 }
190
191 if (!remainingNodes.isEmpty())
192 throw new AssertionError("topo sort algorithm failed (nodes remaining)");
193
194 /**
195 * Rest are relations. Do topological sorting on a DAG where each
196 * arrow points from child to parent. (Because it is faster to
197 * loop over getReferrers() than getMembers().)
198 */
199 @SuppressWarnings({ "unchecked", "rawtypes" })
200 Set<Relation> inR = (Set) in;
201 Set<Relation> childlessR = new HashSet<>();
202 List<Relation> outR = new ArrayList<>(inR.size());
203
204 Map<Relation, Integer> numChilds = new HashMap<>();
205
206 // calculate initial number of childs
207 for (Relation r : inR) {
208 numChilds.put(r, 0);
209 }
210 for (Relation r : inR) {
211 for (OsmPrimitive parent : r.getReferrers()) {
212 if (!(parent instanceof Relation))
213 throw new AssertionError();
214 Integer i = numChilds.get(parent);
215 if (i != null) {
216 numChilds.put((Relation) parent, i+1);
217 }
218 }
219 }
220 for (Relation r : inR) {
221 if (numChilds.get(r).equals(0)) {
222 childlessR.add(r);
223 }
224 }
225
226 while (!childlessR.isEmpty()) {
227 // Identify one childless Relation and
228 // let it virtually die. This makes other
229 // relations childless.
230 Iterator<Relation> it = childlessR.iterator();
231 Relation next = it.next();
232 it.remove();
233 outR.add(next);
234
235 for (OsmPrimitive parentPrim : next.getReferrers()) {
236 Relation parent = (Relation) parentPrim;
237 Integer i = numChilds.get(parent);
238 if (i != null) {
239 numChilds.put(parent, i-1);
240 if (i-1 == 0) {
241 childlessR.add(parent);
242 }
243 }
244 }
245 }
246
247 if (outR.size() != inR.size())
248 throw new AssertionError("topo sort algorithm failed");
249
250 out.addAll(outR);
251
252 return out;
253 }
254
255 @Override
256 public String getDescriptionText() {
257 return trn("Purged {0} object", "Purged {0} objects", toPurge.size(), toPurge.size());
258 }
259
260 @Override
261 public Icon getDescriptionIcon() {
262 return ImageProvider.get("data", "purge");
263 }
264
265 @Override
266 public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
267 return toPurge;
268 }
269
270 @Override
271 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
272 }
273
274 @Override
275 public int hashCode() {
276 final int prime = 31;
277 int result = super.hashCode();
278 result = prime * result + ((ds == null) ? 0 : ds.hashCode());
279 result = prime * result + ((makeIncompleteData == null) ? 0 : makeIncompleteData.hashCode());
280 result = prime * result + ((makeIncompleteDataByPrimId == null) ? 0 : makeIncompleteDataByPrimId.hashCode());
281 result = prime * result + ((purgedConflicts == null) ? 0 : purgedConflicts.hashCode());
282 result = prime * result + ((toPurge == null) ? 0 : toPurge.hashCode());
283 return result;
284 }
285
286 @Override
287 public boolean equals(Object obj) {
288 if (this == obj)
289 return true;
290 if (!super.equals(obj))
291 return false;
292 if (getClass() != obj.getClass())
293 return false;
294 PurgeCommand other = (PurgeCommand) obj;
295 if (ds == null) {
296 if (other.ds != null)
297 return false;
298 } else if (!ds.equals(other.ds))
299 return false;
300 if (makeIncompleteData == null) {
301 if (other.makeIncompleteData != null)
302 return false;
303 } else if (!makeIncompleteData.equals(other.makeIncompleteData))
304 return false;
305 if (makeIncompleteDataByPrimId == null) {
306 if (other.makeIncompleteDataByPrimId != null)
307 return false;
308 } else if (!makeIncompleteDataByPrimId.equals(other.makeIncompleteDataByPrimId))
309 return false;
310 if (purgedConflicts == null) {
311 if (other.purgedConflicts != null)
312 return false;
313 } else if (!purgedConflicts.equals(other.purgedConflicts))
314 return false;
315 if (toPurge == null) {
316 if (other.toPurge != null)
317 return false;
318 } else if (!toPurge.equals(other.toPurge))
319 return false;
320 return true;
321 }
322}
Note: See TracBrowser for help on using the repository browser.