程序包 org.cooder.units

类 UnitNumber<Q extends javax.measure.Quantity<Q>>

  • 类型参数:
    Q - 量的类型

    public final class UnitNumber<Q extends javax.measure.Quantity<Q>>
    extends Object
    表示现象、物体或物质的定量特性,比如质量、时间、距离、角度等。
    这个类是 JSR-385 的核心接口 Quantity 的封装。
    作者:
    wuling
    另请参阅:
    Quantity
    • 字段详细资料

      • UNKNOWN_NUMBER

        public static final UnitNumber<?> UNKNOWN_NUMBER
    • 构造器详细资料

      • UnitNumber

        public UnitNumber​(Number value,
                          javax.measure.Unit<Q> unit)
    • 方法详细资料

      • getValue

        public Number getValue()
      • getUnit

        public javax.measure.Unit<Q> getUnit()
      • add

        public UnitNumber<Q> add​(UnitNumber<Q> addend)
        计算当前量与指定量的和,会自动进行单位转换
        参数:
        addend - 加量
        返回:
        this + addend
      • subtract

        public UnitNumber<Q> subtract​(UnitNumber<Q> subtrahend)
        计算当前量与指定量的差,会自动进行单位转化
        参数:
        subtrahend - 减数
        返回:
        this - subtrahend
      • multiply

        public UnitNumber<Q> multiply​(Number multiplicand)
        计算当前量与指定数值的乘积,单位不会变化
        参数:
        multiplicand - 乘数
        返回:
        this × multiplicand
      • multiply

        public UnitNumber<?> multiply​(UnitNumber<?> multiplicand)
        计算当前量与指定量的乘积,计算后结果会转换为国际单位制。 如果需要转换为指定单位,可以使用to(javax.measure.Unit<Q>)
        参数:
        multiplicand - 乘量
        返回:
        this × multiplicand
      • divide

        public UnitNumber<Q> divide​(Number divisor)
        计算当前量与指定数值的商,单位不会变化
        参数:
        divisor - 除数
        返回:
        this ÷ divisor
      • divide

        public UnitNumber<?> divide​(UnitNumber<?> divisor)
        计算当前量与指定量的商,计算后结果会转换为国际单位制。 如果需要转换为指定单位,可以使用to(javax.measure.Unit<Q>)
        参数:
        divisor - 除数
        返回:
        this ÷ divisor
      • inverse

        public UnitNumber<?> inverse()
        求倒数,数值和单位都会求倒数,比如:
        "10 s" 的倒数是 "0.1 1/s"
        返回:
        倒数
      • negate

        public UnitNumber<Q> negate()
        求相反数
        返回:
        -this.
      • to

        public UnitNumber<Q> to​(javax.measure.Unit<Q> unit)
        单位转换
        参数:
        unit - 转换后的单位,必须保证转换后的单位是等价的
        返回:
        单位转换后的量
      • toSystemUnit

        public UnitNumber<Q> toSystemUnit()
        转换为国际标准单位制
        返回:
        单位转换后量
      • isEquivalentTo

        public boolean isEquivalentTo​(UnitNumber<Q> that)
        将当前量与指定量进行比较, 必要时进行单位转换
        参数:
        that - 指定量
        返回:
        true if that ≡ this.
        另请参阅:
        Quantity.isEquivalentTo(Quantity)
      • compareTo

        public int compareTo​(UnitNumber<Q> that)
        将当前量与指定量进行比较, 如果两个量的单位不完全一致,会直接抛出异常
        参数:
        that - 指定量
        返回:
        the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y
        抛出:
        IllegalStateException - 如果两者的单位不完全一样
      • assertMustBe

        public UnitNumber<Q> assertMustBe​(javax.measure.Unit<?> that)
        判断当前量的单位与指定的单位是否一致,不一致时将抛出异常
        参数:
        that - 指定的单位
        返回:
        this
        抛出:
        IllegalStateException - 如果两者的单位不完全一样
      • assertIncludedInUnits

        public UnitNumber<Q> assertIncludedInUnits()
        判断当前量的单位是否包含在标准单位中,不存在时抛出异常
        返回:
        this
      • hashCode

        public int hashCode()
        覆盖:
        hashCode 在类中 Object
      • parse

        public static UnitNumber<?> parse​(CharSequence cs)
        从字符串里解析出量,混合单位的量也可以解析。比如下面这些表达式都可以支持:
        UnitNumber<Length> height = UnitNumber.parse("1.70 m").asType(Length.class);
        UnitNumber<Length> height = UnitNumber.parse("170 cm").asType(Length.class);
        UnitNumber<Length> height = UnitNumber.parse("1 m 70 cm").asType(Length.class);
        参数:
        cs - 字符串
        返回:
        解析出来的UnitNumber实例
      • add

        public static UnitNumber<?> add​(UnitNumber<?> number1,
                                        UnitNumber<?> number2)
        number1 + number2
        参数:
        number1 -
        number2 -
        返回:
        number1 + number2
      • subtract

        public static UnitNumber<?> subtract​(UnitNumber<?> number1,
                                             UnitNumber<?> number2)
        number1 - number2
        参数:
        number1 -
        number2 -
        返回:
        number1 - number2
      • compare

        public static int compare​(UnitNumber<?> x,
                                  UnitNumber<?> y)
        compare two number with unknown unit.
        参数:
        x - one number
        y - another number
        返回:
        the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y
        抛出:
        IllegalStateException - if unit not compatible