mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-29 02:21:44 +00:00
[wpiunits] Make Java units immutable only (#8115)
Remove mutable implementations, as systemcore doesn't need mutability to keep performance under control.
This commit is contained in:
@@ -5,8 +5,6 @@
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.Acceleration;
|
||||
import edu.wpi.first.units.measure.ImmutableAcceleration;
|
||||
import edu.wpi.first.units.measure.MutAcceleration;
|
||||
|
||||
/**
|
||||
* A generic unit of acceleration.
|
||||
@@ -45,12 +43,12 @@ public final class AccelerationUnit<D extends Unit> extends PerUnit<VelocityUnit
|
||||
|
||||
@Override
|
||||
public Acceleration<D> of(double magnitude) {
|
||||
return new ImmutableAcceleration<>(magnitude, toBaseUnits(magnitude), this);
|
||||
return new Acceleration<>(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Acceleration<D> ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableAcceleration<>(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new Acceleration<>(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -65,11 +63,6 @@ public final class AccelerationUnit<D extends Unit> extends PerUnit<VelocityUnit
|
||||
return (Acceleration<D>) (Acceleration) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutAcceleration<D> mutable(double initialMagnitude) {
|
||||
return new MutAcceleration<>(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VelocityUnit<AccelerationUnit<D>> per(TimeUnit time) {
|
||||
return VelocityUnit.combine(this, time);
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.Angle;
|
||||
import edu.wpi.first.units.measure.MutAngle;
|
||||
|
||||
/**
|
||||
* Unit of angular dimension.
|
||||
@@ -49,11 +48,6 @@ public final class AngleUnit extends Unit {
|
||||
return Angle.ofBaseUnits(baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutAngle mutable(double initialMagnitude) {
|
||||
return new MutAngle(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AngularVelocityUnit per(TimeUnit period) {
|
||||
return AngularVelocityUnit.combine(this, period);
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.AngularAcceleration;
|
||||
import edu.wpi.first.units.measure.ImmutableAngularAcceleration;
|
||||
import edu.wpi.first.units.measure.MutAngularAcceleration;
|
||||
|
||||
/** A unit of angular acceleration, such as {@link Units#RadiansPerSecondPerSecond}. */
|
||||
public final class AngularAccelerationUnit extends PerUnit<AngularVelocityUnit, TimeUnit> {
|
||||
@@ -49,13 +47,12 @@ public final class AngularAccelerationUnit extends PerUnit<AngularVelocityUnit,
|
||||
|
||||
@Override
|
||||
public AngularAcceleration of(double magnitude) {
|
||||
return new ImmutableAngularAcceleration(magnitude, toBaseUnits(magnitude), this);
|
||||
return new AngularAcceleration(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AngularAcceleration ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableAngularAcceleration(
|
||||
fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new AngularAcceleration(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -68,11 +65,6 @@ public final class AngularAccelerationUnit extends PerUnit<AngularVelocityUnit,
|
||||
return (AngularAcceleration) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutAngularAcceleration mutable(double initialMagnitude) {
|
||||
return new MutAngularAcceleration(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VelocityUnit<AngularAccelerationUnit> per(TimeUnit time) {
|
||||
return VelocityUnit.combine(this, time);
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.AngularMomentum;
|
||||
import edu.wpi.first.units.measure.ImmutableAngularMomentum;
|
||||
import edu.wpi.first.units.measure.MutAngularMomentum;
|
||||
|
||||
/**
|
||||
* A unit of angular momentum, modeled as linear momentum of an object rotating some distance away
|
||||
@@ -52,12 +50,12 @@ public final class AngularMomentumUnit extends MultUnit<LinearMomentumUnit, Dist
|
||||
|
||||
@Override
|
||||
public AngularMomentum of(double magnitude) {
|
||||
return new ImmutableAngularMomentum(magnitude, toBaseUnits(magnitude), this);
|
||||
return new AngularMomentum(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AngularMomentum ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableAngularMomentum(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new AngularMomentum(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -70,11 +68,6 @@ public final class AngularMomentumUnit extends MultUnit<LinearMomentumUnit, Dist
|
||||
return (AngularMomentum) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutAngularMomentum mutable(double magnitude) {
|
||||
return new MutAngularMomentum(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VelocityUnit<AngularMomentumUnit> per(TimeUnit time) {
|
||||
return VelocityUnit.combine(this, time);
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.AngularVelocity;
|
||||
import edu.wpi.first.units.measure.ImmutableAngularVelocity;
|
||||
import edu.wpi.first.units.measure.MutAngularVelocity;
|
||||
|
||||
/** A unit of angular velocity like {@link Units#RadiansPerSecond}. */
|
||||
public final class AngularVelocityUnit extends PerUnit<AngleUnit, TimeUnit> {
|
||||
@@ -49,12 +47,12 @@ public final class AngularVelocityUnit extends PerUnit<AngleUnit, TimeUnit> {
|
||||
|
||||
@Override
|
||||
public AngularVelocity of(double magnitude) {
|
||||
return new ImmutableAngularVelocity(magnitude, toBaseUnits(magnitude), this);
|
||||
return new AngularVelocity(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AngularVelocity ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableAngularVelocity(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new AngularVelocity(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -67,11 +65,6 @@ public final class AngularVelocityUnit extends PerUnit<AngleUnit, TimeUnit> {
|
||||
return (AngularVelocity) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutAngularVelocity mutable(double initialMagnitude) {
|
||||
return new MutAngularVelocity(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AngularAccelerationUnit per(TimeUnit period) {
|
||||
return AngularAccelerationUnit.combine(this, period);
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.Current;
|
||||
import edu.wpi.first.units.measure.ImmutableCurrent;
|
||||
import edu.wpi.first.units.measure.MutCurrent;
|
||||
|
||||
/**
|
||||
* Unit of electric current dimension.
|
||||
@@ -53,12 +51,12 @@ public final class CurrentUnit extends Unit {
|
||||
|
||||
@Override
|
||||
public Current of(double magnitude) {
|
||||
return new ImmutableCurrent(magnitude, toBaseUnits(magnitude), this);
|
||||
return new Current(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Current ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableCurrent(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new Current(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -71,11 +69,6 @@ public final class CurrentUnit extends Unit {
|
||||
return (Current) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutCurrent mutable(double initialMagnitude) {
|
||||
return new MutCurrent(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VelocityUnit<CurrentUnit> per(TimeUnit time) {
|
||||
return VelocityUnit.combine(this, time);
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.Dimensionless;
|
||||
import edu.wpi.first.units.measure.ImmutableDimensionless;
|
||||
import edu.wpi.first.units.measure.MutDimensionless;
|
||||
|
||||
/**
|
||||
* A type of unit that corresponds to raw values and not any physical dimension, such as percentage.
|
||||
@@ -40,12 +38,12 @@ public final class DimensionlessUnit extends Unit {
|
||||
|
||||
@Override
|
||||
public Dimensionless of(double magnitude) {
|
||||
return new ImmutableDimensionless(magnitude, toBaseUnits(magnitude), this);
|
||||
return new Dimensionless(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dimensionless ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableDimensionless(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new Dimensionless(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -58,11 +56,6 @@ public final class DimensionlessUnit extends Unit {
|
||||
return (Dimensionless) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutDimensionless mutable(double initialMagnitude) {
|
||||
return new MutDimensionless(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a measurement value in terms of another dimensionless unit to this unit.
|
||||
*
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.Distance;
|
||||
import edu.wpi.first.units.measure.ImmutableDistance;
|
||||
import edu.wpi.first.units.measure.MutDistance;
|
||||
|
||||
/**
|
||||
* Unit of linear dimension.
|
||||
@@ -65,12 +63,12 @@ public final class DistanceUnit extends Unit {
|
||||
|
||||
@Override
|
||||
public Distance of(double magnitude) {
|
||||
return new ImmutableDistance(magnitude, toBaseUnits(magnitude), this);
|
||||
return new Distance(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Distance ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableDistance(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new Distance(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -83,11 +81,6 @@ public final class DistanceUnit extends Unit {
|
||||
return (Distance) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutDistance mutable(double initialMagnitude) {
|
||||
return new MutDistance(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplies this distance unit by a unit of force to create a unit of torque.
|
||||
*
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.Energy;
|
||||
import edu.wpi.first.units.measure.ImmutableEnergy;
|
||||
import edu.wpi.first.units.measure.MutEnergy;
|
||||
|
||||
/**
|
||||
* Unit of energy dimension.
|
||||
@@ -71,12 +69,12 @@ public final class EnergyUnit extends Unit {
|
||||
|
||||
@Override
|
||||
public Energy of(double magnitude) {
|
||||
return new ImmutableEnergy(magnitude, toBaseUnits(magnitude), this);
|
||||
return new Energy(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Energy ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableEnergy(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new Energy(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -88,9 +86,4 @@ public final class EnergyUnit extends Unit {
|
||||
public Energy one() {
|
||||
return (Energy) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutEnergy mutable(double initialMagnitude) {
|
||||
return new MutEnergy(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.Force;
|
||||
import edu.wpi.first.units.measure.ImmutableForce;
|
||||
import edu.wpi.first.units.measure.MutForce;
|
||||
|
||||
/** A unit of force like {@link Units#Newtons}. */
|
||||
public final class ForceUnit extends MultUnit<MassUnit, LinearAccelerationUnit> {
|
||||
@@ -61,12 +59,12 @@ public final class ForceUnit extends MultUnit<MassUnit, LinearAccelerationUnit>
|
||||
|
||||
@Override
|
||||
public Force of(double magnitude) {
|
||||
return new ImmutableForce(magnitude, toBaseUnits(magnitude), this);
|
||||
return new Force(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Force ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableForce(toBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new Force(toBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -79,11 +77,6 @@ public final class ForceUnit extends MultUnit<MassUnit, LinearAccelerationUnit>
|
||||
return (Force) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutForce mutable(double initialMagnitude) {
|
||||
return new MutForce(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VelocityUnit<ForceUnit> per(TimeUnit time) {
|
||||
return VelocityUnit.combine(this, time);
|
||||
|
||||
@@ -7,8 +7,6 @@ package edu.wpi.first.units;
|
||||
import static edu.wpi.first.units.Units.Value;
|
||||
|
||||
import edu.wpi.first.units.measure.Frequency;
|
||||
import edu.wpi.first.units.measure.ImmutableFrequency;
|
||||
import edu.wpi.first.units.measure.MutFrequency;
|
||||
|
||||
/** A unit of frequency like {@link edu.wpi.first.units.Units#Hertz}. */
|
||||
public final class FrequencyUnit extends PerUnit<DimensionlessUnit, TimeUnit> {
|
||||
@@ -62,12 +60,12 @@ public final class FrequencyUnit extends PerUnit<DimensionlessUnit, TimeUnit> {
|
||||
|
||||
@Override
|
||||
public Frequency of(double magnitude) {
|
||||
return new ImmutableFrequency(magnitude, toBaseUnits(magnitude), this);
|
||||
return new Frequency(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Frequency ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableFrequency(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new Frequency(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -80,11 +78,6 @@ public final class FrequencyUnit extends PerUnit<DimensionlessUnit, TimeUnit> {
|
||||
return (Frequency) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutFrequency mutable(double initialMagnitude) {
|
||||
return new MutFrequency(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VelocityUnit<FrequencyUnit> per(TimeUnit time) {
|
||||
return VelocityUnit.combine(this, time);
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.Dimensionless;
|
||||
import edu.wpi.first.units.mutable.GenericMutableMeasureImpl;
|
||||
|
||||
/**
|
||||
* A measure holds the magnitude and unit of some dimension, such as distance, time, or speed. An
|
||||
@@ -45,16 +44,6 @@ public record ImmutableMeasure<U extends Unit>(double magnitude, double baseUnit
|
||||
return new ImmutableMeasure<>(relativeMagnitude, unit.toBaseUnits(relativeMagnitude), unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Measure<U> copy() {
|
||||
return this; // already immutable, no need to allocate a new object
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutableMeasure<U, ?, ?> mutableCopy() {
|
||||
return new GenericMutableMeasureImpl<>(magnitude, baseUnitMagnitude, unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Measure<U> unaryMinus() {
|
||||
return ofBaseUnits(0 - baseUnitMagnitude, unit);
|
||||
|
||||
@@ -4,9 +4,7 @@
|
||||
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.ImmutableLinearAcceleration;
|
||||
import edu.wpi.first.units.measure.LinearAcceleration;
|
||||
import edu.wpi.first.units.measure.MutLinearAcceleration;
|
||||
|
||||
/**
|
||||
* A unit of linear acceleration like {@link edu.wpi.first.units.Units#MetersPerSecondPerSecond}.
|
||||
@@ -51,13 +49,12 @@ public final class LinearAccelerationUnit extends PerUnit<LinearVelocityUnit, Ti
|
||||
|
||||
@Override
|
||||
public LinearAcceleration of(double magnitude) {
|
||||
return new ImmutableLinearAcceleration(magnitude, toBaseUnits(magnitude), this);
|
||||
return new LinearAcceleration(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LinearAcceleration ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableLinearAcceleration(
|
||||
fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new LinearAcceleration(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -70,11 +67,6 @@ public final class LinearAccelerationUnit extends PerUnit<LinearVelocityUnit, Ti
|
||||
return (LinearAcceleration) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutLinearAcceleration mutable(double initialMagnitude) {
|
||||
return new MutLinearAcceleration(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VelocityUnit<LinearAccelerationUnit> per(TimeUnit time) {
|
||||
return VelocityUnit.combine(this, time);
|
||||
|
||||
@@ -4,9 +4,7 @@
|
||||
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.ImmutableLinearMomentum;
|
||||
import edu.wpi.first.units.measure.LinearMomentum;
|
||||
import edu.wpi.first.units.measure.MutLinearMomentum;
|
||||
|
||||
/** A unit of linear momentum like {@link edu.wpi.first.units.Units#KilogramMetersPerSecond}. */
|
||||
public final class LinearMomentumUnit extends MultUnit<MassUnit, LinearVelocityUnit> {
|
||||
@@ -49,12 +47,12 @@ public final class LinearMomentumUnit extends MultUnit<MassUnit, LinearVelocityU
|
||||
|
||||
@Override
|
||||
public LinearMomentum of(double magnitude) {
|
||||
return new ImmutableLinearMomentum(magnitude, toBaseUnits(magnitude), this);
|
||||
return new LinearMomentum(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LinearMomentum ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableLinearMomentum(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new LinearMomentum(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -67,11 +65,6 @@ public final class LinearMomentumUnit extends MultUnit<MassUnit, LinearVelocityU
|
||||
return (LinearMomentum) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutLinearMomentum mutable(double initialMagnitude) {
|
||||
return new MutLinearMomentum(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VelocityUnit<LinearMomentumUnit> per(TimeUnit time) {
|
||||
return VelocityUnit.combine(this, time);
|
||||
|
||||
@@ -4,9 +4,7 @@
|
||||
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.ImmutableLinearVelocity;
|
||||
import edu.wpi.first.units.measure.LinearVelocity;
|
||||
import edu.wpi.first.units.measure.MutLinearVelocity;
|
||||
|
||||
/** A unit of linear velocity like {@link edu.wpi.first.units.Units#MetersPerSecond}. */
|
||||
public final class LinearVelocityUnit extends PerUnit<DistanceUnit, TimeUnit> {
|
||||
@@ -49,12 +47,12 @@ public final class LinearVelocityUnit extends PerUnit<DistanceUnit, TimeUnit> {
|
||||
|
||||
@Override
|
||||
public LinearVelocity of(double magnitude) {
|
||||
return new ImmutableLinearVelocity(magnitude, toBaseUnits(magnitude), this);
|
||||
return new LinearVelocity(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LinearVelocity ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableLinearVelocity(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new LinearVelocity(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -67,11 +65,6 @@ public final class LinearVelocityUnit extends PerUnit<DistanceUnit, TimeUnit> {
|
||||
return (LinearVelocity) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutLinearVelocity mutable(double value) {
|
||||
return new MutLinearVelocity(value, toBaseUnits(value), this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Combines this velocity with a time period of change to form a unit of acceleration.
|
||||
*
|
||||
|
||||
@@ -4,9 +4,7 @@
|
||||
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.ImmutableMass;
|
||||
import edu.wpi.first.units.measure.Mass;
|
||||
import edu.wpi.first.units.measure.MutMass;
|
||||
|
||||
/**
|
||||
* Unit of mass dimension.
|
||||
@@ -39,12 +37,12 @@ public final class MassUnit extends Unit {
|
||||
|
||||
@Override
|
||||
public Mass of(double magnitude) {
|
||||
return new ImmutableMass(magnitude, toBaseUnits(magnitude), this);
|
||||
return new Mass(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mass ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableMass(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new Mass(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -57,11 +55,6 @@ public final class MassUnit extends Unit {
|
||||
return (Mass) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutMass mutable(double initialMagnitude) {
|
||||
return new MutMass(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VelocityUnit<MassUnit> per(TimeUnit period) {
|
||||
return VelocityUnit.combine(this, period);
|
||||
|
||||
@@ -119,22 +119,6 @@ public interface Measure<U extends Unit> extends Comparable<Measure<U>> {
|
||||
return Math.copySign(this.in(unit), other.in(unit));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an immutable copy of this measure. The copy can be used freely and is guaranteed never
|
||||
* to change.
|
||||
*
|
||||
* @return the copied measure
|
||||
*/
|
||||
Measure<U> copy();
|
||||
|
||||
/**
|
||||
* Returns a mutable copy of this measure. It will be initialized to the current state of this
|
||||
* measure, but can be changed over time without needing to allocate new measurement objects.
|
||||
*
|
||||
* @return the copied measure
|
||||
*/
|
||||
MutableMeasure<U, ?, ?> mutableCopy();
|
||||
|
||||
/**
|
||||
* Returns a measure equivalent to this one equal to zero minus its current value. For non-linear
|
||||
* unit types like temperature, the zero point is treated as the zero value of the base unit (eg
|
||||
|
||||
@@ -4,9 +4,7 @@
|
||||
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.ImmutableMomentOfInertia;
|
||||
import edu.wpi.first.units.measure.MomentOfInertia;
|
||||
import edu.wpi.first.units.measure.MutMomentOfInertia;
|
||||
|
||||
/**
|
||||
* A unit of moment of inertia, like {@link edu.wpi.first.units.Units#KilogramSquareMeters}. Moments
|
||||
@@ -55,12 +53,12 @@ public final class MomentOfInertiaUnit extends PerUnit<AngularMomentumUnit, Angu
|
||||
|
||||
@Override
|
||||
public MomentOfInertia of(double magnitude) {
|
||||
return new ImmutableMomentOfInertia(magnitude, toBaseUnits(magnitude), this);
|
||||
return new MomentOfInertia(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MomentOfInertia ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableMomentOfInertia(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new MomentOfInertia(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -73,11 +71,6 @@ public final class MomentOfInertiaUnit extends PerUnit<AngularMomentumUnit, Angu
|
||||
return (MomentOfInertia) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutMomentOfInertia mutable(double initialMagnitude) {
|
||||
return new MutMomentOfInertia(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VelocityUnit<MomentOfInertiaUnit> per(TimeUnit time) {
|
||||
return VelocityUnit.combine(this, time);
|
||||
|
||||
@@ -4,9 +4,7 @@
|
||||
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.ImmutableMult;
|
||||
import edu.wpi.first.units.measure.Mult;
|
||||
import edu.wpi.first.units.measure.MutMult;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
@@ -130,7 +128,7 @@ public class MultUnit<A extends Unit, B extends Unit> extends Unit {
|
||||
* @see #of(double)
|
||||
*/
|
||||
public final Mult<A, B> ofNative(double magnitude) {
|
||||
return new ImmutableMult<>(magnitude, toBaseUnits(magnitude), this);
|
||||
return new Mult<>(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -142,7 +140,7 @@ public class MultUnit<A extends Unit, B extends Unit> extends Unit {
|
||||
* @see #ofBaseUnits(double)
|
||||
*/
|
||||
public final Mult<A, B> ofNativeBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableMult<>(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new Mult<>(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -157,11 +155,6 @@ public class MultUnit<A extends Unit, B extends Unit> extends Unit {
|
||||
return (Measure<? extends MultUnit<A, B>>) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutableMeasure<? extends MultUnit<A, B>, ?, ?> mutable(double initialMagnitude) {
|
||||
return new MutMult<>(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Unit per(TimeUnit time) {
|
||||
return VelocityUnit.combine(this, time);
|
||||
|
||||
@@ -1,180 +0,0 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.Dimensionless;
|
||||
|
||||
/**
|
||||
* A mutable measurement can be used to keep a single object allocation and reference whose state is
|
||||
* mutated or changed as it is used. This is helpful for optimizing memory usage to keep garbage
|
||||
* collection time - and its associated loop overruns - to a minimum.
|
||||
*
|
||||
* @param <U> The dimension of measurement.
|
||||
* @param <Base> The base measure type.
|
||||
* @param <MutSelf> The self type. This MUST inherit from the base measure type.
|
||||
*/
|
||||
public interface MutableMeasure<
|
||||
U extends Unit, Base extends Measure<U>, MutSelf extends MutableMeasure<U, Base, MutSelf>>
|
||||
extends Measure<U> {
|
||||
/**
|
||||
* Overwrites the state of this measure with new values.
|
||||
*
|
||||
* @param magnitude the new magnitude in terms of the new unit
|
||||
* @param newUnit the new unit
|
||||
* @return this measure
|
||||
*/
|
||||
MutSelf mut_replace(double magnitude, U newUnit);
|
||||
|
||||
/**
|
||||
* Overwrites the state of this measure and replaces it completely with values from the given one.
|
||||
* The magnitude, base unit magnitude, and unit will all be copied. This is functionally the same
|
||||
* as calling {@code other.mutableCopy()}, but copying to a pre-existing mutable measure instead
|
||||
* of instantiating a new one.
|
||||
*
|
||||
* @param other the other measure to copy values from
|
||||
* @return this measure
|
||||
*/
|
||||
default MutSelf mut_replace(Base other) {
|
||||
return mut_replace(other.magnitude(), other.unit());
|
||||
}
|
||||
|
||||
@Override
|
||||
Base copy();
|
||||
|
||||
/**
|
||||
* Sets the new magnitude of the measurement. The magnitude must be in terms of the {@link
|
||||
* #unit()}.
|
||||
*
|
||||
* @param magnitude the new magnitude of the measurement
|
||||
* @return this mutable measure
|
||||
*/
|
||||
default MutSelf mut_setMagnitude(double magnitude) {
|
||||
return mut_replace(magnitude, unit());
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the new magnitude of the measurement. The magnitude must be in terms of the base unit of
|
||||
* the current unit.
|
||||
*
|
||||
* @param baseUnitMagnitude the new magnitude of the measurement
|
||||
* @return this mutable measure
|
||||
*/
|
||||
default MutSelf mut_setBaseUnitMagnitude(double baseUnitMagnitude) {
|
||||
return mut_replace(unit().fromBaseUnits(baseUnitMagnitude), unit());
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the current magnitude of the measure by the given value. The value must be in terms
|
||||
* of the current {@link #unit() unit}.
|
||||
*
|
||||
* @param raw the raw value to accumulate by
|
||||
* @return the measure
|
||||
*/
|
||||
default MutSelf mut_acc(double raw) {
|
||||
return mut_setBaseUnitMagnitude(magnitude() + raw);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the current magnitude of the measure by the amount of the given measure.
|
||||
*
|
||||
* @param other the measure whose value should be added to this one
|
||||
* @return this measure
|
||||
*/
|
||||
default MutSelf mut_acc(Base other) {
|
||||
return mut_setMagnitude(magnitude() + unit().fromBaseUnits(other.baseUnitMagnitude()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds another measurement to this one. This will mutate the object instead of generating a new
|
||||
* measurement object.
|
||||
*
|
||||
* @param other the measurement to add
|
||||
* @return this measure
|
||||
*/
|
||||
default MutSelf mut_plus(Base other) {
|
||||
return mut_acc(other);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds another measurement to this one. This will mutate the object instead of generating a new
|
||||
* measurement object. This is a denormalized version of {@link #mut_plus(Measure)} to avoid
|
||||
* having to wrap raw numbers in a {@code Measure} object and pay for an object allocation.
|
||||
*
|
||||
* @param magnitude the magnitude of the other measurement.
|
||||
* @param otherUnit the unit of the other measurement
|
||||
* @return this measure
|
||||
*/
|
||||
default MutSelf mut_plus(double magnitude, U otherUnit) {
|
||||
return mut_setBaseUnitMagnitude(magnitude() + otherUnit.toBaseUnits(magnitude));
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtracts another measurement to this one. This will mutate the object instead of generating a
|
||||
* new measurement object.
|
||||
*
|
||||
* @param other the measurement to subtract from this one
|
||||
* @return this measure
|
||||
*/
|
||||
default MutSelf mut_minus(Base other) {
|
||||
return mut_setBaseUnitMagnitude(baseUnitMagnitude() - other.baseUnitMagnitude());
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtracts another measurement from this one. This will mutate the object instead of generating
|
||||
* a new measurement object. This is a denormalized version of {@link #mut_minus(Measure)} to
|
||||
* avoid having to wrap raw numbers in a {@code Measure} object and pay for an object allocation.
|
||||
*
|
||||
* @param magnitude the magnitude of the other measurement.
|
||||
* @param otherUnit the unit of the other measurement
|
||||
* @return this measure
|
||||
*/
|
||||
default MutSelf mut_minus(double magnitude, U otherUnit) {
|
||||
return mut_setBaseUnitMagnitude(baseUnitMagnitude() - otherUnit.toBaseUnits(magnitude));
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplies this measurement by some constant value. This will mutate the object instead of
|
||||
* generating a new measurement object.
|
||||
*
|
||||
* @param multiplier the multiplier to scale the measurement by
|
||||
* @return this measure
|
||||
*/
|
||||
default MutSelf mut_times(double multiplier) {
|
||||
return mut_setBaseUnitMagnitude(baseUnitMagnitude() * multiplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplies this measurement by some constant value. This will mutate the object instead of
|
||||
* generating a new measurement object.
|
||||
*
|
||||
* @param multiplier the multiplier to scale the measurement by
|
||||
* @return this measure
|
||||
*/
|
||||
default MutSelf mut_times(Dimensionless multiplier) {
|
||||
return mut_times(multiplier.baseUnitMagnitude());
|
||||
}
|
||||
|
||||
/**
|
||||
* Divides this measurement by some constant value. This will mutate the object instead of
|
||||
* generating a new measurement object.
|
||||
*
|
||||
* @param divisor the divisor to scale the measurement by
|
||||
* @return this measure
|
||||
*/
|
||||
default MutSelf mut_divide(double divisor) {
|
||||
return mut_times(1 / divisor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Divides this measurement by some constant value. This will mutate the object instead of
|
||||
* generating a new measurement object.
|
||||
*
|
||||
* @param divisor the divisor to scale the measurement by
|
||||
* @return this measure
|
||||
*/
|
||||
default MutSelf mut_divide(Dimensionless divisor) {
|
||||
return mut_divide(divisor.baseUnitMagnitude());
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,6 @@
|
||||
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.ImmutablePer;
|
||||
import edu.wpi.first.units.measure.MutPer;
|
||||
import edu.wpi.first.units.measure.Per;
|
||||
import java.util.Objects;
|
||||
|
||||
@@ -195,7 +193,7 @@ public class PerUnit<N extends Unit, D extends Unit> extends Unit {
|
||||
* @see #of(double)
|
||||
*/
|
||||
public final Per<N, D> ofNative(double magnitude) {
|
||||
return new ImmutablePer<>(magnitude, toBaseUnits(magnitude), this);
|
||||
return new Per<>(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -207,7 +205,7 @@ public class PerUnit<N extends Unit, D extends Unit> extends Unit {
|
||||
* @see #ofBaseUnits(double)
|
||||
*/
|
||||
public final Per<N, D> ofNativeBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutablePer<>(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new Per<>(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -222,32 +220,6 @@ public class PerUnit<N extends Unit, D extends Unit> extends Unit {
|
||||
return (Measure<? extends PerUnit<N, D>>) super.one();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>Note: When called on an object of type {@code PerUnit} (and <i>not</i> a subclass!), this
|
||||
* method will always return a {@link edu.wpi.first.units.measure.MutPer} instance.
|
||||
*
|
||||
* @param initialMagnitude the starting magnitude of the measure
|
||||
* @return the ratio measure
|
||||
*/
|
||||
@Override
|
||||
public MutableMeasure<? extends PerUnit<N, D>, ?, ?> mutable(double initialMagnitude) {
|
||||
return mutableNative(initialMagnitude);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new mutable measurement of the given magnitude in terms of the ratio unit. This will
|
||||
* always return a {@code Per} object and cannot be overridden by subclasses.
|
||||
*
|
||||
* @param initialMagnitude the starting magnitude of the measure
|
||||
* @return the ratio measure
|
||||
* @see #mutable(double)
|
||||
*/
|
||||
public final MutPer<N, D> mutableNative(double initialMagnitude) {
|
||||
return new MutPer<>(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Unit per(TimeUnit time) {
|
||||
return VelocityUnit.combine(this, time);
|
||||
|
||||
@@ -7,8 +7,6 @@ package edu.wpi.first.units;
|
||||
import static edu.wpi.first.units.Units.Joules;
|
||||
import static edu.wpi.first.units.Units.Seconds;
|
||||
|
||||
import edu.wpi.first.units.measure.ImmutablePower;
|
||||
import edu.wpi.first.units.measure.MutPower;
|
||||
import edu.wpi.first.units.measure.Power;
|
||||
|
||||
/**
|
||||
@@ -112,12 +110,12 @@ public final class PowerUnit extends PerUnit<EnergyUnit, TimeUnit> {
|
||||
|
||||
@Override
|
||||
public Power of(double magnitude) {
|
||||
return new ImmutablePower(magnitude, toBaseUnits(magnitude), this);
|
||||
return new Power(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Power ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutablePower(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new Power(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -130,11 +128,6 @@ public final class PowerUnit extends PerUnit<EnergyUnit, TimeUnit> {
|
||||
return (Power) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutPower mutable(double initialMagnitude) {
|
||||
return new MutPower(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VelocityUnit<PowerUnit> per(TimeUnit time) {
|
||||
return VelocityUnit.combine(this, time);
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.ImmutableResistance;
|
||||
import edu.wpi.first.units.measure.MutResistance;
|
||||
import edu.wpi.first.units.measure.Resistance;
|
||||
|
||||
/**
|
||||
@@ -57,12 +55,12 @@ public final class ResistanceUnit extends PerUnit<VoltageUnit, CurrentUnit> {
|
||||
|
||||
@Override
|
||||
public Resistance of(double magnitude) {
|
||||
return new ImmutableResistance(magnitude, toBaseUnits(magnitude), this);
|
||||
return new Resistance(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Resistance ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableResistance(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new Resistance(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -75,11 +73,6 @@ public final class ResistanceUnit extends PerUnit<VoltageUnit, CurrentUnit> {
|
||||
return (Resistance) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutResistance mutable(double initialMagnitude) {
|
||||
return new MutResistance(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a ratio unit between this unit and an arbitrary other unit.
|
||||
*
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.ImmutableTemperature;
|
||||
import edu.wpi.first.units.measure.MutTemperature;
|
||||
import edu.wpi.first.units.measure.Temperature;
|
||||
|
||||
/**
|
||||
@@ -34,12 +32,12 @@ public final class TemperatureUnit extends Unit {
|
||||
|
||||
@Override
|
||||
public Temperature of(double magnitude) {
|
||||
return new ImmutableTemperature(magnitude, toBaseUnits(magnitude), this);
|
||||
return new Temperature(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Temperature ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableTemperature(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new Temperature(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -52,11 +50,6 @@ public final class TemperatureUnit extends Unit {
|
||||
return (Temperature) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutTemperature mutable(double initialMagnitude) {
|
||||
return new MutTemperature(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VelocityUnit<TemperatureUnit> per(TimeUnit period) {
|
||||
return VelocityUnit.combine(this, period);
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.ImmutableTime;
|
||||
import edu.wpi.first.units.measure.MutTime;
|
||||
import edu.wpi.first.units.measure.Time;
|
||||
|
||||
/**
|
||||
@@ -77,12 +75,12 @@ public final class TimeUnit extends Unit {
|
||||
|
||||
@Override
|
||||
public Time of(double magnitude) {
|
||||
return new ImmutableTime(magnitude, toBaseUnits(magnitude), this);
|
||||
return new Time(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Time ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableTime(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new Time(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -94,9 +92,4 @@ public final class TimeUnit extends Unit {
|
||||
public Time one() {
|
||||
return (Time) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutTime mutable(double initialMagnitude) {
|
||||
return new MutTime(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.ImmutableTorque;
|
||||
import edu.wpi.first.units.measure.MutTorque;
|
||||
import edu.wpi.first.units.measure.Torque;
|
||||
|
||||
/** A unit of torque like {@link edu.wpi.first.units.Units#NewtonMeters}. */
|
||||
@@ -49,12 +47,12 @@ public final class TorqueUnit extends MultUnit<DistanceUnit, ForceUnit> {
|
||||
|
||||
@Override
|
||||
public Torque of(double magnitude) {
|
||||
return new ImmutableTorque(magnitude, toBaseUnits(magnitude), this);
|
||||
return new Torque(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Torque ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableTorque(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new Torque(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -67,11 +65,6 @@ public final class TorqueUnit extends MultUnit<DistanceUnit, ForceUnit> {
|
||||
return (Torque) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutTorque mutable(double initialMagnitude) {
|
||||
return new MutTorque(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VelocityUnit<TorqueUnit> per(TimeUnit time) {
|
||||
return VelocityUnit.combine(this, time);
|
||||
|
||||
@@ -85,16 +85,6 @@ public abstract class Unit {
|
||||
*/
|
||||
public abstract Measure<?> ofBaseUnits(double baseUnitMagnitude);
|
||||
|
||||
/**
|
||||
* Creates a new mutable measurement that is initialized to the given magnitude in terms of this
|
||||
* unit. Implementations are <strong>strongly</strong> recommended to sharpen the return type to a
|
||||
* unit-specific measurement implementation.
|
||||
*
|
||||
* @param initialMagnitude the initial magnitude of the mutable measurement
|
||||
* @return the mutable measurement object
|
||||
*/
|
||||
public abstract MutableMeasure<?, ?, ?> mutable(double initialMagnitude);
|
||||
|
||||
/**
|
||||
* Gets a measure of zero magnitude in terms of this unit. The returned object is guaranteed to be
|
||||
* of the same type returned by {@link #of(double)}. Subclasses are encouraged to override this
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.ImmutableVelocity;
|
||||
import edu.wpi.first.units.measure.MutVelocity;
|
||||
import edu.wpi.first.units.measure.Velocity;
|
||||
|
||||
/**
|
||||
@@ -80,12 +78,12 @@ public final class VelocityUnit<D extends Unit> extends PerUnit<D, TimeUnit> {
|
||||
|
||||
@Override
|
||||
public Velocity<D> of(double magnitude) {
|
||||
return new ImmutableVelocity<>(magnitude, toBaseUnits(magnitude), this);
|
||||
return new Velocity<>(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Velocity<D> ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableVelocity<>(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new Velocity<>(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -100,11 +98,6 @@ public final class VelocityUnit<D extends Unit> extends PerUnit<D, TimeUnit> {
|
||||
return (Velocity<D>) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutableMeasure<VelocityUnit<D>, ?, ?> mutable(double initialMagnitude) {
|
||||
return new MutVelocity<>(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Combines this velocity unit with a unit of a period of change to create an acceleration unit.
|
||||
*
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
|
||||
package edu.wpi.first.units;
|
||||
|
||||
import edu.wpi.first.units.measure.ImmutableVoltage;
|
||||
import edu.wpi.first.units.measure.MutVoltage;
|
||||
import edu.wpi.first.units.measure.Voltage;
|
||||
|
||||
/**
|
||||
@@ -53,12 +51,12 @@ public final class VoltageUnit extends Unit {
|
||||
|
||||
@Override
|
||||
public Voltage of(double magnitude) {
|
||||
return new ImmutableVoltage(magnitude, toBaseUnits(magnitude), this);
|
||||
return new Voltage(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Voltage ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableVoltage(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
return new Voltage(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -71,11 +69,6 @@ public final class VoltageUnit extends Unit {
|
||||
return (Voltage) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutVoltage mutable(double magnitude) {
|
||||
return new MutVoltage(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VelocityUnit<VoltageUnit> per(TimeUnit period) {
|
||||
return VelocityUnit.combine(this, period);
|
||||
|
||||
@@ -1,79 +0,0 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
package edu.wpi.first.units.mutable;
|
||||
|
||||
import edu.wpi.first.units.ImmutableMeasure;
|
||||
import edu.wpi.first.units.Measure;
|
||||
import edu.wpi.first.units.MutableMeasure;
|
||||
import edu.wpi.first.units.Unit;
|
||||
import edu.wpi.first.units.measure.Dimensionless;
|
||||
|
||||
/**
|
||||
* A generic implementation of a mutable measure. This is used in scenarios no unit-specific mutable
|
||||
* implementation can be determined.
|
||||
*
|
||||
* @param <U> the unit of measure
|
||||
*/
|
||||
public final class GenericMutableMeasureImpl<U extends Unit>
|
||||
extends MutableMeasureBase<U, Measure<U>, GenericMutableMeasureImpl<U>> {
|
||||
/**
|
||||
* Initializes the mutable measure with initial conditions. Both relative and base unit magnitudes
|
||||
* are required to avoid unnecessary calculations. It is up to the caller to ensure they are
|
||||
* correct.
|
||||
*
|
||||
* @param initialValue the initial magnitude of the measure, in terms of the unit
|
||||
* @param baseUnitMagnitude the initial magnitude of the measure, in terms of the base unit
|
||||
* @param unit the initial unit of measure
|
||||
*/
|
||||
public GenericMutableMeasureImpl(double initialValue, double baseUnitMagnitude, U unit) {
|
||||
super(initialValue, baseUnitMagnitude, unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Measure<U> copy() {
|
||||
return ImmutableMeasure.ofBaseUnits(m_baseUnitMagnitude, m_unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutableMeasure<U, ?, ?> mutableCopy() {
|
||||
return new GenericMutableMeasureImpl<>(m_magnitude, m_baseUnitMagnitude, m_unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Measure<U> unaryMinus() {
|
||||
return ImmutableMeasure.ofBaseUnits(0 - m_baseUnitMagnitude, m_unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Measure<U> plus(Measure<? extends U> other) {
|
||||
return ImmutableMeasure.ofBaseUnits(m_baseUnitMagnitude + other.baseUnitMagnitude(), m_unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Measure<U> minus(Measure<? extends U> other) {
|
||||
return ImmutableMeasure.ofBaseUnits(m_baseUnitMagnitude - other.baseUnitMagnitude(), m_unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Measure<U> times(double multiplier) {
|
||||
return ImmutableMeasure.ofBaseUnits(m_baseUnitMagnitude * multiplier, m_unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Measure<U> times(Dimensionless multiplier) {
|
||||
return ImmutableMeasure.ofBaseUnits(
|
||||
m_baseUnitMagnitude * multiplier.baseUnitMagnitude(), m_unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Measure<U> div(double divisor) {
|
||||
return ImmutableMeasure.ofBaseUnits(m_baseUnitMagnitude / divisor, m_unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Measure<U> div(Dimensionless divisor) {
|
||||
return ImmutableMeasure.ofBaseUnits(m_baseUnitMagnitude / divisor.baseUnitMagnitude(), m_unit);
|
||||
}
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
package edu.wpi.first.units.mutable;
|
||||
|
||||
import edu.wpi.first.units.Measure;
|
||||
import edu.wpi.first.units.MutableMeasure;
|
||||
import edu.wpi.first.units.Unit;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* A partial implementation of {@link MutableMeasure} to handle most of the state and Java object-
|
||||
* related implementations.
|
||||
*
|
||||
* @param <U> The dimension of measurement.
|
||||
* @param <Base> The base measure type.
|
||||
* @param <MutSelf> The self type. This MUST inherit from the base measure type.
|
||||
*/
|
||||
public abstract class MutableMeasureBase<
|
||||
U extends Unit, Base extends Measure<U>, MutSelf extends MutableMeasure<U, Base, MutSelf>>
|
||||
implements Measure<U>, MutableMeasure<U, Base, MutSelf> {
|
||||
/** The magnitude of the measurement, in terms of {@link #m_unit}. */
|
||||
protected double m_magnitude;
|
||||
|
||||
/** The magnitude of the measurement, in terms of {@link #m_unit}'s base unit. */
|
||||
protected double m_baseUnitMagnitude;
|
||||
|
||||
/** The unit of measurement. */
|
||||
protected U m_unit;
|
||||
|
||||
/**
|
||||
* Initializes the mutable measure with initial conditions. Both relative and base unit magnitudes
|
||||
* are required to avoid unnecessary calculations. It is up to the caller to ensure they are
|
||||
* correct.
|
||||
*
|
||||
* @param magnitude the initial magnitude of the measure, in terms of the unit
|
||||
* @param baseUnitMagnitude the initial magnitude of the measure, in terms of the base unit
|
||||
* @param unit the initial unit of measure
|
||||
*/
|
||||
protected MutableMeasureBase(double magnitude, double baseUnitMagnitude, U unit) {
|
||||
this.m_magnitude = magnitude;
|
||||
this.m_baseUnitMagnitude = baseUnitMagnitude;
|
||||
this.m_unit = Objects.requireNonNull(unit, "Unit cannot be null");
|
||||
}
|
||||
|
||||
@Override
|
||||
public double magnitude() {
|
||||
return m_magnitude;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double baseUnitMagnitude() {
|
||||
return m_baseUnitMagnitude;
|
||||
}
|
||||
|
||||
@Override
|
||||
public U unit() {
|
||||
return m_unit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toShortString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return this == o || o instanceof Measure<?> m && isEquivalent(m);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(m_magnitude, m_baseUnitMagnitude, m_unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public MutSelf mut_replace(double magnitude, U newUnit) {
|
||||
this.m_unit = Objects.requireNonNull(newUnit, "New unit cannot be null");
|
||||
this.m_magnitude = magnitude;
|
||||
this.m_baseUnitMagnitude = m_unit.toBaseUnits(magnitude);
|
||||
|
||||
return (MutSelf) this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user