source: josm/trunk/src/org/openstreetmap/josm/actions/downloadtasks/ChangesetHeaderDownloadTask.java@ 15152

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

more uses of Java 8 stream API

  • Property svn:eol-style set to native
File size: 6.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.downloadtasks;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.io.IOException;
8import java.util.Collection;
9import java.util.Collections;
10import java.util.HashSet;
11import java.util.Objects;
12import java.util.Set;
13import java.util.stream.Collectors;
14
15import org.openstreetmap.josm.data.osm.Changeset;
16import org.openstreetmap.josm.data.osm.ChangesetCache;
17import org.openstreetmap.josm.gui.ExceptionDialogUtil;
18import org.openstreetmap.josm.gui.MainApplication;
19import org.openstreetmap.josm.io.OsmTransferException;
20import org.xml.sax.SAXException;
21
22/**
23 * This is an asynchronous task for downloading a collection of changests from the OSM server.
24 *
25 * The task only downloads the changeset properties without the changeset content. It
26 * updates the global {@link ChangesetCache}.
27 * @since 2613
28 */
29public class ChangesetHeaderDownloadTask extends AbstractChangesetDownloadTask {
30
31 class DownloadTask extends RunnableDownloadTask {
32 /** the list of changeset ids to download */
33 private final Set<Integer> toDownload = new HashSet<>();
34 /** whether to include discussions or not */
35 private final boolean includeDiscussion;
36
37 DownloadTask(Component parent, Collection<Integer> ids, boolean includeDiscussion) {
38 super(parent, tr("Download changesets"));
39 this.includeDiscussion = includeDiscussion;
40 for (int id: ids != null ? ids : Collections.<Integer>emptyList()) {
41 if (id <= 0) {
42 continue;
43 }
44 toDownload.add(id);
45 }
46 }
47
48 @Override
49 protected void realRun() throws SAXException, IOException, OsmTransferException {
50 try {
51 downloadedChangesets.addAll(reader.readChangesets(toDownload, includeDiscussion,
52 getProgressMonitor().createSubTaskMonitor(0, false)));
53 } catch (OsmTransferException e) {
54 if (isCanceled())
55 // ignore exception if canceled
56 return;
57 // remember other exceptions
58 rememberLastException(e);
59 }
60 }
61
62 @Override
63 protected void finish() {
64 rememberDownloadedData(downloadedChangesets);
65 if (isCanceled())
66 return;
67 if (lastException != null) {
68 ExceptionDialogUtil.explainException(lastException);
69 }
70 updateChangesets();
71 }
72 }
73
74 /**
75 * Creates the download task for a collection of changeset ids. Uses a {@link org.openstreetmap.josm.gui.PleaseWaitDialog}
76 * whose parent is {@link MainApplication#getMainFrame}.
77 *
78 * Null ids or or ids &lt;= 0 in the id collection are ignored.
79 *
80 * @param ids the collection of ids. Empty collection assumed if null.
81 */
82 public ChangesetHeaderDownloadTask(Collection<Integer> ids) {
83 this(MainApplication.getMainFrame(), ids, false);
84 }
85
86 /**
87 * Creates the download task for a collection of changeset ids. Uses a {@link org.openstreetmap.josm.gui.PleaseWaitDialog}
88 * whose parent is the parent window of <code>dialogParent</code>.
89 *
90 * Null ids or or ids &lt;= 0 in the id collection are ignored.
91 *
92 * @param dialogParent the parent reference component for the {@link org.openstreetmap.josm.gui.PleaseWaitDialog}. Must not be null.
93 * @param ids the collection of ids. Empty collection assumed if null.
94 * @throws IllegalArgumentException if dialogParent is null
95 */
96 public ChangesetHeaderDownloadTask(Component dialogParent, Collection<Integer> ids) {
97 this(dialogParent, ids, false);
98 }
99
100 /**
101 * Creates the download task for a collection of changeset ids, with possibility to download changeset discussion.
102 * Uses a {@link org.openstreetmap.josm.gui.PleaseWaitDialog} whose parent is the parent window of <code>dialogParent</code>.
103 *
104 * Null ids or or ids &lt;= 0 in the id collection are ignored.
105 *
106 * @param dialogParent the parent reference component for the {@link org.openstreetmap.josm.gui.PleaseWaitDialog}. Must not be null.
107 * @param ids the collection of ids. Empty collection assumed if null.
108 * @param includeDiscussion determines if discussion comments must be downloaded or not
109 * @throws IllegalArgumentException if dialogParent is null
110 * @since 7704
111 */
112 public ChangesetHeaderDownloadTask(Component dialogParent, Collection<Integer> ids, boolean includeDiscussion) {
113 setDownloadTask(new DownloadTask(dialogParent, ids, includeDiscussion));
114 }
115
116 /**
117 * Builds a download task from for a collection of changesets.
118 *
119 * Ignores null values and changesets with {@link Changeset#isNew()} == true.
120 *
121 * @param changesets the collection of changesets. Assumes an empty collection if null.
122 * @return the download task
123 */
124 public static ChangesetHeaderDownloadTask buildTaskForChangesets(Collection<Changeset> changesets) {
125 return buildTaskForChangesets(MainApplication.getMainFrame(), changesets);
126 }
127
128 /**
129 * Builds a download task from for a collection of changesets.
130 *
131 * Ignores null values and changesets with {@link Changeset#isNew()} == true.
132 *
133 * @param parent the parent component relative to which the {@link org.openstreetmap.josm.gui.PleaseWaitDialog} is displayed.
134 * Must not be null.
135 * @param changesets the collection of changesets. Assumes an empty collection if null.
136 * @return the download task
137 * @throws NullPointerException if parent is null
138 */
139 public static ChangesetHeaderDownloadTask buildTaskForChangesets(Component parent, Collection<Changeset> changesets) {
140 return new ChangesetHeaderDownloadTask(Objects.requireNonNull(parent, "parent"),
141 changesets == null ? Collections.<Integer>emptySet() :
142 changesets.stream().filter(cs -> cs != null && !cs.isNew()).map(Changeset::getId).collect(Collectors.toSet()));
143 }
144}
Note: See TracBrowser for help on using the repository browser.