Add eager null checks in drive base classes (#1447)

All null motors will be specified in the error messages.
This commit is contained in:
Sam Carlberg
2018-11-27 21:11:56 -05:00
committed by Peter Johnson
parent acb786a791
commit a60f312d19
3 changed files with 89 additions and 0 deletions

View File

@@ -7,6 +7,8 @@
package edu.wpi.first.wpilibj.drive;
import java.util.StringJoiner;
import edu.wpi.first.hal.FRCNetComm.tInstances;
import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
@@ -113,6 +115,7 @@ public class DifferentialDrive extends RobotDriveBase {
* inverted, do so before passing it in.
*/
public DifferentialDrive(SpeedController leftMotor, SpeedController rightMotor) {
verify(leftMotor, rightMotor);
m_leftMotor = leftMotor;
m_rightMotor = rightMotor;
addChild(m_leftMotor);
@@ -121,6 +124,29 @@ public class DifferentialDrive extends RobotDriveBase {
setName("DifferentialDrive", instances);
}
/**
* Verifies that all motors are nonnull, throwing a NullPointerException if any of them are.
* The exception's error message will specify all null motors, e.g. {@code
* NullPointerException("leftMotor, rightMotor")}, to give as much information as possible to
* the programmer.
*
* @throws NullPointerException if any of the given motors are null
*/
@SuppressWarnings("PMD.AvoidThrowingNullPointerException")
private void verify(SpeedController leftMotor, SpeedController rightMotor) {
if (leftMotor != null && rightMotor != null) {
return;
}
StringJoiner joiner = new StringJoiner(", ");
if (leftMotor == null) {
joiner.add("leftMotor");
}
if (rightMotor == null) {
joiner.add("rightMotor");
}
throw new NullPointerException(joiner.toString());
}
/**
* Arcade drive method for differential drive platform.
* The calculated values will be squared to decrease sensitivity at low speeds.

View File

@@ -7,6 +7,8 @@
package edu.wpi.first.wpilibj.drive;
import java.util.StringJoiner;
import edu.wpi.first.hal.FRCNetComm.tInstances;
import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
@@ -87,6 +89,7 @@ public class KilloughDrive extends RobotDriveBase {
public KilloughDrive(SpeedController leftMotor, SpeedController rightMotor,
SpeedController backMotor, double leftMotorAngle, double rightMotorAngle,
double backMotorAngle) {
verify(leftMotor, rightMotor, backMotor);
m_leftMotor = leftMotor;
m_rightMotor = rightMotor;
m_backMotor = backMotor;
@@ -103,6 +106,33 @@ public class KilloughDrive extends RobotDriveBase {
setName("KilloughDrive", instances);
}
/**
* Verifies that all motors are nonnull, throwing a NullPointerException if any of them are.
* The exception's error message will specify all null motors, e.g. {@code
* NullPointerException("leftMotor, rightMotor")}, to give as much information as possible to
* the programmer.
*
* @throws NullPointerException if any of the given motors are null
*/
@SuppressWarnings("PMD.AvoidThrowingNullPointerException")
private void verify(SpeedController leftMotor, SpeedController rightMotor,
SpeedController backMotor) {
if (leftMotor != null && rightMotor != null && backMotor != null) {
return;
}
StringJoiner joiner = new StringJoiner(", ");
if (leftMotor == null) {
joiner.add("leftMotor");
}
if (rightMotor == null) {
joiner.add("rightMotor");
}
if (backMotor == null) {
joiner.add("backMotor");
}
throw new NullPointerException(joiner.toString());
}
/**
* Drive method for Killough platform.
*

View File

@@ -7,6 +7,8 @@
package edu.wpi.first.wpilibj.drive;
import java.util.StringJoiner;
import edu.wpi.first.hal.FRCNetComm.tInstances;
import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
@@ -74,6 +76,7 @@ public class MecanumDrive extends RobotDriveBase {
*/
public MecanumDrive(SpeedController frontLeftMotor, SpeedController rearLeftMotor,
SpeedController frontRightMotor, SpeedController rearRightMotor) {
verify(frontLeftMotor, rearLeftMotor, frontRightMotor, rearRightMotor);
m_frontLeftMotor = frontLeftMotor;
m_rearLeftMotor = rearLeftMotor;
m_frontRightMotor = frontRightMotor;
@@ -86,6 +89,36 @@ public class MecanumDrive extends RobotDriveBase {
setName("MecanumDrive", instances);
}
/**
* Verifies that all motors are nonnull, throwing a NullPointerException if any of them are.
* The exception's error message will specify all null motors, e.g. {@code
* NullPointerException("frontLeftMotor, rearRightMotor")}, to give as much information as
* possible to the programmer.
*
* @throws NullPointerException if any of the given motors are null
*/
@SuppressWarnings({"PMD.AvoidThrowingNullPointerException", "PMD.CyclomaticComplexity"})
private void verify(SpeedController frontLeft, SpeedController rearLeft,
SpeedController frontRight, SpeedController rearRightMotor) {
if (frontLeft != null && rearLeft != null && frontRight != null && rearRightMotor != null) {
return;
}
StringJoiner joiner = new StringJoiner(", ");
if (frontLeft == null) {
joiner.add("frontLeftMotor");
}
if (rearLeft == null) {
joiner.add("rearLeftMotor");
}
if (frontRight == null) {
joiner.add("frontRightMotor");
}
if (rearRightMotor == null) {
joiner.add("rearRightMotor");
}
throw new NullPointerException(joiner.toString());
}
/**
* Drive method for Mecanum platform.
*