RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
ParallelGroup.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.Collection;
10import java.util.HashSet;
11import java.util.LinkedHashSet;
12import java.util.Set;
13
20public final class ParallelGroup implements Command {
21 private final Collection<Command> m_requiredCommands = new HashSet<>();
22 private final Collection<Command> m_optionalCommands = new LinkedHashSet<>();
23 private final Set<Mechanism> m_requirements = new HashSet<>();
24 private final String m_name;
25 private final int m_priority;
26
36 ParallelGroup(
37 String name, Collection<Command> requiredCommands, Collection<Command> optionalCommands) {
38 requireNonNullParam(name, "name", "ParallelGroup");
39 requireNonNullParam(requiredCommands, "requiredCommands", "ParallelGroup");
40 requireNonNullParam(optionalCommands, "optionalCommands", "ParallelGroup");
41
42 int i = 0;
43 for (Command requiredCommand : requiredCommands) {
44 requireNonNullParam(requiredCommand, "requiredCommands[" + i + "]", "ParallelGroup");
45 i++;
46 }
47
48 i = 0;
49 for (Command c : optionalCommands) {
50 requireNonNullParam(c, "optionalCommands[" + i + "]", "ParallelGroup");
51 i++;
52 }
53
54 var allCommands = new LinkedHashSet<Command>();
55 allCommands.addAll(requiredCommands);
56 allCommands.addAll(optionalCommands);
57
59
60 m_name = name;
61 m_optionalCommands.addAll(optionalCommands);
62 m_requiredCommands.addAll(requiredCommands);
63
64 for (var command : allCommands) {
65 m_requirements.addAll(command.requirements());
66 }
67
68 m_priority =
69 allCommands.stream().mapToInt(Command::priority).max().orElse(Command.DEFAULT_PRIORITY);
70 }
71
72 @Override
73 public void run(Coroutine coroutine) {
74 coroutine.fork(m_optionalCommands);
75
76 if (m_requiredCommands.isEmpty()) {
77 // No required commands - just wait for the first optional command to finish
78 coroutine.awaitAny(m_optionalCommands);
79 } else {
80 // Wait for every required command to finish
81 coroutine.awaitAll(m_requiredCommands);
82 }
83
84 // The scheduler will cancel any optional child commands that are still running when
85 // the `run` method returns
86 }
87
88 @Override
89 public String name() {
90 return m_name;
91 }
92
93 @Override
94 public Set<Mechanism> requirements() {
95 return m_requirements;
96 }
97
98 @Override
99 public int priority() {
100 return m_priority;
101 }
102
103 @Override
104 public String toString() {
105 return "ParallelGroup[name=" + m_name + "]";
106 }
107
108 // package-private for testing
109
110 Collection<Command> getRequiredCommands() {
111 return m_requiredCommands;
112 }
113
114 Collection<Command> getOptionalCommands() {
115 return m_optionalCommands;
116 }
117}
static void throwIfConflicts(Collection<? extends Command > commands)
ForkResult awaitAll(Collection<? extends Command > commands)
ForkResult fork(Command... commands)
ForkResult awaitAny(Collection<? extends Command > commands)
void run(Coroutine coroutine)