Implements AutoCloseable for types, replacing free() (#1048)

This commit is contained in:
Thad House
2018-05-22 23:33:17 -07:00
committed by Peter Johnson
parent a2ecb1027a
commit cbaff52850
58 changed files with 242 additions and 171 deletions

View File

@@ -93,9 +93,9 @@ public class ADXL345_I2C extends SensorBase implements Accelerometer, Sendable {
}
@Override
public void free() {
super.free();
m_i2c.free();
public void close() {
super.close();
m_i2c.close();
}
@Override

View File

@@ -79,9 +79,9 @@ public class ADXL345_SPI extends SensorBase implements Accelerometer, Sendable {
}
@Override
public void free() {
super.free();
m_spi.free();
public void close() {
super.close();
m_spi.close();
}
/**

View File

@@ -93,7 +93,7 @@ public class ADXL362 extends SensorBase implements Accelerometer, Sendable {
transferBuffer.put(1, kPartIdRegister);
m_spi.transaction(transferBuffer, transferBuffer, 3);
if (transferBuffer.get(2) != (byte) 0xF2) {
m_spi.free();
m_spi.close();
m_spi = null;
DriverStation.reportError("could not find ADXL362 on SPI port " + port.value, false);
return;
@@ -112,10 +112,10 @@ public class ADXL362 extends SensorBase implements Accelerometer, Sendable {
}
@Override
public void free() {
super.free();
public void close() {
super.close();
if (m_spi != null) {
m_spi.free();
m_spi.close();
m_spi = null;
}
}

View File

@@ -64,7 +64,7 @@ public class ADXRS450_Gyro extends GyroBase implements Gyro, PIDSource, Sendable
// Validate the part ID
if ((readRegister(kPIDRegister) & 0xff00) != 0x5200) {
m_spi.free();
m_spi.close();
m_spi = null;
DriverStation.reportError("could not find ADXRS450 gyro on SPI port " + port.value,
false);
@@ -141,10 +141,10 @@ public class ADXRS450_Gyro extends GyroBase implements Gyro, PIDSource, Sendable
* Delete (free) the spi port used for the gyro and stop accumulating.
*/
@Override
public void free() {
super.free();
public void close() {
super.close();
if (m_spi != null) {
m_spi.free();
m_spi.close();
m_spi = null;
}
}

View File

@@ -67,10 +67,10 @@ public class AnalogAccelerometer extends SensorBase implements PIDSource, Sendab
* Delete the analog components used for the accelerometer.
*/
@Override
public void free() {
super.free();
public void close() {
super.close();
if (m_analogChannel != null && m_allocatedChannel) {
m_analogChannel.free();
m_analogChannel.close();
}
m_analogChannel = null;
}

View File

@@ -119,10 +119,10 @@ public class AnalogGyro extends GyroBase implements Gyro, PIDSource, Sendable {
* Delete (free) the accumulator and the analog components used for the gyro.
*/
@Override
public void free() {
super.free();
public void close() {
super.close();
if (m_analog != null && m_channelAllocated) {
m_analog.free();
m_analog.close();
}
m_analog = null;
AnalogGyroJNI.freeAnalogGyro(m_gyroHandle);

View File

@@ -54,8 +54,8 @@ public class AnalogInput extends SensorBase implements PIDSource, Sendable {
* Channel destructor.
*/
@Override
public void free() {
super.free();
public void close() {
super.close();
AnalogJNI.freeAnalogInputPort(m_port);
m_port = 0;
m_channel = 0;

View File

@@ -40,8 +40,8 @@ public class AnalogOutput extends SendableBase implements Sendable {
* Channel destructor.
*/
@Override
public void free() {
super.free();
public void close() {
super.close();
AnalogJNI.freeAnalogOutputPort(m_port);
m_port = 0;
m_channel = 0;

View File

@@ -158,10 +158,10 @@ public class AnalogPotentiometer extends SensorBase implements Potentiometer, Se
* Frees this resource.
*/
@Override
public void free() {
super.free();
public void close() {
super.close();
if (m_initAnalogInput) {
m_analogInput.free();
m_analogInput.close();
m_analogInput = null;
m_initAnalogInput = false;
}

View File

@@ -78,12 +78,12 @@ public class AnalogTrigger extends SensorBase implements Sendable {
* Release the resources used by this object.
*/
@Override
public void free() {
super.free();
public void close() {
super.close();
AnalogJNI.cleanAnalogTrigger(m_port);
m_port = 0;
if (m_ownsAnalog && m_analogInput != null) {
m_analogInput.free();
m_analogInput.close();
}
}

View File

@@ -180,8 +180,8 @@ public class Counter extends SensorBase implements CounterBase, Sendable, PIDSou
}
@Override
public void free() {
super.free();
public void close() {
super.close();
setUpdateWhenEmpty(true);
clearUpSource();
@@ -222,7 +222,7 @@ public class Counter extends SensorBase implements CounterBase, Sendable, PIDSou
*/
public void setUpSource(DigitalSource source) {
if (m_upSource != null && m_allocatedUpSource) {
m_upSource.free();
m_upSource.close();
m_allocatedUpSource = false;
}
m_upSource = source;
@@ -263,7 +263,7 @@ public class Counter extends SensorBase implements CounterBase, Sendable, PIDSou
*/
public void clearUpSource() {
if (m_upSource != null && m_allocatedUpSource) {
m_upSource.free();
m_upSource.close();
m_allocatedUpSource = false;
}
m_upSource = null;
@@ -292,7 +292,7 @@ public class Counter extends SensorBase implements CounterBase, Sendable, PIDSou
requireNonNull(source, "The Digital Source given was null");
if (m_downSource != null && m_allocatedDownSource) {
m_downSource.free();
m_downSource.close();
m_allocatedDownSource = false;
}
CounterJNI.setCounterDownSource(m_counter, source.getPortHandleForRouting(),
@@ -332,7 +332,7 @@ public class Counter extends SensorBase implements CounterBase, Sendable, PIDSou
*/
public void clearDownSource() {
if (m_downSource != null && m_allocatedDownSource) {
m_downSource.free();
m_downSource.close();
m_allocatedDownSource = false;
}
m_downSource = null;

View File

@@ -43,8 +43,8 @@ public class DigitalGlitchFilter extends SensorBase {
/**
* Free the resources used by this object.
*/
public void free() {
super.free();
public void close() {
super.close();
if (m_channelIndex >= 0) {
synchronized (m_mutex) {
m_filterAllocated[m_channelIndex] = false;

View File

@@ -40,8 +40,8 @@ public class DigitalInput extends DigitalSource implements Sendable {
/**
* Frees the resources for this output.
*/
public void free() {
super.free();
public void close() {
super.close();
if (m_interrupt != 0) {
cancelInterrupts();
}

View File

@@ -44,8 +44,8 @@ public class DigitalOutput extends SendableBase implements Sendable {
* Free the resources associated with a digital output.
*/
@Override
public void free() {
super.free();
public void close() {
super.close();
// disable the pwm only if we have allocated it
if (m_pwmGenerator != invalidPwmGenerator) {
disablePWM();

View File

@@ -86,11 +86,10 @@ public class DoubleSolenoid extends SolenoidBase implements Sendable {
* Destructor.
*/
@Override
public synchronized void free() {
super.free();
public synchronized void close() {
super.close();
SolenoidJNI.freeSolenoidPort(m_forwardHandle);
SolenoidJNI.freeSolenoidPort(m_reverseHandle);
super.free();
}
/**

View File

@@ -294,18 +294,18 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, Senda
* Free the resources used by this object.
*/
@Override
public void free() {
super.free();
public void close() {
super.close();
if (m_aSource != null && m_allocatedA) {
m_aSource.free();
m_aSource.close();
m_allocatedA = false;
}
if (m_bSource != null && m_allocatedB) {
m_bSource.free();
m_bSource.close();
m_allocatedB = false;
}
if (m_indexSource != null && m_allocatedI) {
m_indexSource.free();
m_indexSource.close();
m_allocatedI = false;
}

View File

@@ -22,7 +22,7 @@ import static java.util.Objects.requireNonNull;
* <p>This class is intended to be used by sensor (and other I2C device) drivers. It probably should
* not be used directly.
*/
public class I2C {
public class I2C implements AutoCloseable {
public enum Port {
kOnboard(0), kMXP(1);
@@ -52,10 +52,16 @@ public class I2C {
HAL.report(tResourceType.kResourceType_I2C, deviceAddress);
}
@Deprecated
public void free() {
close();
}
/**
* Destructor.
*/
public void free() {
@Override
public void close() {
I2CJNI.i2CClose(m_port);
}

View File

@@ -48,8 +48,8 @@ public abstract class InterruptableSensorBase extends SensorBase {
* Frees the resources for this output.
*/
@Override
public void free() {
super.free();
public void close() {
super.close();
if (m_interrupt != 0) {
cancelInterrupts();
}

View File

@@ -53,10 +53,10 @@ public class NidecBrushless extends SendableBase implements SpeedController, Mot
* Free the resources used by this object.
*/
@Override
public void free() {
super.free();
m_dio.free();
m_pwm.free();
public void close() {
super.close();
m_dio.close();
m_pwm.close();
}
/**

View File

@@ -92,8 +92,8 @@ public class PIDController extends PIDBase implements Controller {
* Free the PID object.
*/
@Override
public void free() {
super.free();
public void close() {
super.close();
m_controlLoop.stop();
m_thisMutex.lock();
try {

View File

@@ -71,8 +71,8 @@ public class PWM extends SendableBase implements Sendable {
* <p>Free the resource associated with the PWM channel and set the value to 0.
*/
@Override
public void free() {
super.free();
public void close() {
super.close();
if (m_handle == 0) {
return;
}

View File

@@ -141,8 +141,8 @@ public class Relay extends SendableBase implements MotorSafety, Sendable {
}
@Override
public void free() {
super.free();
public void close() {
super.close();
freeRelay();
}

View File

@@ -32,7 +32,7 @@ import edu.wpi.first.wpilibj.util.WPILibVersion;
* run to completion before the OperatorControl code could start. In the future the Autonomous code
* might be spawned as a task, then killed at the end of the Autonomous period.
*/
public abstract class RobotBase {
public abstract class RobotBase implements AutoCloseable {
/**
* The ID of the main Java thread.
*/
@@ -93,10 +93,15 @@ public abstract class RobotBase {
LiveWindow.setEnabled(false);
}
@Deprecated
public void free() {
}
/**
* Free the resources for a RobotBase class.
*/
public void free() {
@Override
public void close() {
}
/**

View File

@@ -25,7 +25,7 @@ import static java.util.Objects.requireNonNull;
* or {@link edu.wpi.first.wpilibj.drive.MecanumDrive} classes instead.
*/
@Deprecated
public class RobotDrive implements MotorSafety {
public class RobotDrive implements MotorSafety, AutoCloseable {
protected MotorSafetyHelper m_safetyHelper;
/**
@@ -662,22 +662,28 @@ public class RobotDrive implements MotorSafety {
m_maxOutput = maxOutput;
}
@Deprecated
public void free() {
close();
}
/**
* Free the speed controllers if they were allocated locally.
*/
public void free() {
@Override
public void close() {
if (m_allocatedSpeedControllers) {
if (m_frontLeftMotor != null) {
((PWM) m_frontLeftMotor).free();
((PWM) m_frontLeftMotor).close();
}
if (m_frontRightMotor != null) {
((PWM) m_frontRightMotor).free();
((PWM) m_frontRightMotor).close();
}
if (m_rearLeftMotor != null) {
((PWM) m_rearLeftMotor).free();
((PWM) m_rearLeftMotor).close();
}
if (m_rearRightMotor != null) {
((PWM) m_rearRightMotor).free();
((PWM) m_rearRightMotor).close();
}
}
}

View File

@@ -16,7 +16,7 @@ import edu.wpi.first.wpilibj.hal.SPIJNI;
/**
* Represents a SPI bus port.
*/
public class SPI {
public class SPI implements AutoCloseable {
public enum Port {
kOnboardCS0(0), kOnboardCS1(1), kOnboardCS2(2), kOnboardCS3(3), kMXP(4);
@@ -49,12 +49,19 @@ public class SPI {
HAL.report(tResourceType.kResourceType_SPI, devices);
}
@Deprecated
public void free() {
close();
}
/**
* Free the resources used by this object.
*/
public void free() {
@Override
public void close() {
if (m_accum != null) {
m_accum.free();
m_accum.close();
m_accum = null;
}
SPIJNI.spiClose(m_port);
@@ -416,7 +423,7 @@ public class SPI {
private static final int kAccumulateDepth = 2048;
private static class Accumulator {
private static class Accumulator implements AutoCloseable {
Accumulator(int port, int xferSize, int validMask, int validValue, int dataShift,
int dataSize, boolean isSigned, boolean bigEndian) {
m_notifier = new Notifier(this::update);
@@ -432,7 +439,8 @@ public class SPI {
m_port = port;
}
void free() {
@Override
public void close() {
m_notifier.stop();
}
@@ -570,7 +578,7 @@ public class SPI {
*/
public void freeAccumulator() {
if (m_accum != null) {
m_accum.free();
m_accum.close();
m_accum = null;
}
freeAuto();

View File

@@ -13,7 +13,7 @@ import edu.wpi.first.wpilibj.livewindow.LiveWindow;
* Base class for all sensors. Stores most recent status information as well as containing utility
* functions for checking channels and error processing.
*/
public abstract class SendableBase implements Sendable {
public abstract class SendableBase implements Sendable, AutoCloseable {
private String m_name = "";
private String m_subsystem = "Ungrouped";
@@ -35,10 +35,16 @@ public abstract class SendableBase implements Sendable {
}
}
@Deprecated
public void free() {
close();
}
/**
* Free the resources used by this object.
*/
public void free() {
@Override
public void close() {
LiveWindow.remove(this);
}

View File

@@ -197,7 +197,7 @@ public class SerialPort {
/**
* Destructor.
*/
public void free() {
public void close() {
SerialPortJNI.serialClose(m_port);
}

View File

@@ -55,8 +55,8 @@ public class Solenoid extends SolenoidBase implements Sendable {
* Destructor.
*/
@Override
public synchronized void free() {
super.free();
public synchronized void close() {
super.close();
SolenoidJNI.freeSolenoidPort(m_solenoidHandle);
m_solenoidHandle = 0;
}

View File

@@ -191,21 +191,21 @@ public class Ultrasonic extends SensorBase implements PIDSource, Sendable {
* sensor).
*/
@Override
public synchronized void free() {
super.free();
public synchronized void close() {
super.close();
final boolean wasAutomaticMode = m_automaticEnabled;
setAutomaticMode(false);
if (m_allocatedChannels) {
if (m_pingChannel != null) {
m_pingChannel.free();
m_pingChannel.close();
}
if (m_echoChannel != null) {
m_echoChannel.free();
m_echoChannel.close();
}
}
if (m_counter != null) {
m_counter.free();
m_counter.close();
m_counter = null;
}

View File

@@ -10,7 +10,7 @@ package edu.wpi.first.wpilibj.interfaces;
/**
* Interface for yaw rate gyros.
*/
public interface Gyro {
public interface Gyro extends AutoCloseable {
/**
* Calibrate the gyro by running for a number of samples and computing the center value. Then use
* the center value as the Accumulator center value for subsequent measurements. It's important to
@@ -52,5 +52,12 @@ public interface Gyro {
/**
* Free the resources used by the gyro.
*/
@Deprecated
void free();
/**
* Free the resources used by the gyro.
*/
@Override
void close();
}