Class Unit

java.lang.Object
com.tccc.kos.commons.util.units.Unit
Direct Known Subclasses:
BarUnit, CelsiusUnit, FahrenheitUnit, FluidOunceUnit, GramUnit, MilliliterUnit, OunceUnit, PsiUnit

public abstract class Unit extends Object
This class describes a single unit within a unit system. It allows new units to be added to existing unit systems. The parts of the unit are described below:
  • measure : What the unit measures. This is used to aggregate multiple units that measure the same thing. For example, a measure of "length" may include inches, feet, miles, etc... All units that measure the same property should have the same name in order to group them. Where applicable, use the MEASURE_XXX constants to ensure consistency.
  • name : The name of the unit, such as inches, feet, miles. When possible, use the standardized name from the link below to allow UI code to use standardized labels for units. These values are not intended for display. They are leveraged by UI code to use standardized unit labels or can be used to generate localization keys for display.
  • hint : When multiple units for the same measure, the hint to use to select this particular unit. Within every measure, exactly one unit should have a hint of "default". Passing null as the hint will result in the unit being tagged as default. Every other unit requires a unique hint. For example, if the default unit for volume is fluid ounces but one display element requires gallons, it would use the hint for gallons to perform the correct type conversion.
  • scale and offset : The m and b from y=mx+b used to convert from base units (typically SI) to this particular unit.
  • decimals : The default number of decimal places to show when formatting a value in this unit. For example, ml may have zero decimals while liters may have one depending on the typical usage of the value.

Equality is based on measure and name only. This means that any existing unit can be replaced in an existing system by simply defining a new unit with the same measure and name.

Since:
1.0
Version:
2023-03-03
See Also:
  • Field Details

  • Constructor Details

    • Unit

      public Unit(String measure, String name, String alias, boolean def, double scale, double offset, int decimals)
      Creates a new Unit to be added to a UnitSystem . A UnitSystem can have multiple units for the same type of measure (inches and feet are both lengths), and these units can have aliases which allow for cross-system support. For example, consider inches, feet, and miles vs. centimeters, meters, and kilometers. These are units of length in two different systems at three different scales. By using aliases it is possible to use the correct unit in any scale. Inches and centimeters may both have an alias of short, feet and meters an alias of medium, miles and kilometers an alias of long. This allows code to display big distances in long units and the system will simply use the correct unit for that system.

      Within a unit system, exactly one unit per measure should be marked default. When selecting a unit from a system by measure only, the default unit will be returned. When selecting a unit from a system by measure and alias, the defaul will be returned if no matching alias is found.

      Parameters:
      measure - what the unit measures (use "MEASURE_???" constants where applicable)
      name - name of the unit (use standard unit names as noted in the class javadoc)
      alias - alternate name for this unit which may be system neutral (such as oz/ml/etc...)
      def - if true, this is the default unit for the measure in a system
      scale - m from y=mx+b used to convert from base units to this unit
      offset - b from y=mx+b used to convert from base units to this unit
      decimals - suggested number of decimal places to show
  • Method Details

    • setDefault

      public void setDefault(boolean def)
      Sets the default flag on the unit. Exactly one unit for each measure should be marked default in a unit system. This is the unit that is used if no alias is specified or matched.
    • isDefault

      public boolean isDefault()
      Returns true if this is the default unit for the measure in the system.
    • setAlias

      public void setAlias(String alias)
      Sets the alias for the unit.
    • toStandard

      public double toStandard(double val)
      Converts from this unit to standard kOS units.
      Parameters:
      val - the value in the current unit to convert to standard unit
      Returns:
      value in standard kOS units
    • fromStandard

      public double fromStandard(double val)
      Converts from standard kOS units to this unit.
      Parameters:
      val - the value in standard units to convert to this unit
      Returns:
      value in this unit
    • convert

      public double convert(double val, Unit unit)
      Converts the specified value from this unit to the specified unit. The units must measure the same type of property (temperature, mass, volume, etc.).
      Parameters:
      val - the value in this unit
      unit - the target unit to convert to
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • getMeasure

      public String getMeasure()
    • getName

      public String getName()
    • getAlias

      public String getAlias()
    • getScale

      public double getScale()
    • getOffset

      public double getOffset()
    • getDecimals

      public int getDecimals()