source: josm/trunk/src/org/openstreetmap/josm/gui/io/CloseChangesetTask.java@ 12687

Last change on this file since 12687 was 11553, checked in by Don-vip, 7 years ago

refactor handling of null values - use Java 8 Optional where possible

  • Property svn:eol-style set to native
File size: 2.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.IOException;
7import java.util.ArrayList;
8import java.util.Collection;
9import java.util.List;
10import java.util.Optional;
11
12import javax.swing.SwingUtilities;
13
14import org.openstreetmap.josm.data.osm.Changeset;
15import org.openstreetmap.josm.data.osm.ChangesetCache;
16import org.openstreetmap.josm.gui.ExceptionDialogUtil;
17import org.openstreetmap.josm.gui.PleaseWaitRunnable;
18import org.openstreetmap.josm.io.OsmApi;
19import org.openstreetmap.josm.io.OsmTransferException;
20import org.xml.sax.SAXException;
21
22/**
23 * A task for closing a collection of changesets.
24 *
25 */
26public class CloseChangesetTask extends PleaseWaitRunnable {
27 private boolean canceled;
28 private Exception lastException;
29 private final Collection<Changeset> changesets;
30 private final List<Changeset> closedChangesets;
31
32 /**
33 * Closes all changesets in <code>changesets</code> if they are not null, if they
34 * are still open and if they have an id &gt; 0. Other changesets in the collection
35 * are ignored.
36 *
37 * @param changesets the collection of changesets. Empty collection assumes, if null.
38 */
39 public CloseChangesetTask(Collection<Changeset> changesets) {
40 super(tr("Closing changeset"), false /* don't ignore exceptions */);
41 this.changesets = Optional.ofNullable(changesets).orElseGet(ArrayList::new);
42 this.closedChangesets = new ArrayList<>();
43 }
44
45 @Override
46 protected void cancel() {
47 this.canceled = true;
48 OsmApi.getOsmApi().cancel();
49 }
50
51 @Override
52 protected void finish() {
53 if (canceled)
54 return;
55 if (lastException != null) {
56 ExceptionDialogUtil.explainException(lastException);
57 }
58 SwingUtilities.invokeLater(() -> ChangesetCache.getInstance().update(closedChangesets));
59 }
60
61 @Override
62 protected void realRun() throws SAXException, IOException, OsmTransferException {
63 try {
64 for (Changeset cs: changesets) {
65 if (canceled) return;
66 if (cs == null || cs.getId() <= 0 || !cs.isOpen()) {
67 continue;
68 }
69 getProgressMonitor().subTask(tr("Closing changeset {0}", cs.getId()));
70 OsmApi.getOsmApi().closeChangeset(cs, getProgressMonitor().createSubTaskMonitor(1, false));
71 closedChangesets.add(cs);
72 }
73 } catch (OsmTransferException e) {
74 if (canceled)
75 return;
76 lastException = e;
77 }
78 }
79}
Note: See TracBrowser for help on using the repository browser.