Skip to content

Commit

Permalink
[wpiunits] Add Measure.per overloads for all known unit types (#7699)
Browse files Browse the repository at this point in the history
Instead of only providing per(TimeUnit)

Useful for making conversion factors easier, eg `Inches.of(10).per(Rotation)` vs `Inches.of(10).per(Rotation.one())`

Update VelocityUnit.one() and VelocityUnit.zero() to return Velocity objects instead of generic Measure<? extends VelocityUnit<D>>; VelocityUnit is final, so the wildcard generic is unnecessary, and this makes the generated `per` functions possible for this type
  • Loading branch information
SamCarlberg authored Jan 17, 2025
1 parent a9f3fc6 commit e52f400
Show file tree
Hide file tree
Showing 28 changed files with 3,300 additions and 145 deletions.
37 changes: 32 additions & 5 deletions wpiunits/src/generate/main/java/Measure-interface.java.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ public interface {{ helpers['type_decl'](name) }} extends Measure<{{ helpers['mt
default {{ helpers['type_usage'](name) }} divide(double divisor) {
return ({{ helpers['type_usage'](name) }}) div(divisor);
}

@Override
default {{ config[name]['divide']['Time'] or "Velocity<{}>".format(helpers['mtou'](name)) }} per(TimeUnit period) {
return div(period.of(1));
}
{% for unit in math_units -%}
{% if unit == "Dimensionless" %}
@Override
Expand Down Expand Up @@ -147,6 +142,15 @@ public interface {{ helpers['type_decl'](name) }} extends Measure<{{ helpers['mt
default {{ config[name]['divide'][unit] }} divide({{ unit }} divisor) {
return div(divisor);
}

@Override
default {{ config[name]['divide'][unit] }} per({{ helpers['mtou'](unit) }} divisorUnit) {
{%- if unit == "Mult<?, ?>" or unit == "Per<?, ?>" %}
return div(divisorUnit.ofNative(1));
{%- else %}
return div(divisorUnit.one());
{%- endif %}
}
{% elif unit == "Time" %}
@Override
default Velocity<{{ helpers['mtou'](name) }}> div({{ unit }} divisor) {
Expand All @@ -164,6 +168,15 @@ public interface {{ helpers['type_decl'](name) }} extends Measure<{{ helpers['mt
default Velocity<{{ helpers['mtou'](name) }}> divide({{ unit }} divisor) {
return div(divisor);
}

@Override
default Velocity<{{ helpers['mtou'](name) }}> per({{ helpers['mtou'](unit) }} divisorUnit) {
{%- if unit == "Mult<?, ?>" or unit == "Per<?, ?>" %}
return div(divisorUnit.ofNative(1));
{%- else %}
return div(divisorUnit.one());
{%- endif %}
}
{% elif unit == name %}
@Override
default Dimensionless div({{ unit }} divisor) {
Expand All @@ -181,6 +194,11 @@ public interface {{ helpers['type_decl'](name) }} extends Measure<{{ helpers['mt
default Dimensionless divide({{ unit }} divisor) {
return div(divisor);
}

@Override
default Dimensionless per({{ helpers['mtou'](unit) }} divisorUnit) {
return div(divisorUnit.one());
}
{% else %}
@Override
default Per<{{ helpers['mtou'](name) }}, {{ helpers['mtou'](unit) }}> div({{ unit }} divisor) {
Expand All @@ -198,6 +216,15 @@ public interface {{ helpers['type_decl'](name) }} extends Measure<{{ helpers['mt
default Per<{{ helpers['mtou'](name) }}, {{ helpers['mtou'](unit) }}> divide({{ unit }} divisor) {
return div(divisor);
}

@Override
default Per<{{ helpers['mtou'](name) }}, {{ helpers['mtou'](unit) }}> per({{ helpers['mtou'](unit) }} divisorUnit) {
{%- if unit == "Mult<?, ?>" or unit == "Per<?, ?>" %}
return div(divisorUnit.ofNative(1));
{%- else %}
return div(divisorUnit.one());
{%- endif %}
}
{% endif -%}
{% endif -%}
{% endfor -%}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e52f400

Please sign in to comment.