RobotCore
1.0
WPILib port to raspberry Pi
Toggle main menu visibility
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
5
package
org.wpilib.command3;
6
7
import
static
org.wpilib.util.ErrorMessages.requireNonNullParam;
8
9
import
java.util.Collection;
10
import
java.util.HashSet;
11
import
java.util.LinkedHashSet;
12
import
java.util.Set;
13
20
public
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
58
ConflictDetector
.
throwIfConflicts
(allCommands);
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
}
org.wpilib.command3.ConflictDetector
Definition
ConflictDetector.java:17
org.wpilib.command3.ConflictDetector.throwIfConflicts
static void throwIfConflicts(Collection<? extends Command > commands)
Definition
ConflictDetector.java:55
org.wpilib.command3.Coroutine
Definition
Coroutine.java:35
org.wpilib.command3.Coroutine.awaitAll
ForkResult awaitAll(Collection<? extends Command > commands)
Definition
Coroutine.java:502
org.wpilib.command3.Coroutine.fork
ForkResult fork(Command... commands)
Definition
Coroutine.java:380
org.wpilib.command3.Coroutine.awaitAny
ForkResult awaitAny(Collection<? extends Command > commands)
Definition
Coroutine.java:570
org.wpilib.command3.ParallelGroup.run
void run(Coroutine coroutine)
Definition
ParallelGroup.java:73
org.wpilib.command3.ParallelGroup.priority
int priority()
Definition
ParallelGroup.java:99
org.wpilib.command3.ParallelGroup.name
String name()
Definition
ParallelGroup.java:89
org.wpilib.command3.ParallelGroup.requirements
Set< Mechanism > requirements()
Definition
ParallelGroup.java:94
org.wpilib.command3.Command
Definition
Command.java:98
org.wpilib.command3.Command.DEFAULT_PRIORITY
int DEFAULT_PRIORITY
Definition
Command.java:100
org.wpilib.command3.Command.priority
default int priority()
Definition
Command.java:174
org
wpilib
command3
ParallelGroup.java
Generated by
1.18.0