Use Java 17 features (#6691)

Uses enhanced instanceof (and simplify equals methods)
Uses switch expressions and arrow labels
Seal and finalize some Shuffleboard classes

Co-authored-by: Sam Carlberg <sam@slfc.dev>
This commit is contained in:
Gold856
2024-06-05 00:09:10 -04:00
committed by GitHub
parent d6b66bfa55
commit b99d9c1710
95 changed files with 957 additions and 1594 deletions

View File

@@ -83,14 +83,9 @@ public class ImmutableMeasure<U extends Unit<U>> implements Measure<U> {
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Measure)) {
return false;
}
Measure<?> that = (Measure<?>) o;
return Objects.equals(m_unit, that.unit()) && m_baseUnitMagnitude == that.baseUnitMagnitude();
return o instanceof Measure<?> that
&& Objects.equals(m_unit, that.unit())
&& m_baseUnitMagnitude == that.baseUnitMagnitude();
}
@Override

View File

@@ -85,36 +85,29 @@ public interface Measure<U extends Unit<U>> extends Comparable<Measure<U>> {
* @param other the unit to multiply by
* @return the multiplicative unit
*/
@SuppressWarnings("unchecked")
default <U2 extends Unit<U2>> Measure<?> times(Measure<U2> other) {
if (other.unit() instanceof Dimensionless) {
// scalar multiplication
return times(other.baseUnitMagnitude());
}
if (unit() instanceof Per
&& other.unit().getBaseUnit().equals(((Per<?, ?>) unit()).denominator().getBaseUnit())) {
if (unit() instanceof Per<?, ?> per
&& other.unit().getBaseUnit().equals(per.denominator().getBaseUnit())) {
// denominator of the Per cancels out, return with just the units of the numerator
Unit<?> numerator = ((Per<?, ?>) unit()).numerator();
Unit<?> numerator = per.numerator();
return numerator.ofBaseUnits(baseUnitMagnitude() * other.baseUnitMagnitude());
} else if (unit() instanceof Velocity && other.unit().getBaseUnit().equals(Seconds)) {
} else if (unit() instanceof Velocity<?> v && other.unit().getBaseUnit().equals(Seconds)) {
// Multiplying a velocity by a time, return the scalar unit (eg Distance)
Unit<?> numerator = ((Velocity<?>) unit()).getUnit();
Unit<?> numerator = v.getUnit();
return numerator.ofBaseUnits(baseUnitMagnitude() * other.baseUnitMagnitude());
} else if (other.unit() instanceof Per
&& unit().getBaseUnit().equals(((Per<?, ?>) other.unit()).denominator().getBaseUnit())) {
Unit<?> numerator = ((Per<?, ?>) other.unit()).numerator();
} else if (other.unit() instanceof Per<?, ?> per
&& unit().getBaseUnit().equals(per.denominator().getBaseUnit())) {
Unit<?> numerator = per.numerator();
return numerator.ofBaseUnits(baseUnitMagnitude() * other.baseUnitMagnitude());
} else if (unit() instanceof Per
&& other.unit() instanceof Per
&& ((Per<?, ?>) unit())
.denominator()
.getBaseUnit()
.equals(((Per<?, U>) other.unit()).numerator().getBaseUnit())
&& ((Per<?, ?>) unit())
.numerator()
.getBaseUnit()
.equals(((Per<?, ?>) other.unit()).denominator().getBaseUnit())) {
} else if (unit() instanceof Per<?, ?> per
&& other.unit() instanceof Per<?, ?> otherPer
&& per.denominator().getBaseUnit().equals(otherPer.numerator().getBaseUnit())
&& per.numerator().getBaseUnit().equals(otherPer.denominator().getBaseUnit())) {
// multiplying eg meters per second * milliseconds per foot
// return a scalar
return Units.Value.of(baseUnitMagnitude() * other.baseUnitMagnitude());

View File

@@ -286,14 +286,9 @@ public final class MutableMeasure<U extends Unit<U>> implements Measure<U> {
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Measure)) {
return false;
}
Measure<?> that = (Measure<?>) o;
return Objects.equals(m_unit, that.unit()) && this.isEquivalent(that);
return o instanceof Measure<?> that
&& Objects.equals(m_unit, that.unit())
&& this.isEquivalent(that);
}
@Override

View File

@@ -289,11 +289,10 @@ public class Unit<U extends Unit<U>> {
if (this == o) {
return true;
}
if (!(o instanceof Unit)) {
return false;
}
Unit<?> that = (Unit<?>) o;
return m_name.equals(that.m_name) && m_symbol.equals(that.m_symbol) && this.equivalent(that);
return o instanceof Unit<?> that
&& m_name.equals(that.m_name)
&& m_symbol.equals(that.m_symbol)
&& this.equivalent(that);
}
@Override