mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +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:
117
wpiunits/src/generate/main/java/Measure-interface.java.jinja
Normal file
117
wpiunits/src/generate/main/java/Measure-interface.java.jinja
Normal file
@@ -0,0 +1,117 @@
|
||||
// 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.
|
||||
|
||||
// THIS FILE WAS AUTO-GENERATED BY ./wpiunits/generate_units.py. DO NOT MODIFY
|
||||
|
||||
package edu.wpi.first.units.measure;
|
||||
|
||||
import static edu.wpi.first.units.Units.*;
|
||||
import edu.wpi.first.units.*;
|
||||
|
||||
@SuppressWarnings({"unchecked", "cast", "checkstyle", "PMD"})
|
||||
public interface {{ helpers['type_decl'](name) }} extends Measure<{{ helpers['mtou'](name) }}> {
|
||||
static {{ helpers['generics_list'](name) }} {{ helpers['type_usage'](name) }} ofRelativeUnits(double magnitude, {{ helpers['mtou'](name) }} unit) {
|
||||
return new Immutable{{ helpers['type_usage'](name) }}(magnitude, unit.toBaseUnits(magnitude), unit);
|
||||
}
|
||||
|
||||
static {{ helpers['generics_list'](name) }} {{ helpers['type_usage'](name) }} ofBaseUnits(double baseUnitMagnitude, {{ helpers['mtou'](name) }} unit) {
|
||||
return new Immutable{{ helpers['type_usage'](name) }}(unit.fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
{{ helpers['type_usage'](name) }} copy();
|
||||
|
||||
@Override
|
||||
default Mut{{ helpers['type_usage'](name) }} mutableCopy() {
|
||||
return new Mut{{ helpers['type_usage'](name) }}(magnitude(), baseUnitMagnitude(), unit());
|
||||
}
|
||||
|
||||
@Override
|
||||
{{ helpers['mtou'](name) }} unit();
|
||||
|
||||
@Override
|
||||
default {{ helpers['mtou'](name) }} baseUnit() { return ({{ helpers['mtou'](name) }}) unit().getBaseUnit(); }
|
||||
|
||||
@Override
|
||||
default double in({{ helpers['mtou'](name) }} unit) {
|
||||
return unit.fromBaseUnits(baseUnitMagnitude());
|
||||
}
|
||||
|
||||
@Override
|
||||
default {{ helpers['type_usage'](name) }} unaryMinus() {
|
||||
return ({{ helpers['type_usage'](name) }}) unit().ofBaseUnits(0 - baseUnitMagnitude());
|
||||
}
|
||||
|
||||
@Override
|
||||
default {{ helpers['type_usage'](name) }} plus(Measure<? extends {{ helpers['mtou'](name) }}> other) {
|
||||
return ({{ helpers['type_usage'](name) }}) unit().ofBaseUnits(baseUnitMagnitude() + other.baseUnitMagnitude());
|
||||
}
|
||||
|
||||
@Override
|
||||
default {{ helpers['type_usage'](name) }} minus(Measure<? extends {{ helpers['mtou'](name) }}> other) {
|
||||
return ({{ helpers['type_usage'](name) }}) unit().ofBaseUnits(baseUnitMagnitude() - other.baseUnitMagnitude());
|
||||
}
|
||||
|
||||
@Override
|
||||
default {{ helpers['type_usage'](name) }} times(double multiplier) {
|
||||
return ({{ helpers['type_usage'](name) }}) unit().ofBaseUnits(baseUnitMagnitude() * multiplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
default {{ helpers['type_usage'](name) }} divide(double divisor) {
|
||||
return ({{ helpers['type_usage'](name) }}) unit().ofBaseUnits(baseUnitMagnitude() / divisor);
|
||||
}
|
||||
|
||||
@Override
|
||||
default {{ config[name]['divide']['Time'] or "Velocity<{}>".format(helpers['mtou'](name)) }} per(TimeUnit period) {
|
||||
return divide(period.of(1));
|
||||
}
|
||||
{% for unit in math_units -%}
|
||||
{% if unit == "Dimensionless" %}
|
||||
@Override
|
||||
default {{ helpers['type_usage'](name) }} divide({{ unit }} divisor) {
|
||||
return ({{ helpers['type_usage'](name) }}) {{ config[name]['base_unit'] }}.of(baseUnitMagnitude() / divisor.baseUnitMagnitude());
|
||||
}
|
||||
|
||||
@Override
|
||||
default {{ helpers['type_usage'](name) }} times({{ unit }} multiplier) {
|
||||
return ({{ helpers['type_usage'](name) }}) {{ config[name]['base_unit'] }}.of(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
|
||||
}
|
||||
{% else %}
|
||||
{% if unit in config[name]['multiply'] %}
|
||||
@Override
|
||||
default {{ config[name]['multiply'][unit] }} times({{ unit }} multiplier) {
|
||||
return {{ config[config[name]['multiply'][unit]]['base_unit'] }}.of(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
|
||||
}
|
||||
{% else %}
|
||||
@Override
|
||||
default Mult<{{ helpers['mtou'](name) }}, {{ helpers['mtou'](unit) }}> times({{ unit }} multiplier) {
|
||||
return (Mult<{{ helpers['mtou'](name) }}, {{ helpers['mtou'](unit) }}>) Measure.super.times(multiplier);
|
||||
}
|
||||
{% endif -%}
|
||||
{% if unit in config[name]['divide'] %}
|
||||
@Override
|
||||
default {{ config[name]['divide'][unit] }} divide({{ unit }} divisor) {
|
||||
return {{ config[config[name]['divide'][unit]]['base_unit'] }}.of(baseUnitMagnitude() / divisor.baseUnitMagnitude());
|
||||
}
|
||||
{% elif unit == "Time" %}
|
||||
@Override
|
||||
default Velocity<{{ helpers['mtou'](name) }}> divide({{ unit }} divisor) {
|
||||
return VelocityUnit.combine(unit(), divisor.unit()).ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
|
||||
}
|
||||
{% elif unit == name %}
|
||||
@Override
|
||||
default Dimensionless divide({{ unit }} divisor) {
|
||||
return Value.of(baseUnitMagnitude() / divisor.baseUnitMagnitude());
|
||||
}
|
||||
{% else %}
|
||||
@Override
|
||||
default Per<{{ helpers['mtou'](name) }}, {{ helpers['mtou'](unit) }}> divide({{ unit }} divisor) {
|
||||
return (Per<{{ helpers['mtou'](name) }}, {{ helpers['mtou'](unit) }}>) Measure.super.divide(divisor);
|
||||
}
|
||||
{% endif -%}
|
||||
{% endif -%}
|
||||
{% endfor -%}
|
||||
{{ config[name]['extra'] }}
|
||||
}
|
||||
Reference in New Issue
Block a user