Changeset 14347 in josm


Ignore:
Timestamp:
2018-10-18T16:55:57+02:00 (7 months ago)
Author:
Don-vip
Message:

fix #16850 - Make overriding DownloadOsmTask.AbstractInternalTask.createNewLayer() methods easier (patch by floscher, modified)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTask.java

    r14247 r14347  
    1212import java.util.HashSet;
    1313import java.util.Objects;
     14import java.util.Optional;
    1415import java.util.Set;
    1516import java.util.concurrent.Future;
     
    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();
    259             }
    260             if (settings.getDownloadPolicy() != null) {
    261                 dataSet.setDownloadPolicy(settings.getDownloadPolicy());
    262             }
    263             if (settings.getUploadPolicy() != null) {
    264                 dataSet.setUploadPolicy(settings.getUploadPolicy());
    265             }
     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         * @since 14347
     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         * @since 14347
     276         */
     277        protected OsmDataLayer createNewLayer(final DataSet dataset, final Optional<String> layerName) {
     278            if (layerName.filter(Utils::isStripEmpty).isPresent()) {
     279                throw new IllegalArgumentException("Blank layer name!");
     280            }
     281            return new OsmDataLayer(
     282                Objects.requireNonNull(dataset, "dataset parameter"),
     283                layerName.orElseGet(this::generateLayerName),
     284                null
     285            );
     286        }
     287
     288        /**
     289         * Convenience method for {@link #createNewLayer(DataSet, Optional)}, uses the dataset
     290         * from field {@link #dataSet} and applies the settings from field {@link #settings}.
     291         *
     292         * @param layerName an optional layer name, must be non-blank if the [Optional] is present
     293         * @return a newly constructed layer
     294         * @since 14347
     295         */
     296        protected final OsmDataLayer createNewLayer(final Optional<String> layerName) {
     297            Optional.ofNullable(settings.getDownloadPolicy())
     298                .ifPresent(dataSet::setDownloadPolicy);
     299            Optional.ofNullable(settings.getUploadPolicy())
     300                .ifPresent(dataSet::setUploadPolicy);
    266301            if (dataSet.isLocked() && !settings.isLocked()) {
    267302                dataSet.unlock();
     
    269304                dataSet.lock();
    270305            }
    271             return new OsmDataLayer(dataSet, layerName, null);
    272         }
    273 
     306            return createNewLayer(dataSet, layerName);
     307        }
     308
     309        /**
     310         * @param layerName the name of the new layer
     311         * @deprecated Use {@link #createNewLayer(DataSet, Optional)}
     312         * @return a newly constructed layer
     313         */
     314        @Deprecated
     315        protected OsmDataLayer createNewLayer(final String layerName) {
     316            return createNewLayer(Optional.ofNullable(layerName).filter(it -> !Utils.isStripEmpty(it)));
     317        }
     318
     319        /**
     320         * @deprecated Use {@link #createNewLayer(Optional)}
     321         * @return a newly constructed layer
     322         */
     323        @Deprecated
    274324        protected OsmDataLayer createNewLayer() {
    275             return createNewLayer(null);
     325            return createNewLayer(Optional.empty());
    276326        }
    277327
Note: See TracChangeset for help on using the changeset viewer.