Skip to content

Commit

Permalink
Replaced stream api usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel1464 committed Nov 5, 2024
1 parent c5432ed commit 09cce4b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

package edu.wpi.first.wpilibj2.command;

import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
* A command composition that runs a set of commands in parallel, ending when the last command ends.
Expand Down Expand Up @@ -52,20 +51,26 @@ public final void addCommands(Command... commands) {
CommandScheduler.getInstance().registerComposedCommands(commands);

for (Command command : commands) {
var currentRequirements = getRequirements();
if (!Collections.disjoint(command.getRequirements(), currentRequirements)) {
String requirementsStr = command.getRequirements()
.stream()
.filter(currentRequirements::contains)
.map(Subsystem::getName)
.collect(Collectors.joining(", "));
var sharedRequirements = new HashSet<>(this.getRequirements());
sharedRequirements.retainAll(command.getRequirements());
if (!sharedRequirements.isEmpty()) {
StringBuilder sharedRequirementsStr = new StringBuilder();
boolean first = true;
for (Subsystem requirement: sharedRequirements) {
if (first) {
first = false;
} else {
sharedRequirementsStr.append(", ");
}
sharedRequirementsStr.append(requirement.getName());
}
throw new IllegalArgumentException(
String.format(
"Command %s could not be added to this ParallelCommandGroup"
+ " because the subsystems [%s] are already required in this command."
+ " Multiple commands in a parallel composition cannot require"
+ " the same subsystems.",
command.getName(), requirementsStr));
command.getName(), sharedRequirementsStr));
}
m_commands.put(command, false);
addRequirements(command.getRequirements());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
package edu.wpi.first.wpilibj2.command;

import edu.wpi.first.util.sendable.SendableBuilder;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
* A command composition that runs a set of commands in parallel, ending only when a specific
Expand Down Expand Up @@ -82,20 +81,26 @@ public final void addCommands(Command... commands) {
CommandScheduler.getInstance().registerComposedCommands(commands);

for (Command command : commands) {
var currentRequirements = getRequirements();
if (!Collections.disjoint(command.getRequirements(), currentRequirements)) {
String requirementsStr = command.getRequirements()
.stream()
.filter(currentRequirements::contains)
.map(Subsystem::getName)
.collect(Collectors.joining(", "));
var sharedRequirements = new HashSet<>(this.getRequirements());
sharedRequirements.retainAll(command.getRequirements());
if (!sharedRequirements.isEmpty()) {
StringBuilder sharedRequirementsStr = new StringBuilder();
boolean first = true;
for (Subsystem requirement: sharedRequirements) {
if (first) {
first = false;
} else {
sharedRequirementsStr.append(", ");
}
sharedRequirementsStr.append(requirement.getName());
}
throw new IllegalArgumentException(
String.format(
"Command %s could not be added to this ParallelCommandGroup"
+ " because the subsystems [%s] are already required in this command."
+ " Multiple commands in a parallel composition cannot require"
+ " the same subsystems.",
command.getName(), requirementsStr));
command.getName(), sharedRequirementsStr));
}
m_commands.put(command, false);
addRequirements(command.getRequirements());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

package edu.wpi.first.wpilibj2.command;

import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
* A composition that runs a set of commands in parallel, ending when any one of the commands ends
Expand Down Expand Up @@ -53,20 +52,26 @@ public final void addCommands(Command... commands) {
CommandScheduler.getInstance().registerComposedCommands(commands);

for (Command command : commands) {
var currentRequirements = getRequirements();
if (!Collections.disjoint(command.getRequirements(), currentRequirements)) {
String requirementsStr = command.getRequirements()
.stream()
.filter(currentRequirements::contains)
.map(Subsystem::getName)
.collect(Collectors.joining(", "));
var sharedRequirements = new HashSet<>(this.getRequirements());
sharedRequirements.retainAll(command.getRequirements());
if (!sharedRequirements.isEmpty()) {
StringBuilder sharedRequirementsStr = new StringBuilder();
boolean first = true;
for (Subsystem requirement: sharedRequirements) {
if (first) {
first = false;
} else {
sharedRequirementsStr.append(", ");
}
sharedRequirementsStr.append(requirement.getName());
}
throw new IllegalArgumentException(
String.format(
"Command %s could not be added to this ParallelCommandGroup"
+ " because the subsystems [%s] are already required in this command."
+ " Multiple commands in a parallel composition cannot require"
+ " the same subsystems.",
command.getName(), requirementsStr));
command.getName(), sharedRequirementsStr));
}
m_commands.add(command);
addRequirements(command.getRequirements());
Expand Down

0 comments on commit 09cce4b

Please sign in to comment.