// 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 static edu.wpi.first.units.Units.Seconds; /** * A measure holds the magnitude and unit of some dimension, such as distance, time, or speed. Two * measures with the same unit and magnitude are effectively equivalent objects. * * @param the unit type of the measure */ public interface Measure> extends Comparable> { /** * The threshold for two measures to be considered equivalent if converted to the same unit. This * is only needed due to floating-point error. */ double EQUIVALENCE_THRESHOLD = 1e-12; /** * Gets the unitless magnitude of this measure. * * @return the magnitude in terms of {@link #unit() the unit}. */ double magnitude(); /** * Gets the magnitude of this measure in terms of the base unit. If the unit is the base unit for * its system of measure, then the value will be equivalent to {@link #magnitude()}. * * @return the magnitude in terms of the base unit */ double baseUnitMagnitude(); /** * Gets the units of this measure. * * @return the unit */ U unit(); /** * Converts this measure to a measure with a different unit of the same type, eg minutes to * seconds. Converting to the same unit is equivalent to calling {@link #magnitude()}. * *
   *   Meters.of(12).in(Feet) // 39.3701
   *   Seconds.of(15).in(Minutes) // 0.25
   * 
* * @param unit the unit to convert this measure to * @return the value of this measure in the given unit */ default double in(Unit unit) { if (this.unit().equals(unit)) { return magnitude(); } else { return unit.fromBaseUnits(baseUnitMagnitude()); } } /** * Multiplies this measurement by some constant multiplier and returns the result. The magnitude * of the result will be the base magnitude multiplied by the scalar value. If the measure * uses a unit with a non-linear relation to its base unit (such as Fahrenheit for temperature), * then the result will only be a multiple in terms of the base unit. * * @param multiplier the constant to multiply by * @return the resulting measure */ default Measure times(double multiplier) { return ImmutableMeasure.ofBaseUnits(baseUnitMagnitude() * multiplier, unit()); } /** * Generates a new measure that is equal to this measure multiplied by another. Some dimensional * analysis is performed to reduce the units down somewhat; for example, multiplying a {@code * Measure