Java optimization and formatting fixes (#4857)

This commit is contained in:
Sriman Achanta
2022-12-26 14:37:53 -05:00
committed by GitHub
parent 92149efa11
commit 26bdbf3d41
13 changed files with 21 additions and 35 deletions

View File

@@ -118,8 +118,10 @@ public enum NetworkTableType {
} else if (data instanceof Float) {
return "float";
} else if (data instanceof Long) {
// Checking Long because NT supports 64-bit integers
return "int";
} else if (data instanceof Double || data instanceof Number) {
// If typeof Number class, return "double" as the type. Functions as a "catch-all".
return "double";
} else if (data instanceof String) {
return "string";
@@ -130,6 +132,7 @@ public enum NetworkTableType {
} else if (data instanceof long[] || data instanceof Long[]) {
return "int[]";
} else if (data instanceof double[] || data instanceof Double[] || data instanceof Number[]) {
// If typeof Number class, return "double[]" as the type. Functions as a "catch-all".
return "double[]";
} else if (data instanceof String[]) {
return "string[]";

View File

@@ -15,7 +15,7 @@ public class PubSubOption {
disableRemote,
disableLocal,
excludePublisher,
excludeSelf;
excludeSelf
}
PubSubOption(Kind kind, boolean value) {

View File

@@ -103,10 +103,7 @@ public final class CommandScheduler implements NTSendable, AutoCloseable {
disable();
cancelAll();
});
LiveWindow.setDisabledListener(
() -> {
enable();
});
LiveWindow.setDisabledListener(this::enable);
}
/**

View File

@@ -117,11 +117,7 @@ public class MecanumControllerCommand extends CommandBase {
m_desiredRotation =
requireNonNullParam(desiredRotation, "desiredRotation", "MecanumControllerCommand");
m_maxWheelVelocityMetersPerSecond =
requireNonNullParam(
maxWheelVelocityMetersPerSecond,
"maxWheelVelocityMetersPerSecond",
"MecanumControllerCommand");
m_maxWheelVelocityMetersPerSecond = maxWheelVelocityMetersPerSecond;
m_frontLeftController =
requireNonNullParam(frontLeftController, "frontLeftController", "MecanumControllerCommand");
@@ -258,11 +254,7 @@ public class MecanumControllerCommand extends CommandBase {
m_desiredRotation =
requireNonNullParam(desiredRotation, "desiredRotation", "MecanumControllerCommand");
m_maxWheelVelocityMetersPerSecond =
requireNonNullParam(
maxWheelVelocityMetersPerSecond,
"maxWheelVelocityMetersPerSecond",
"MecanumControllerCommand");
m_maxWheelVelocityMetersPerSecond = maxWheelVelocityMetersPerSecond;
m_frontLeftController = null;
m_rearLeftController = null;

View File

@@ -72,7 +72,7 @@ public class SelectCommand extends CommandBase {
@Override
public void initialize() {
if (m_selector != null) {
if (!m_commands.keySet().contains(m_selector.get())) {
if (!m_commands.containsKey(m_selector.get())) {
m_selectedCommand =
new PrintCommand(
"SelectCommand selector value does not correspond to" + " any command!");

View File

@@ -19,6 +19,7 @@ import edu.wpi.first.math.kinematics.SwerveModulePosition;
import edu.wpi.first.math.numbers.N1;
import edu.wpi.first.math.numbers.N3;
import edu.wpi.first.util.WPIUtilJNI;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
@@ -371,13 +372,13 @@ public class SwerveDrivePoseEstimator {
}
InterpolationRecord record = (InterpolationRecord) obj;
return Objects.equals(gyroAngle, record.gyroAngle)
&& Objects.equals(modulePositions, record.modulePositions)
&& Arrays.equals(modulePositions, record.modulePositions)
&& Objects.equals(poseMeters, record.poseMeters);
}
@Override
public int hashCode() {
return Objects.hash(gyroAngle, modulePositions, poseMeters);
return Objects.hash(gyroAngle, Arrays.hashCode(modulePositions), poseMeters);
}
}
}

View File

@@ -128,9 +128,7 @@ public class LinearFilter {
}
double[] ffGains = new double[taps];
for (int i = 0; i < ffGains.length; i++) {
ffGains[i] = 1.0 / taps;
}
Arrays.fill(ffGains, 1.0 / taps);
double[] fbGains = new double[0];

View File

@@ -46,7 +46,7 @@ public final class SplineHelper {
* Returns quintic splines from a set of waypoints.
*
* @param waypoints The waypoints
* @return List of splines.
* @return array of splines.
*/
public static QuinticHermiteSpline[] getQuinticSplinesFromWaypoints(List<Pose2d> waypoints) {
QuinticHermiteSpline[] splines = new QuinticHermiteSpline[waypoints.size() - 1];

View File

@@ -15,14 +15,13 @@ import edu.wpi.first.math.spline.SplineHelper;
import edu.wpi.first.math.spline.SplineParameterizer;
import edu.wpi.first.math.spline.SplineParameterizer.MalformedSplineException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.BiConsumer;
public final class TrajectoryGenerator {
private static final Trajectory kDoNothingTrajectory =
new Trajectory(Arrays.asList(new Trajectory.State()));
new Trajectory(List.of(new Trajectory.State()));
private static BiConsumer<String, StackTraceElement[]> errorFunc;
/** Private constructor because this is a utility class. */

View File

@@ -4,6 +4,8 @@
package edu.wpi.first.util;
import java.util.Arrays;
/** This is a simple circular buffer so we don't need to "bucket brigade" copy old values. */
public class CircularBuffer {
private double[] m_data;
@@ -21,9 +23,7 @@ public class CircularBuffer {
*/
public CircularBuffer(int size) {
m_data = new double[size];
for (int i = 0; i < m_data.length; i++) {
m_data[i] = 0.0;
}
Arrays.fill(m_data, 0.0);
}
/**
@@ -150,9 +150,7 @@ public class CircularBuffer {
/** Sets internal buffer contents to zero. */
public void clear() {
for (int i = 0; i < m_data.length; i++) {
m_data[i] = 0.0;
}
Arrays.fill(m_data, 0.0);
m_front = 0;
m_length = 0;
}

View File

@@ -34,7 +34,7 @@ public class EventVector {
public void remove(int handle) {
m_lock.lock();
try {
m_events.removeIf(x -> x.intValue() == handle);
m_events.removeIf(x -> x == handle);
} finally {
m_lock.unlock();
}

View File

@@ -92,7 +92,7 @@ public final class RuntimeLoader<T> {
if (hashIs == null) {
throw new IOException(getLoadErrorMessage(ule));
}
try (Scanner scanner = new Scanner(hashIs, StandardCharsets.UTF_8.name())) {
try (Scanner scanner = new Scanner(hashIs, StandardCharsets.UTF_8)) {
String hash = scanner.nextLine();
File jniLibrary = new File(m_extractionRoot, resName + "." + hash);
try {

View File

@@ -9,7 +9,6 @@ import java.io.RandomAccessFile;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.util.NoSuchElementException;
@@ -36,8 +35,7 @@ public class DataLogReader implements Iterable<DataLogRecord> {
public DataLogReader(String filename) throws IOException {
RandomAccessFile f = new RandomAccessFile(filename, "r");
FileChannel channel = f.getChannel();
MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
m_buf = buf;
m_buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
m_buf.order(ByteOrder.LITTLE_ENDIAN);
channel.close();
f.close();