Ticket #16850: DownloadOsmTask.2.patch

File DownloadOsmTask.2.patch, 4.7 KB (added by floscher, 5 years ago)

Instead of TODO comments, deprecate old API and already add new API

  • core/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTask.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    1111import java.util.Collections;
    1212import java.util.HashSet;
    1313import java.util.Objects;
     14import java.util.Optional;
    1415import java.util.Set;
    1516import java.util.concurrent.Future;
    1617import java.util.regex.Matcher;
     
    3031import org.openstreetmap.josm.gui.MainApplication;
    3132import org.openstreetmap.josm.gui.MapFrame;
    3233import org.openstreetmap.josm.gui.PleaseWaitRunnable;
     34import org.openstreetmap.josm.gui.ProgramArguments;
    3335import org.openstreetmap.josm.gui.io.UpdatePrimitivesTask;
    3436import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    3537import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
     
    250252            return getModifiableDataLayers().findFirst().orElse(null);
    251253        }
    252254
    253         protected OsmDataLayer createNewLayer(String layerName) {
    254             if (layerName == null || layerName.isEmpty()) {
    255                 layerName = settings.getLayerName();
    256             }
    257             if (layerName == null || layerName.isEmpty()) {
    258                 layerName = OsmDataLayer.createNewName();
     255        /**
     256         * Creates a name for a new layer by utilizing the settings ({@link DownloadParams#getLayerName()}) or
     257         * {@link OsmDataLayer#createNewName()} if the former option is {@code null}.
     258         *
     259         * @return a name for a new layer
     260         */
     261        protected String generateLayerName() {
     262            return Optional.ofNullable(settings.getLayerName())
     263                .filter(layerName -> !Utils.isStripEmpty(layerName))
     264                .orElse(OsmDataLayer.createNewName());
     265        }
     266
     267        /**
     268         * Can be overridden (e.g. by plugins) if a subclass of {@link OsmDataLayer} is needed.
     269         * If you want to change how the name is determined, consider overriding
     270         * {@link #generateLayerName()} instead.
     271         *
     272         * @param dataset the dataset on which the layer is based, must be non-null
     273         * @param layerName the name of the new layer, must be either non-blank or non-present
     274         * @return a new instance of {@link OsmDataLayer} constructed with the given arguments
     275         */
     276        protected OsmDataLayer createNewLayer(final DataSet dataset, final Optional<String> layerName) {
     277            if (layerName.filter(Utils::isStripEmpty).isPresent()) {
     278                throw new IllegalArgumentException("Blank layer name!");
    259279            }
    260             if (settings.getDownloadPolicy() != null) {
    261                 dataSet.setDownloadPolicy(settings.getDownloadPolicy());
    262             }
    263             if (settings.getUploadPolicy() != null) {
    264                 dataSet.setUploadPolicy(settings.getUploadPolicy());
    265             }
     280            return new OsmDataLayer(
     281                Objects.requireNonNull(dataset, "dataset parameter"),
     282                layerName.orElseGet(this::generateLayerName),
     283                null
     284            );
     285        }
     286
     287        /**
     288         * Convenience method for {@link #createNewLayer(DataSet, Optional)}, uses the dataset
     289         * from field {@link #dataSet} and applies the settings from field {@link #settings}.
     290         *
     291         * @param layerName an optional layer name, must be non-blank if the [Optional] is present
     292         * @return a newly constructed layer
     293         */
     294        protected final OsmDataLayer createNewLayer(final Optional<String> layerName) {
     295            Optional.ofNullable(settings.getDownloadPolicy())
     296                .ifPresent(dataSet::setDownloadPolicy);
     297            Optional.ofNullable(settings.getUploadPolicy())
     298                .ifPresent(dataSet::setUploadPolicy);
    266299            if (dataSet.isLocked() && !settings.isLocked()) {
    267300                dataSet.unlock();
    268301            } else if (!dataSet.isLocked() && settings.isLocked()) {
    269302                dataSet.lock();
    270303            }
    271             return new OsmDataLayer(dataSet, layerName, null);
     304            return createNewLayer(dataSet, layerName);
     305        }
     306
     307        @Deprecated
     308        protected OsmDataLayer createNewLayer(final String layerName) {
     309            return createNewLayer(Optional.ofNullable(layerName).filter(it -> !Utils.isStripEmpty(it)));
    272310        }
    273311
     312        @Deprecated
    274313        protected OsmDataLayer createNewLayer() {
    275             return createNewLayer(null);
     314            return createNewLayer(Optional.empty());
    276315        }
    277316
    278317        protected ProjectionBounds computeBbox(Bounds bounds) {