Remove unnecessary boxing (#7539)

* Remove unnecessary boxing
Also remove unnecessary warning suppression

* Use more idiomatic functional interfaces in NumericalIntegration
This commit is contained in:
Joseph Eng
2024-12-12 19:18:40 -08:00
committed by GitHub
parent 39d05ebe7c
commit 4225b732fd
9 changed files with 28 additions and 42 deletions

View File

@@ -96,8 +96,7 @@ public class PneumaticHub implements PneumaticsBase {
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);
}

View File

@@ -47,8 +47,7 @@ public class PneumaticsControlModule implements PneumaticsBase {
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);
}

View File

@@ -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 @@ public class CounterTest extends AbstractComsSetup {
* @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: " +

View File

@@ -39,7 +39,7 @@ public class DIOCrossConnectTest extends AbstractInterruptTest {
* @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();
}

View File

@@ -51,7 +51,7 @@ public class PDPTest extends AbstractComsSetup {
* @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();

View File

@@ -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 @@ public class PIDTest extends AbstractComsSetup {
* @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();

View File

@@ -37,10 +37,8 @@ public class DIOCrossConnectFixture implements ITestFixture {
* @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;

View File

@@ -8,8 +8,9 @@ import edu.wpi.first.math.Matrix;
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,13 +26,12 @@ public final class 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<Double> 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);
}
@@ -45,15 +45,13 @@ public final class NumericalIntegration {
* @param dtSeconds The time over which to integrate.
* @return The result of Runge Kutta integration (4th order).
*/
@SuppressWarnings("overloads")
public static double rk4(
BiFunction<Double, Double, Double> 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);
}
@@ -69,7 +67,6 @@ public final class NumericalIntegration {
* @param dtSeconds The time over which to integrate.
* @return the integration of dx/dt = f(x, u) for dt.
*/
@SuppressWarnings("overloads")
public static <States extends Num, Inputs extends Num> Matrix<States, N1> rk4(
BiFunction<Matrix<States, N1>, Matrix<Inputs, N1>, Matrix<States, N1>> f,
Matrix<States, N1> x,
@@ -94,9 +91,8 @@ public final class NumericalIntegration {
* @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 <States extends Num> Matrix<States, N1> rk4(
Function<Matrix<States, N1>, Matrix<States, N1>> f, Matrix<States, N1> x, double dtSeconds) {
UnaryOperator<Matrix<States, N1>> f, Matrix<States, N1> x, double dtSeconds) {
final var h = dtSeconds;
Matrix<States, N1> k1 = f.apply(x);
@@ -145,7 +141,6 @@ public final class NumericalIntegration {
* @param dtSeconds The time over which to integrate.
* @return the integration of dx/dt = f(x, u) for dt.
*/
@SuppressWarnings("overloads")
public static <States extends Num, Inputs extends Num> Matrix<States, N1> rkdp(
BiFunction<Matrix<States, N1>, Matrix<Inputs, N1>, Matrix<States, N1>> f,
Matrix<States, N1> x,
@@ -166,7 +161,6 @@ public final class NumericalIntegration {
* @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 <States extends Num, Inputs extends Num> Matrix<States, N1> rkdp(
BiFunction<Matrix<States, N1>, Matrix<Inputs, N1>, Matrix<States, N1>> f,
Matrix<States, N1> x,
@@ -291,7 +285,6 @@ public final class NumericalIntegration {
* @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 <Rows extends Num, Cols extends Num> Matrix<Rows, Cols> rkdp(
BiFunction<Double, Matrix<Rows, Cols>, Matrix<Rows, Cols>> f,
double t,

View File

@@ -48,7 +48,7 @@ public class EventVector {
public void wakeup() {
m_lock.lock();
try {
for (Integer eventHandle : m_events) {
for (int eventHandle : m_events) {
WPIUtilJNI.setEvent(eventHandle);
}
} finally {