Ticket #16850: DownloadOsmTask.patch

File DownloadOsmTask.patch, 3.9 KB (added by floscher, 8 years ago)

Another update

  • 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;
     
    250251            return getModifiableDataLayers().findFirst().orElse(null);
    251252        }
    252253
    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();
     254        /**
     255         * Creates a name for a new layer by utilizing the settings ({@link DownloadParams#getLayerName()}) or
     256         * {@link OsmDataLayer#createNewName()} if the former option is {@code null}.
     257         *
     258         * @return a name for a new layer
     259         */
     260        protected String generateLayerName() {
     261            return Optional.ofNullable(settings.getLayerName())
     262                .orElse(OsmDataLayer.createNewName());
     263        }
     264
     265        /**
     266         * Can be overridden (e.g. by plugins) if a subclass of {@link OsmDataLayer} is needed.
     267         * If you want to change how the name is determined, consider overriding
     268         * {@link #generateLayerName()} instead.
     269         *
     270         * @param dataset the dataset on which the layer is based, must be non-null
     271         * @param layerName the name of the new layer, must be non-blank
     272         * @return a new instance of {@link OsmDataLayer} constructed with the given arguments
     273         */
     274        protected OsmDataLayer createNewLayer(final DataSet dataset, final String layerName) {
     275            if (Utils.isStripEmpty(layerName)) {
     276                throw new IllegalArgumentException("Blank layer name!");
    259277            }
    260             if (settings.getDownloadPolicy() != null) {
    261                 dataSet.setDownloadPolicy(settings.getDownloadPolicy());
    262             }
    263             if (settings.getUploadPolicy() != null) {
    264                 dataSet.setUploadPolicy(settings.getUploadPolicy());
    265             }
     278            return new OsmDataLayer(
     279                Objects.requireNonNull(dataset, "dataset parameter"),
     280                Objects.requireNonNull(layerName, "layerName parameter"),
     281                null
     282            );
     283        }
     284
     285        //TODO: Make final as soon as this method is no longer overridden by plugins, change param type to Optional<String>
     286        protected OsmDataLayer createNewLayer(final String layerName) {
     287            Optional.ofNullable(settings.getDownloadPolicy())
     288                .ifPresent(dataSet::setDownloadPolicy);
     289            Optional.ofNullable(settings.getUploadPolicy())
     290                .ifPresent(dataSet::setUploadPolicy);
    266291            if (dataSet.isLocked() && !settings.isLocked()) {
    267292                dataSet.unlock();
    268293            } else if (!dataSet.isLocked() && settings.isLocked()) {
    269294                dataSet.lock();
    270295            }
    271             return new OsmDataLayer(dataSet, layerName, null);
     296            return createNewLayer(
     297                dataSet,
     298                Optional.ofNullable(layerName)
     299                    .filter(it -> !Utils.isStripEmpty(it))
     300                    .orElseGet(this::generateLayerName)
     301            );
    272302        }
    273303
     304        //TODO: Remove this method as soon as it is no longer overridden by plugins
    274305        protected OsmDataLayer createNewLayer() {
    275306            return createNewLayer(null);
    276307        }