From 5038c1a07a9587973231773855cf572e1b870652 Mon Sep 17 00:00:00 2001 From: Joseph Eng Date: Wed, 11 Dec 2024 17:17:27 -0800 Subject: [PATCH 1/2] Remove unnecessary boxing Also remove unnecessary warning suppression --- .../main/java/edu/wpi/first/wpilibj/PneumaticHub.java | 3 +-- .../edu/wpi/first/wpilibj/PneumaticsControlModule.java | 3 +-- .../src/main/java/edu/wpi/first/wpilibj/CounterTest.java | 9 +++------ .../java/edu/wpi/first/wpilibj/DIOCrossConnectTest.java | 2 +- .../src/main/java/edu/wpi/first/wpilibj/PDPTest.java | 2 +- .../src/main/java/edu/wpi/first/wpilibj/PIDTest.java | 8 ++++---- .../first/wpilibj/fixtures/DIOCrossConnectFixture.java | 6 ++---- .../edu/wpi/first/math/system/NumericalIntegration.java | 9 +-------- .../src/main/java/edu/wpi/first/util/EventVector.java | 2 +- 9 files changed, 15 insertions(+), 29 deletions(-) diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PneumaticHub.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PneumaticHub.java index dedd790b45f..127fb94f4f0 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PneumaticHub.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PneumaticHub.java @@ -96,8 +96,7 @@ public void removeRef() { private static DataStore getForModule(int module) { synchronized (m_handleLock) { - Integer moduleBoxed = module; - DataStore pcm = m_handleMap.get(moduleBoxed); + DataStore pcm = m_handleMap.get(module); if (pcm == null) { pcm = new DataStore(module); } diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PneumaticsControlModule.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PneumaticsControlModule.java index 4f1d70c5161..c075c46a755 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PneumaticsControlModule.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PneumaticsControlModule.java @@ -47,8 +47,7 @@ public void removeRef() { private static DataStore getForModule(int module) { synchronized (m_handleLock) { - Integer moduleBoxed = module; - DataStore pcm = m_handleMap.get(moduleBoxed); + DataStore pcm = m_handleMap.get(module); if (pcm == null) { pcm = new DataStore(module); } diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/CounterTest.java b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/CounterTest.java index 793c7e939e2..9fa5e0aae77 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/CounterTest.java +++ b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/CounterTest.java @@ -26,8 +26,8 @@ public class CounterTest extends AbstractComsSetup { private static FakeCounterFixture counter = null; private static final Logger logger = Logger.getLogger(CounterTest.class.getName()); - Integer m_input; - Integer m_output; + int m_input; + int m_output; @Override protected Logger getClassLogger() { @@ -40,10 +40,7 @@ protected Logger getClassLogger() { * @param input The input Port * @param output The output Port */ - public CounterTest(Integer input, Integer output) { - assert input != null; - assert output != null; - + public CounterTest(int input, int output) { m_input = input; m_output = output; // System.out.println("Counter Test: Input: " + input + " Output: " + diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/DIOCrossConnectTest.java b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/DIOCrossConnectTest.java index 97405ce5cab..9a3ee5c8017 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/DIOCrossConnectTest.java +++ b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/DIOCrossConnectTest.java @@ -39,7 +39,7 @@ protected Logger getClassLogger() { * @param input The port for the input wire * @param output The port for the output wire */ - public DIOCrossConnectTest(Integer input, Integer output) { + public DIOCrossConnectTest(int input, int output) { if (dio != null) { dio.teardown(); } diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PDPTest.java b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PDPTest.java index d668d16d5a0..eb7fda51794 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PDPTest.java +++ b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PDPTest.java @@ -51,7 +51,7 @@ public static void tearDownAfterClass() { * @param mef Motor encoder fixture. * @param expectedCurrentDraw Expected current draw in Amps. */ - public PDPTest(MotorEncoderFixture mef, Double expectedCurrentDraw) { + public PDPTest(MotorEncoderFixture mef, double expectedCurrentDraw) { logger.fine("Constructor with: " + mef.getType()); if (me != null && !me.equals(mef)) { me.teardown(); diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PIDTest.java b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PIDTest.java index 909194556c8..90c41eef97d 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PIDTest.java +++ b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PIDTest.java @@ -41,9 +41,9 @@ public class PIDTest extends AbstractComsSetup { private PIDController m_controller = null; private static MotorEncoderFixture me = null; - private final Double m_p; - private final Double m_i; - private final Double m_d; + private final double m_p; + private final double m_i; + private final double m_d; @Override protected Logger getClassLogger() { @@ -58,7 +58,7 @@ protected Logger getClassLogger() { * @param d D gain. * @param mef Motor encoder fixture. */ - public PIDTest(Double p, Double i, Double d, MotorEncoderFixture mef) { + public PIDTest(double p, double i, double d, MotorEncoderFixture mef) { logger.fine("Constructor with: " + mef.getType()); if (PIDTest.me != null && !PIDTest.me.equals(mef)) { PIDTest.me.teardown(); diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/fixtures/DIOCrossConnectFixture.java b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/fixtures/DIOCrossConnectFixture.java index 78880d84a71..03aacb6a7da 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/fixtures/DIOCrossConnectFixture.java +++ b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/fixtures/DIOCrossConnectFixture.java @@ -37,10 +37,8 @@ public DIOCrossConnectFixture(DigitalInput input, DigitalOutput output) { * @param input The port of the {@link DigitalInput} * @param output The port of the {@link DigitalOutput} */ - public DIOCrossConnectFixture(Integer input, Integer output) { - assert input != null; - assert output != null; - assert !input.equals(output); + public DIOCrossConnectFixture(int input, int output) { + assert input != output; m_input = new DigitalInput(input); m_output = new DigitalOutput(output); m_allocated = true; diff --git a/wpimath/src/main/java/edu/wpi/first/math/system/NumericalIntegration.java b/wpimath/src/main/java/edu/wpi/first/math/system/NumericalIntegration.java index 1dcddea8404..8fa8ceb8f4a 100644 --- a/wpimath/src/main/java/edu/wpi/first/math/system/NumericalIntegration.java +++ b/wpimath/src/main/java/edu/wpi/first/math/system/NumericalIntegration.java @@ -25,7 +25,6 @@ private NumericalIntegration() { * @param dtSeconds The time over which to integrate. * @return the integration of dx/dt = f(x) for dt. */ - @SuppressWarnings("overloads") public static double rk4(DoubleFunction f, double x, double dtSeconds) { final var h = dtSeconds; final var k1 = f.apply(x); @@ -45,9 +44,8 @@ public static double rk4(DoubleFunction f, double x, double dtSeconds) { * @param dtSeconds The time over which to integrate. * @return The result of Runge Kutta integration (4th order). */ - @SuppressWarnings("overloads") public static double rk4( - BiFunction f, double x, Double u, double dtSeconds) { + BiFunction f, double x, double u, double dtSeconds) { final var h = dtSeconds; final var k1 = f.apply(x, u); @@ -69,7 +67,6 @@ public static double rk4( * @param dtSeconds The time over which to integrate. * @return the integration of dx/dt = f(x, u) for dt. */ - @SuppressWarnings("overloads") public static Matrix rk4( BiFunction, Matrix, Matrix> f, Matrix x, @@ -94,7 +91,6 @@ public static Matrix rk4( * @param dtSeconds The time over which to integrate. * @return 4th order Runge-Kutta integration of dx/dt = f(x) for dt. */ - @SuppressWarnings("overloads") public static Matrix rk4( Function, Matrix> f, Matrix x, double dtSeconds) { final var h = dtSeconds; @@ -145,7 +141,6 @@ public static Matrix rk4( * @param dtSeconds The time over which to integrate. * @return the integration of dx/dt = f(x, u) for dt. */ - @SuppressWarnings("overloads") public static Matrix rkdp( BiFunction, Matrix, Matrix> f, Matrix x, @@ -166,7 +161,6 @@ public static Matrix rkdp( * @param maxError The maximum acceptable truncation error. Usually a small number like 1e-6. * @return the integration of dx/dt = f(x, u) for dt. */ - @SuppressWarnings("overloads") public static Matrix rkdp( BiFunction, Matrix, Matrix> f, Matrix x, @@ -291,7 +285,6 @@ public static Matrix rkdp( * @param maxError The maximum acceptable truncation error. Usually a small number like 1e-6. * @return the integration of dx/dt = f(x, u) for dt. */ - @SuppressWarnings("overloads") public static Matrix rkdp( BiFunction, Matrix> f, double t, diff --git a/wpiutil/src/main/java/edu/wpi/first/util/EventVector.java b/wpiutil/src/main/java/edu/wpi/first/util/EventVector.java index 60fb5d13d09..9257d9706f7 100644 --- a/wpiutil/src/main/java/edu/wpi/first/util/EventVector.java +++ b/wpiutil/src/main/java/edu/wpi/first/util/EventVector.java @@ -48,7 +48,7 @@ public void remove(int handle) { public void wakeup() { m_lock.lock(); try { - for (Integer eventHandle : m_events) { + for (int eventHandle : m_events) { WPIUtilJNI.setEvent(eventHandle); } } finally { From cb741b03bf295b349b3755cb36430e61119888f5 Mon Sep 17 00:00:00 2001 From: Joseph Eng Date: Wed, 11 Dec 2024 20:13:04 -0800 Subject: [PATCH 2/2] Use more idiomatic functional interfaces in NumericalIntegration --- .../math/system/NumericalIntegration.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/wpimath/src/main/java/edu/wpi/first/math/system/NumericalIntegration.java b/wpimath/src/main/java/edu/wpi/first/math/system/NumericalIntegration.java index 8fa8ceb8f4a..a54e2eae448 100644 --- a/wpimath/src/main/java/edu/wpi/first/math/system/NumericalIntegration.java +++ b/wpimath/src/main/java/edu/wpi/first/math/system/NumericalIntegration.java @@ -8,8 +8,9 @@ import edu.wpi.first.math.Num; import edu.wpi.first.math.numbers.N1; import java.util.function.BiFunction; -import java.util.function.DoubleFunction; -import java.util.function.Function; +import java.util.function.DoubleBinaryOperator; +import java.util.function.DoubleUnaryOperator; +import java.util.function.UnaryOperator; /** Numerical integration utilities. */ public final class NumericalIntegration { @@ -25,12 +26,12 @@ private NumericalIntegration() { * @param dtSeconds The time over which to integrate. * @return the integration of dx/dt = f(x) for dt. */ - public static double rk4(DoubleFunction f, double x, double dtSeconds) { + public static double rk4(DoubleUnaryOperator f, double x, double dtSeconds) { final var h = dtSeconds; - final var k1 = f.apply(x); - final var k2 = f.apply(x + h * k1 * 0.5); - final var k3 = f.apply(x + h * k2 * 0.5); - final var k4 = f.apply(x + h * k3); + final var k1 = f.applyAsDouble(x); + final var k2 = f.applyAsDouble(x + h * k1 * 0.5); + final var k3 = f.applyAsDouble(x + h * k2 * 0.5); + final var k4 = f.applyAsDouble(x + h * k3); return x + h / 6.0 * (k1 + 2.0 * k2 + 2.0 * k3 + k4); } @@ -44,14 +45,13 @@ public static double rk4(DoubleFunction f, double x, double dtSeconds) { * @param dtSeconds The time over which to integrate. * @return The result of Runge Kutta integration (4th order). */ - public static double rk4( - BiFunction f, double x, double u, double dtSeconds) { + public static double rk4(DoubleBinaryOperator f, double x, double u, double dtSeconds) { final var h = dtSeconds; - final var k1 = f.apply(x, u); - final var k2 = f.apply(x + h * k1 * 0.5, u); - final var k3 = f.apply(x + h * k2 * 0.5, u); - final var k4 = f.apply(x + h * k3, u); + final var k1 = f.applyAsDouble(x, u); + final var k2 = f.applyAsDouble(x + h * k1 * 0.5, u); + final var k3 = f.applyAsDouble(x + h * k2 * 0.5, u); + final var k4 = f.applyAsDouble(x + h * k3, u); return x + h / 6.0 * (k1 + 2.0 * k2 + 2.0 * k3 + k4); } @@ -92,7 +92,7 @@ public static Matrix rk4( * @return 4th order Runge-Kutta integration of dx/dt = f(x) for dt. */ public static Matrix rk4( - Function, Matrix> f, Matrix x, double dtSeconds) { + UnaryOperator> f, Matrix x, double dtSeconds) { final var h = dtSeconds; Matrix k1 = f.apply(x);