001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.command;
003
004import static org.openstreetmap.josm.tools.I18n.trn;
005
006import java.util.Collection;
007import java.util.Collections;
008import java.util.HashSet;
009import java.util.Objects;
010
011import org.openstreetmap.josm.data.osm.DataSet;
012import org.openstreetmap.josm.data.osm.OsmPrimitive;
013
014/**
015 * Command that selects OSM primitives
016 *
017 * @author Landwirt
018 */
019public class SelectCommand extends Command {
020
021    /** the primitives to select when executing the command */
022    private final Collection<OsmPrimitive> newSelection;
023
024    /** the selection before applying the new selection */
025    private Collection<OsmPrimitive> oldSelection;
026
027    /**
028     * Constructs a new select command.
029     * @param dataset The dataset the selection belongs to
030     * @param newSelection the primitives to select when executing the command.
031     * @since 12349
032     */
033    public SelectCommand(DataSet dataset, Collection<OsmPrimitive> newSelection) {
034        super(dataset);
035        if (newSelection == null || newSelection.isEmpty()) {
036            this.newSelection = Collections.emptySet();
037        } else if (newSelection.contains(null)) {
038            throw new IllegalArgumentException("null primitive in selection");
039        } else {
040            this.newSelection = new HashSet<>(newSelection);
041        }
042    }
043
044    @Override
045    public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
046        // Do nothing
047    }
048
049    @Override
050    public void undoCommand() {
051        ensurePrimitivesAreInDataset();
052
053        getAffectedDataSet().setSelected(oldSelection);
054    }
055
056    @Override
057    public boolean executeCommand() {
058        ensurePrimitivesAreInDataset();
059
060        oldSelection = getAffectedDataSet().getSelected();
061        getAffectedDataSet().setSelected(newSelection);
062        return true;
063    }
064
065    @Override
066    public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
067        return Collections.unmodifiableCollection(newSelection);
068    }
069
070    @Override
071    public String getDescriptionText() {
072        int size = newSelection != null ? newSelection.size() : 0;
073        return trn("Selected {0} object", "Selected {0} objects", size, size);
074    }
075
076    @Override
077    public int hashCode() {
078        return Objects.hash(super.hashCode(), newSelection, oldSelection);
079    }
080
081    @Override
082    public boolean equals(Object obj) {
083        if (this == obj) return true;
084        if (obj == null || getClass() != obj.getClass()) return false;
085        if (!super.equals(obj)) return false;
086        SelectCommand that = (SelectCommand) obj;
087        return Objects.equals(newSelection, that.newSelection) &&
088                Objects.equals(oldSelection, that.oldSelection);
089    }
090}