Class SimpleUnit

  • Direct Known Subclasses:
    TimeUnit

    public class SimpleUnit
    extends Object
    Convenience routines on top of ucar.units package.

    The ucar.units package handles

    1. scientific units, which are factors of the fundamental dimensions such as length, time, mass, etc
    2. dates, represented as "n units of time since reference date" eg "1203 days since 1970-01-01 00:00:00"
    • Field Detail

      • meterUnit

        public static final SimpleUnit meterUnit
      • pressureUnit

        public static final SimpleUnit pressureUnit
      • secsUnit

        protected static Unit secsUnit
      • dateReferenceUnit

        protected static Unit dateReferenceUnit
      • debugParse

        protected static boolean debugParse
      • uu

        protected Unit uu
    • Constructor Detail

      • SimpleUnit

        protected SimpleUnit()
        for subclasses.
    • Method Detail

      • factory

        public static SimpleUnit factory​(String name)
        Create a SimpleUnit from the given name, catch Exceptions.
        Parameters:
        name - parse this name to create a unit.
        Returns:
        SimpleUnit, DateUnit, TimeUnit, or null if failed
        See Also:
        UnitFormat.parse(java.lang.String)
      • isCompatible

        public static boolean isCompatible​(String unitString1,
                                           String unitString2)
        Return true if unitString1 is compatible to unitString2, meaning one can be converted to the other. If either unit string is illegal, return false.
        Parameters:
        unitString1 - compare this unit
        unitString2 - compare this unit
        Returns:
        true if the 2 units are compatible
      • isCompatibleWithExceptions

        public static boolean isCompatibleWithExceptions​(String unitString1,
                                                         String unitString2)
                                                  throws UnitException
        Return true if unitString1 is convertible to unitString2
        Parameters:
        unitString1 - compare this unit
        unitString2 - compare this unit
        Returns:
        true if the 2 units are compatible
        Throws:
        UnitException - if units parsing fails
      • isDateUnit

        public static boolean isDateUnit​(Unit uu)
        Return true if this ucar.units.Unit is a Date.
        Parameters:
        uu - check this Unit
        Returns:
        true if its a Date
      • isTimeUnit

        public static boolean isTimeUnit​(Unit uu)
        Return true if this ucar.units.Unit is convertible to secs.
        Parameters:
        uu - check this Unit
        Returns:
        true if its a Time
      • isDateUnit

        public static boolean isDateUnit​(String unitString)
        Return true if the given unit is convertible to a date Unit. allowed format is something like:
         [-]Y[Y[Y[Y]]]-MM-DD[(T| )hh[:mm[:ss[.sss*]]][ [+|-]hh[[:]mm]]]
         
        Parameters:
        unitString - check this unit string
        Returns:
        true if its a Date
      • isTimeUnit

        public static boolean isTimeUnit​(String unitString)
        Return true if the given unit is a time Unit, eg "seconds".
        Parameters:
        unitString - check this unit string
        Returns:
        true if its a Time
      • getConversionFactor

        public static double getConversionFactor​(String inputUnitString,
                                                 String outputUnitString)
                                          throws IllegalArgumentException
        Get the conversion factor to convert inputUnit to outputUnit.
        Parameters:
        inputUnitString - inputUnit in string form
        outputUnitString - outputUnit in string form
        Returns:
        conversion factor
        Throws:
        IllegalArgumentException - if not convertible
      • toString

        public String toString()
        Unit string representation.
        Overrides:
        toString in class Object
      • getUnit

        public Unit getUnit()
        Get underlying ucar.units.Unit.
        Returns:
        underlying ucar.units.Unit.
      • convertTo

        public double convertTo​(double value,
                                SimpleUnit outputUnit)
                         throws IllegalArgumentException
        Convert given value of this unit to the new unit.
        Parameters:
        value - value in this unit
        outputUnit - convert to this unit
        Returns:
        value in outputUnit
        Throws:
        IllegalArgumentException - if outputUnit not convertible from this unit
      • isCompatible

        public boolean isCompatible​(String unitString)
        Return true if unitString1 is compatible to unitString2, meaning one can be converted to the other. If either unit string is illegal, return false.
        Parameters:
        unitString - check if this is compatible with unitString
        Returns:
        true if compatible
      • isUnknownUnit

        public boolean isUnknownUnit()
        Is this an instance of an UnknownUnit?
        Returns:
        true if an instance of an UnknownUnit
      • getValue

        public double getValue()
        Extract the value, can only be called for ScaledUnit.
        Returns:
        value of this unit if ScaledUnit, else NaN
      • getUnitString

        public String getUnitString()
        Extract the simple unit string (no number), eg "s" or "m".
        Returns:
        unit string with no value
      • getCanonicalString

        public String getCanonicalString()
      • getImplementingClass

        public String getImplementingClass()