source: josm/trunk/src/org/openstreetmap/josm/command/SequenceCommand.java@ 12167

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

findbugs - UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD

  • Property svn:eol-style set to native
File size: 5.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.Arrays;
7import java.util.Collection;
8import java.util.HashSet;
9import java.util.Objects;
10
11import javax.swing.Icon;
12
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.tools.ImageProvider;
15import org.openstreetmap.josm.tools.Utils;
16
17/**
18 * A command consisting of a sequence of other commands. Executes the other commands
19 * and undo them in reverse order.
20 * @author imi
21 * @since 31
22 */
23public class SequenceCommand extends Command {
24
25 /** The command sequence to be executed. */
26 private Command[] sequence;
27 private boolean sequenceComplete;
28 private final String name;
29 /** Determines if the sequence execution should continue after one of its commands fails. */
30 protected final boolean continueOnError;
31
32 /**
33 * Create the command by specifying the list of commands to execute.
34 * @param name The description text
35 * @param sequenz The sequence that should be executed
36 * @param continueOnError Determines if the sequence execution should continue after one of its commands fails
37 * @since 11874
38 */
39 public SequenceCommand(String name, Collection<Command> sequenz, boolean continueOnError) {
40 this.name = name;
41 this.sequence = sequenz.toArray(new Command[sequenz.size()]);
42 this.continueOnError = continueOnError;
43 }
44
45 /**
46 * Create the command by specifying the list of commands to execute.
47 * @param name The description text
48 * @param sequenz The sequence that should be executed.
49 */
50 public SequenceCommand(String name, Collection<Command> sequenz) {
51 this(name, sequenz, false);
52 }
53
54 /**
55 * Convenient constructor, if the commands are known at compile time.
56 * @param name The description text
57 * @param sequenz The sequence that should be executed.
58 */
59 public SequenceCommand(String name, Command... sequenz) {
60 this(name, Arrays.asList(sequenz));
61 }
62
63 @Override public boolean executeCommand() {
64 for (int i = 0; i < sequence.length; i++) {
65 boolean result = sequence[i].executeCommand();
66 if (!result && !continueOnError) {
67 undoCommands(i-1);
68 return false;
69 }
70 }
71 sequenceComplete = true;
72 return true;
73 }
74
75 /**
76 * Returns the last command.
77 * @return The last command, or {@code null} if the sequence is empty.
78 */
79 public Command getLastCommand() {
80 if (sequence.length == 0)
81 return null;
82 return sequence[sequence.length-1];
83 }
84
85 protected final void undoCommands(int start) {
86 for (int i = start; i >= 0; --i) {
87 sequence[i].undoCommand();
88 }
89 }
90
91 @Override public void undoCommand() {
92 // We probably aborted this halfway though the
93 // execution sequence because of a sub-command
94 // error. We already undid the sub-commands.
95 if (!sequenceComplete)
96 return;
97 undoCommands(sequence.length-1);
98 }
99
100 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
101 for (Command c : sequence) {
102 c.fillModifiedData(modified, deleted, added);
103 }
104 }
105
106 @Override
107 public String getDescriptionText() {
108 return tr("Sequence: {0}", name);
109 }
110
111 @Override
112 public Icon getDescriptionIcon() {
113 return ImageProvider.get("data", "sequence");
114 }
115
116 @Override
117 public Collection<PseudoCommand> getChildren() {
118 return Arrays.<PseudoCommand>asList(sequence);
119 }
120
121 @Override
122 public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
123 Collection<OsmPrimitive> prims = new HashSet<>();
124 for (Command c : sequence) {
125 prims.addAll(c.getParticipatingPrimitives());
126 }
127 return prims;
128 }
129
130 protected final void setSequence(Command... sequence) {
131 this.sequence = Utils.copyArray(sequence);
132 }
133
134 protected final void setSequenceComplete(boolean sequenceComplete) {
135 this.sequenceComplete = sequenceComplete;
136 }
137
138 @Override
139 public void invalidateAffectedLayers() {
140 super.invalidateAffectedLayers();
141 for (Command c : sequence) {
142 c.invalidateAffectedLayers();
143 }
144 }
145
146 @Override
147 public int hashCode() {
148 return Objects.hash(super.hashCode(), Arrays.hashCode(sequence), sequenceComplete, name, continueOnError);
149 }
150
151 @Override
152 public boolean equals(Object obj) {
153 if (this == obj) return true;
154 if (obj == null || getClass() != obj.getClass()) return false;
155 if (!super.equals(obj)) return false;
156 SequenceCommand that = (SequenceCommand) obj;
157 return sequenceComplete == that.sequenceComplete &&
158 continueOnError == that.continueOnError &&
159 Arrays.equals(sequence, that.sequence) &&
160 Objects.equals(name, that.name);
161 }
162}
Note: See TracBrowser for help on using the repository browser.