RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
ParallelGroupBuilder.java
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5package org.wpilib.command3;
6
7import static org.wpilib.util.ErrorMessages.requireNonNullParam;
8
9import java.util.Arrays;
10import java.util.LinkedHashSet;
11import java.util.Set;
12import java.util.function.BooleanSupplier;
13import java.util.stream.Collectors;
14import org.wpilib.annotation.NoDiscard;
15
24 private final Set<Command> m_optionalCommands = new LinkedHashSet<>();
25 private final Set<Command> m_requiredCommands = new LinkedHashSet<>();
26 private BooleanSupplier m_endCondition;
27
33
42 requireNonNullParam(commands, "commands", "ParallelGroupBuilder.optional");
43 for (int i = 0; i < commands.length; i++) {
44 requireNonNullParam(commands[i], "commands[" + i + "]", "ParallelGroupBuilder.optional");
45 }
46
47 m_optionalCommands.addAll(Arrays.asList(commands));
48 return this;
49 }
50
59 requireNonNullParam(commands, "commands", "ParallelGroupBuilder.requiring");
60 for (int i = 0; i < commands.length; i++) {
61 requireNonNullParam(commands[i], "commands[" + i + "]", "ParallelGroupBuilder.requiring");
62 }
63
64 m_requiredCommands.addAll(Arrays.asList(commands));
65 return this;
66 }
67
74 // Note: this primarily exists so users can cleanly chain .alongWith calls to build a
75 // parallel group, eg `fooCommand().alongWith(barCommand()).alongWith(bazCommand())`
77 return requiring(command);
78 }
79
89 public ParallelGroupBuilder until(BooleanSupplier condition) {
90 m_endCondition = condition;
91 return this;
92 }
93
102 public ParallelGroup named(String name) {
103 return named(name, false);
104 }
105
106 private ParallelGroup named(String name, boolean useAutomaticName) {
107 requireNonNullParam(name, "name", "ParallelGroupBuilder.named");
108
109 var group = new ParallelGroup(name, m_requiredCommands, m_optionalCommands);
110 if (m_endCondition == null) {
111 // No custom end condition, return the group as is
112 return group;
113 }
114
115 // We have a custom end condition, so we need to wrap the group in a race
116 var out =
118 .optional(group, Command.waitUntil(m_endCondition).named("Until Condition"));
119 return useAutomaticName ? out.withAutomaticName() : out.named(name);
120 }
121
128 // eg "(CommandA & CommandB & CommandC)"
129 String required =
130 m_requiredCommands.stream().map(Command::name).collect(Collectors.joining(" & ", "(", ")"));
131
132 // eg "(CommandA | CommandB | CommandC)"
133 String optional =
134 m_optionalCommands.stream().map(Command::name).collect(Collectors.joining(" | ", "(", ")"));
135
136 if (m_requiredCommands.isEmpty()) {
137 // No required commands, pure race
138 return named(optional, true);
139 } else if (m_optionalCommands.isEmpty()) {
140 // Everything required
141 return named(required, true);
142 } else {
143 // Some form of deadline
144 // eg "[(CommandA & CommandB) * (CommandX | CommandY | ...)]"
145 String name = "[%s * %s]".formatted(required, optional);
146 return named(name, true);
147 }
148 }
149}
ParallelGroupBuilder optional(Command... commands)
ParallelGroupBuilder alongWith(Command command)
ParallelGroupBuilder requiring(Command... commands)
ParallelGroupBuilder until(BooleanSupplier condition)