source: josm/trunk/src/org/openstreetmap/josm/command/SelectCommand.java@ 12726

Last change on this file since 12726 was 12726, checked in by Don-vip, 7 years ago

see #13036 - deprecate Command() default constructor, fix unit tests and java warnings

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.trn;
5
6import java.util.Collection;
7import java.util.Collections;
8import java.util.HashSet;
9import java.util.Objects;
10
11import org.openstreetmap.josm.data.osm.DataSet;
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13
14/**
15 * Command that selects OSM primitives
16 *
17 * @author Landwirt
18 */
19public class SelectCommand extends Command {
20
21 /** the primitives to select when executing the command */
22 private final Collection<OsmPrimitive> newSelection;
23
24 /** the selection before applying the new selection */
25 private Collection<OsmPrimitive> oldSelection;
26
27 /**
28 * Constructs a new select command.
29 * @param newSelection the primitives to select when executing the command.
30 * @deprecated to be removed end of 2017. Use {@link #SelectCommand(DataSet, Collection)} instead
31 */
32 @Deprecated
33 public SelectCommand(Collection<OsmPrimitive> newSelection) {
34 if (newSelection == null || newSelection.isEmpty()) {
35 this.newSelection = Collections.emptySet();
36 } else {
37 this.newSelection = new HashSet<>(newSelection);
38 }
39 }
40
41 /**
42 * Constructs a new select command.
43 * @param dataset The dataset the selection belongs to
44 * @param newSelection the primitives to select when executing the command.
45 * @since 12349
46 */
47 public SelectCommand(DataSet dataset, Collection<OsmPrimitive> newSelection) {
48 super(dataset);
49 if (newSelection == null || newSelection.isEmpty()) {
50 this.newSelection = Collections.emptySet();
51 } else {
52 this.newSelection = new HashSet<>(newSelection);
53 }
54 }
55
56 @Override
57 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
58 // Do nothing
59 }
60
61 @Override
62 public void undoCommand() {
63 ensurePrimitivesAreInDataset();
64
65 getAffectedDataSet().setSelected(oldSelection);
66 }
67
68 @Override
69 public boolean executeCommand() {
70 ensurePrimitivesAreInDataset();
71
72 oldSelection = getAffectedDataSet().getSelected();
73 getAffectedDataSet().setSelected(newSelection);
74 return true;
75 }
76
77 @Override
78 public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
79 return Collections.unmodifiableCollection(newSelection);
80 }
81
82 @Override
83 public String getDescriptionText() {
84 int size = newSelection != null ? newSelection.size() : 0;
85 return trn("Selected {0} object", "Selected {0} objects", size, size);
86 }
87
88 @Override
89 public int hashCode() {
90 return Objects.hash(super.hashCode(), newSelection, oldSelection);
91 }
92
93 @Override
94 public boolean equals(Object obj) {
95 if (this == obj) return true;
96 if (obj == null || getClass() != obj.getClass()) return false;
97 if (!super.equals(obj)) return false;
98 SelectCommand that = (SelectCommand) obj;
99 return Objects.equals(newSelection, that.newSelection) &&
100 Objects.equals(oldSelection, that.oldSelection);
101 }
102}
Note: See TracBrowser for help on using the repository browser.