Skip to content

Commit

Permalink
Remove unnecessary boxing (#7539)
Browse files Browse the repository at this point in the history
* Remove unnecessary boxing
Also remove unnecessary warning suppression

* Use more idiomatic functional interfaces in NumericalIntegration
  • Loading branch information
KangarooKoala authored Dec 13, 2024
1 parent 39d05eb commit 4225b73
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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: " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -25,13 +26,12 @@ 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<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);
}
Expand All @@ -45,15 +45,13 @@ public static double rk4(DoubleFunction<Double> 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<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);
}
Expand All @@ -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 <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,
Expand All @@ -94,9 +91,8 @@ public static <States extends Num, Inputs extends Num> Matrix<States, N1> 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 <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);
Expand Down Expand Up @@ -145,7 +141,6 @@ public static <Rows extends Num, Cols extends Num> Matrix<Rows, Cols> rk4(
* @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,
Expand All @@ -166,7 +161,6 @@ public static <States extends Num, Inputs extends Num> Matrix<States, N1> 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 <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,
Expand Down Expand Up @@ -291,7 +285,6 @@ public static <States extends Num, Inputs extends Num> Matrix<States, N1> 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 <Rows extends Num, Cols extends Num> Matrix<Rows, Cols> rkdp(
BiFunction<Double, Matrix<Rows, Cols>, Matrix<Rows, Cols>> f,
double t,
Expand Down
2 changes: 1 addition & 1 deletion wpiutil/src/main/java/edu/wpi/first/util/EventVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4225b73

Please sign in to comment.