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

Last change on this file since 7122 was 7005, checked in by Don-vip, 10 years ago

see #8465 - use diamond operator where applicable

  • Property svn:eol-style set to native
File size: 9.6 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> makeIncompleteData_byPrimId;
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 makeIncompleteData_byPrimId = 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 (makeIncompleteData_byPrimId.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 = makeIncompleteData_byPrimId.get(osm);
124 if (data != null) {
125 if (ds.getPrimitiveById(osm) != osm)
126 throw new AssertionError(String.format("Primitive %s has been made incomplete when purging, but it cannot be found on undo.", osm));
127 osm.load(data);
128 } else {
129 if (ds.getPrimitiveById(osm) != null)
130 throw new AssertionError(String.format("Primitive %s was removed when purging, but is still there on undo", osm));
131 ds.addPrimitive(osm);
132 }
133 }
134
135 for (Conflict<?> conflict : purgedConflicts) {
136 getLayer().getConflicts().add(conflict);
137 }
138 }
139
140 /**
141 * Sorts a collection of primitives such that for each object
142 * its referrers come later in the sorted collection.
143 */
144 public static List<OsmPrimitive> topoSort(Collection<OsmPrimitive> sel) {
145 Set<OsmPrimitive> in = new HashSet<>(sel);
146
147 List<OsmPrimitive> out = new ArrayList<>(in.size());
148
149 // Nodes not deleted in the first pass
150 Set<OsmPrimitive> remainingNodes = new HashSet<>(in.size());
151
152 /**
153 * First add nodes that have no way referrer.
154 */
155 outer:
156 for (Iterator<OsmPrimitive> it = in.iterator(); it.hasNext();) {
157 OsmPrimitive u = it.next();
158 if (u instanceof Node) {
159 Node n = (Node) u;
160 for (OsmPrimitive ref : n.getReferrers()) {
161 if (ref instanceof Way && in.contains(ref)) {
162 it.remove();
163 remainingNodes.add(n);
164 continue outer;
165 }
166 }
167 it.remove();
168 out.add(n);
169 }
170 }
171
172 /**
173 * Then add all ways, each preceded by its (remaining) nodes.
174 */
175 for (Iterator<OsmPrimitive> it = in.iterator(); it.hasNext();) {
176 OsmPrimitive u = it.next();
177 if (u instanceof Way) {
178 Way w = (Way) u;
179 it.remove();
180 for (Node n : w.getNodes()) {
181 if (remainingNodes.contains(n)) {
182 remainingNodes.remove(n);
183 out.add(n);
184 }
185 }
186 out.add(w);
187 }
188 }
189
190 if (!remainingNodes.isEmpty())
191 throw new AssertionError("topo sort algorithm failed (nodes remaining)");
192
193 /**
194 * Rest are relations. Do topological sorting on a DAG where each
195 * arrow points from child to parent. (Because it is faster to
196 * loop over getReferrers() than getMembers().)
197 */
198 @SuppressWarnings({ "unchecked", "rawtypes" })
199 Set<Relation> inR = (Set) in;
200 Set<Relation> childlessR = new HashSet<>();
201 List<Relation> outR = new ArrayList<>(inR.size());
202
203 HashMap<Relation, Integer> numChilds = new HashMap<>();
204
205 // calculate initial number of childs
206 for (Relation r : inR) {
207 numChilds.put(r, 0);
208 }
209 for (Relation r : inR) {
210 for (OsmPrimitive parent : r.getReferrers()) {
211 if (!(parent instanceof Relation))
212 throw new AssertionError();
213 Integer i = numChilds.get(parent);
214 if (i != null) {
215 numChilds.put((Relation)parent, i+1);
216 }
217 }
218 }
219 for (Relation r : inR) {
220 if (numChilds.get(r).equals(0)) {
221 childlessR.add(r);
222 }
223 }
224
225 while (!childlessR.isEmpty()) {
226 // Identify one childless Relation and
227 // let it virtually die. This makes other
228 // relations childless.
229 Iterator<Relation> it = childlessR.iterator();
230 Relation next = it.next();
231 it.remove();
232 outR.add(next);
233
234 for (OsmPrimitive parentPrim : next.getReferrers()) {
235 Relation parent = (Relation) parentPrim;
236 Integer i = numChilds.get(parent);
237 if (i != null) {
238 numChilds.put(parent, i-1);
239 if (i-1 == 0) {
240 childlessR.add(parent);
241 }
242 }
243 }
244 }
245
246 if (outR.size() != inR.size())
247 throw new AssertionError("topo sort algorithm failed");
248
249 out.addAll(outR);
250
251 return out;
252 }
253
254 @Override
255 public String getDescriptionText() {
256 return trn("Purged {0} object", "Purged {0} objects", toPurge.size(), toPurge.size());
257 }
258
259 @Override
260 public Icon getDescriptionIcon() {
261 return ImageProvider.get("data", "purge");
262 }
263
264 @Override
265 public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
266 return toPurge;
267 }
268
269 @Override
270 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
271 }
272}
Note: See TracBrowser for help on using the repository browser.