RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
SequentialGroupBuilder.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.ArrayList;
10import java.util.Arrays;
11import java.util.List;
12import java.util.function.BooleanSupplier;
13import java.util.stream.Collectors;
14import org.wpilib.annotation.NoDiscard;
15
24 private final List<Command> m_steps = new ArrayList<>();
25 private BooleanSupplier m_endCondition;
26
32
40 requireNonNullParam(next, "next", "SequentialGroupBuilder.andThen");
41
42 m_steps.add(next);
43 return this;
44 }
45
53 public SequentialGroupBuilder andThen(Command... nextCommands) {
54 requireNonNullParam(nextCommands, "nextCommands", "SequentialGroupBuilder.andThen");
55 for (int i = 0; i < nextCommands.length; i++) {
56 requireNonNullParam(
57 nextCommands[i], "nextCommands[" + i + "]", "SequentialGroupBuilder.andThen");
58 }
59
60 m_steps.addAll(Arrays.asList(nextCommands));
61 return this;
62 }
63
73 public SequentialGroupBuilder until(BooleanSupplier endCondition) {
74 m_endCondition = endCondition;
75 return this;
76 }
77
84 public Command named(String name) {
85 return named(name, false);
86 }
87
88 private Command named(String name, boolean useAutomaticName) {
89 var seq = new SequentialGroup(name, m_steps);
90 if (m_endCondition == null) {
91 // No custom end condition, return the group as is
92 return seq;
93 }
94
95 var out =
97 .optional(seq, Command.waitUntil(m_endCondition).named("Until Condition"));
98 // We have a custom end condition, so we need to wrap the group in a race
99 return useAutomaticName ? out.withAutomaticName() : out.named(name);
100 }
101
109 return named(m_steps.stream().map(Command::name).collect(Collectors.joining(" -> ")), true);
110 }
111}
SequentialGroupBuilder until(BooleanSupplier endCondition)
SequentialGroupBuilder andThen(Command... nextCommands)
SequentialGroupBuilder andThen(Command next)