Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Collapse Runnable interfaces into Procedure classes
Browse files Browse the repository at this point in the history
  • Loading branch information
rcahoon committed Sep 12, 2024
1 parent 4a5666f commit 4534427
Show file tree
Hide file tree
Showing 18 changed files with 149 additions and 190 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
id 'java-library'
id 'maven-publish'
id "edu.wpi.first.GradleRIO" version "2024.3.2"
id 'com.diffplug.spotless' version "7.0.0.BETA2"
id 'com.diffplug.spotless' version '6.22.0'
id 'com.google.protobuf' version '0.8.19'
}

Expand Down Expand Up @@ -244,7 +244,7 @@ spotless {
}
toggleOffOn()
googleJavaFormat().formatJavadoc(false).aosp()
// removeUnusedImports() // https://github.com/diffplug/spotless/issues/2159
removeUnusedImports()
trimTrailingWhitespace()
endWithNewline()
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/team766/framework3/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,23 @@ public interface Context {
* calling Procedure.
* In most cases, you want to use runParallel instead of startAsync.
*/
LaunchedContext startAsync(final RunnableWithContext func);
LaunchedContext startAsync(final Procedure func);

/**
* Run the given Procedure synchronously (the calling Procedure will not resume until this one
* has finished).
*/
void runSync(final RunnableWithContext func);
void runSync(final Procedure func);

/**
* Run the given Procedures at the same time. The calling Procedure will resume after all
* Procedures in the group finish.
*/
void runParallel(RunnableWithContext... procedures);
void runParallel(Procedure... procedures);

/**
* Run the given Procedures at the same time. The calling Procedure will resume once any
* Procedure in the group finishes, and the others will be cancelled.
*/
void runParallelRace(RunnableWithContext... procedures);
void runParallelRace(Procedure... procedures);
}
24 changes: 10 additions & 14 deletions src/main/java/com/team766/framework3/ContextImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public boolean succeeded() {
/**
* The top-level procedure being run by this Context.
*/
private final RunnableWithContext m_func;
private final Procedure m_func;

/**
* The OS thread that this Context is executing on.
Expand Down Expand Up @@ -149,7 +149,7 @@ public boolean succeeded() {
* {@link Scheduler#startAsync}.
*/

ContextImpl(final RunnableWithContext func) {
ContextImpl(final Procedure func) {
m_func = func;
Logger.get(Category.FRAMEWORK)
.logRaw(
Expand All @@ -170,10 +170,6 @@ public String getContextName() {
return "Context/" + Integer.toHexString(hashCode()) + "/" + m_func.toString();
}

/* package */ RunnableWithContext getRunnable() {
return m_func;
}

@Override
public String toString() {
String repr = getContextName();
Expand Down Expand Up @@ -349,21 +345,21 @@ public void yield() {
}

@Override
public LaunchedContext startAsync(final RunnableWithContext func) {
public LaunchedContext startAsync(final Procedure func) {
checkProcedureReservationsDisjoint(func);
var context = new ContextImpl(func);
context.schedule();
return context;
}

@Override
public void runSync(final RunnableWithContext func) {
public void runSync(final Procedure func) {
checkProcedureReservationsSubset(func);
func.run(this);
}

@Override
public void runParallel(RunnableWithContext... procedures) {
public void runParallel(Procedure... procedures) {
var contexts = new ContextImpl[procedures.length];
for (int i = 0; i < contexts.length; ++i) {
var procedure = procedures[i];
Expand All @@ -375,7 +371,7 @@ public void runParallel(RunnableWithContext... procedures) {
}

@Override
public void runParallelRace(RunnableWithContext... procedures) {
public void runParallelRace(Procedure... procedures) {
var contexts = new ContextImpl[procedures.length];
for (int i = 0; i < contexts.length; ++i) {
var procedure = procedures[i];
Expand All @@ -386,28 +382,28 @@ public void runParallelRace(RunnableWithContext... procedures) {
runSync(new WPILibCommandProcedure(Commands.race(contexts)));
}

private void checkProcedureReservationsSubset(RunnableWithContext procedure) {
private void checkProcedureReservationsSubset(Procedure procedure) {
final var this_reservations = getRequirements();
for (var req : procedure.reservations()) {
if (!this_reservations.contains(req)) {
throw new IllegalArgumentException(
getName()
+ " tried to run "
+ procedure.toString()
+ procedure.getName()
+ " but is missing the reservation on "
+ req.getName());
}
}
}

private void checkProcedureReservationsDisjoint(RunnableWithContext procedure) {
private void checkProcedureReservationsDisjoint(Procedure procedure) {
final var this_reservations = getRequirements();
for (var req : procedure.reservations()) {
if (this_reservations.contains(req)) {
throw new IllegalArgumentException(
getName()
+ " tried to launch "
+ procedure.toString()
+ procedure.getName()
+ " asynchronously, but both have a reservation on "
+ req.getName());
}
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/team766/framework3/FunctionalProcedure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.team766.framework3;

import edu.wpi.first.wpilibj2.command.Subsystem;
import java.util.Set;
import java.util.function.Consumer;

public final class FunctionalProcedure extends Procedure {
private final Consumer<Context> runnable;

public FunctionalProcedure(Set<Subsystem> reservations, Consumer<Context> runnable) {
super(runnable.toString());
this.runnable = runnable;
for (var r : reservations) {
reserve(r);
}
}

@Override
public void run(Context context) {
runnable.accept(context);
}
}

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/com/team766/framework3/InstantCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
public class InstantCommand extends Command {
private final Runnable runnable;

public InstantCommand(InstantRunnable runnable) {
this(runnable.reservations(), runnable);
public InstantCommand(InstantProcedure procedure) {
this(procedure.reservations(), procedure);
}

public InstantCommand(Set<Subsystem> requirements, Runnable runnable) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/team766/framework3/InstantProcedure.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.team766.framework3;

public abstract non-sealed class InstantProcedure extends ProcedureBase implements InstantRunnable {
public abstract class InstantProcedure extends Procedure implements Runnable {
@Override
public abstract void run();

@Override
public final void run(Context context) {
run();
Expand Down
18 changes: 0 additions & 18 deletions src/main/java/com/team766/framework3/InstantRunnable.java

This file was deleted.

59 changes: 58 additions & 1 deletion src/main/java/com/team766/framework3/Procedure.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,60 @@
package com.team766.framework3;

public abstract class Procedure extends ProcedureBase implements RunnableWithContext {}
import com.google.common.collect.Sets;
import com.team766.logging.Category;
import edu.wpi.first.wpilibj2.command.Subsystem;
import java.util.Set;

public abstract class Procedure implements LoggingBase {
// A reusable Procedure that does nothing.
private static final class NoOpProcedure extends InstantProcedure {
@Override
public void run() {}
}

public static final InstantProcedure NO_OP = new NoOpProcedure();

private static int c_idCounter = 0;

private static synchronized int createNewId() {
return c_idCounter++;
}

private final String name;
private final Set<Subsystem> reservations = Sets.newHashSet();
protected Category loggerCategory = Category.PROCEDURES;

protected Procedure() {
this.name = this.getClass().getName() + "/" + createNewId();
}

protected Procedure(String name) {
this.name = name;
}

public abstract void run(Context context);

@Override
public final String getName() {
return name;
}

@Override
public Category getLoggerCategory() {
return loggerCategory;
}

protected final <M extends Subsystem> M reserve(M m) {
reservations.add(m);
return m;
}

public final Set<Subsystem> reservations() {
return reservations;
}

@Override
public final String toString() {
return getName();
}
}
54 changes: 0 additions & 54 deletions src/main/java/com/team766/framework3/ProcedureBase.java

This file was deleted.

Loading

0 comments on commit 4534427

Please sign in to comment.