001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.io;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.util.Collection;
007import java.util.Collections;
008
009import org.openstreetmap.josm.data.osm.OsmPrimitive;
010import org.openstreetmap.josm.gui.layer.OsmDataLayer;
011import org.openstreetmap.josm.io.MultiFetchServerObjectReader;
012
013/**
014 * The asynchronous task for updating a collection of objects using multi fetch.
015 * @since 2599
016 */
017public class UpdatePrimitivesTask extends AbstractPrimitiveTask {
018
019    private final Collection<? extends OsmPrimitive> toUpdate;
020
021    /**
022     * Constructs a new {@code UpdatePrimitivesTask}.
023     *
024     * @param layer the layer in which primitives are updated. Must not be null.
025     * @param toUpdate a collection of primitives to update from the server. Set to
026     * the empty collection if null.
027     * @throws IllegalArgumentException if layer is null.
028     */
029    public UpdatePrimitivesTask(OsmDataLayer layer, Collection<? extends OsmPrimitive> toUpdate) {
030        super(tr("Update objects"), layer);
031        this.toUpdate = toUpdate != null ? toUpdate : Collections.<OsmPrimitive>emptyList();
032    }
033
034    @Override
035    protected void initMultiFetchReader(MultiFetchServerObjectReader reader) {
036        // don't update new primitives
037        toUpdate.stream().filter(p -> !p.isNew()).forEach(reader::append);
038    }
039}