mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[wpiunits] Java units API rewrite (#6958)
Java generics are too limited to do what we need. This refactors generic code previously in Unit and Measure into unit-specific classes that can have unit-safe math operations (notably, times and divide) that can return values in known units instead of a wildcarded Measure<?>. Unit-specific measure implementations are automatically generated by ./wpiunits/generate_units.py, which generates generic interfaces and mutable and immutable implementations of those interfaces. These make up the bulk of the diff of this PR (approximately 9300 LOC). This also adds units for angular and linear velocities, accelerations, and momenta; moment of inertia; and torque.
This commit is contained in:
102
wpiunits/src/main/java/edu/wpi/first/units/DistanceUnit.java
Normal file
102
wpiunits/src/main/java/edu/wpi/first/units/DistanceUnit.java
Normal file
@@ -0,0 +1,102 @@
|
||||
// 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.Distance;
|
||||
import edu.wpi.first.units.measure.ImmutableDistance;
|
||||
import edu.wpi.first.units.measure.MutDistance;
|
||||
|
||||
/**
|
||||
* Unit of linear dimension.
|
||||
*
|
||||
* <p>This is the base type for units of linear dimension. It is also used to specify the dimension
|
||||
* for {@link Measure}: <code>Measure<DistanceUnit></code>.
|
||||
*
|
||||
* <p>Actual units (such as {@link Units#Meters} and {@link Units#Inches}) can be found in the
|
||||
* {@link Units} class.
|
||||
*/
|
||||
public final class DistanceUnit extends Unit {
|
||||
DistanceUnit(DistanceUnit baseUnit, double baseUnitEquivalent, String name, String symbol) {
|
||||
super(baseUnit, baseUnitEquivalent, name, symbol);
|
||||
}
|
||||
|
||||
DistanceUnit(
|
||||
DistanceUnit baseUnit,
|
||||
UnaryFunction toBaseConverter,
|
||||
UnaryFunction fromBaseConverter,
|
||||
String name,
|
||||
String symbol) {
|
||||
super(baseUnit, toBaseConverter, fromBaseConverter, name, symbol);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DistanceUnit getBaseUnit() {
|
||||
return (DistanceUnit) super.getBaseUnit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public LinearVelocityUnit per(TimeUnit period) {
|
||||
return LinearVelocityUnit.combine(this, period);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a ratio unit between this unit and an arbitrary other unit.
|
||||
*
|
||||
* @param other the other unit
|
||||
* @param <U> the type of the other unit
|
||||
* @return the ratio unit
|
||||
*/
|
||||
public <U extends Unit> PerUnit<DistanceUnit, U> per(U other) {
|
||||
return PerUnit.combine(this, other);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a measurement value in terms of another distance unit to this unit.
|
||||
*
|
||||
* @param magnitude the magnitude of the measurement in terms of the other distance unit
|
||||
* @param otherUnit the other distance unit
|
||||
* @return the value of the measurement in terms of this unit
|
||||
*/
|
||||
public double convertFrom(double magnitude, DistanceUnit otherUnit) {
|
||||
return fromBaseUnits(otherUnit.toBaseUnits(magnitude));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Distance of(double magnitude) {
|
||||
return new ImmutableDistance(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Distance ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableDistance(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Distance zero() {
|
||||
return (Distance) super.zero();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Distance one() {
|
||||
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.
|
||||
*
|
||||
* @param force the unit of force
|
||||
* @return the combined torque unit
|
||||
*/
|
||||
public TorqueUnit multAsTorque(ForceUnit force) {
|
||||
return TorqueUnit.combine(this, force);
|
||||
}
|
||||
|
||||
// TODO: Add a multAsEnergy equivalent
|
||||
}
|
||||
Reference in New Issue
Block a user