Class NumberValue<TYPE extends Number>

java.lang.Object
com.iizix.value.NumberValue<TYPE>
All Implemented Interfaces:
IValueObject<TYPE>
Direct Known Subclasses:
BigDecimalValue, BigIntegerValue, ByteValue, DoubleValue, FloatValue, IntegerValue, LongValue, ShortValue

public abstract class NumberValue<TYPE extends Number> extends Object implements IValueObject<TYPE>
Base class for Number values.
Author:
Christopher Mindus
  • Constructor Details

    • NumberValue

      public NumberValue()
  • Method Details

    • isRightAligned

      public final boolean isRightAligned()
      Checks if this type is right-aligned in table columns.
      Specified by:
      isRightAligned in interface IValueObject<TYPE extends Number>
      Returns:
      true for right-aligned values, default is false: left-aligned.
    • longValueExact

      public static long longValueExact(Number n)
      Narrows a number to a long exactly at the boundaries of the long range.

      A guard written as a doubleValue() comparison against Long.MAX_VALUE has a blind band: the bound rounds up to 2^63 as a double, so 2^63 itself passes the comparison and longValue() then wraps a BigInteger to Long.MIN_VALUE and saturates a double to Long.MAX_VALUE, in silence. Number.longValue() is as silent for NaN (0) and the infinities (the long extremes). This helper dispatches on the source class instead: the integral wrappers narrow as themselves, BigInteger and BigDecimal are checked exactly, and only a floating point source is compared as a double, against 2^63 itself so the comparison is exact.

      Parameters:
      n - The number, not null.
      Returns:
      The long value; a fractional part is truncated toward zero, as Number.longValue() truncates.
      Throws:
      ArithmeticException - If the number is NaN, infinite or outside the range of a long.
      NullPointerException - If n is null.
    • getFormatter

      public final NumberFormat getFormatter(String pattern, LocaleInfo localeInfo) throws IllegalArgumentException
      Returns the Formatter for a pattern.
      Specified by:
      getFormatter in interface IValueObject<TYPE extends Number>
      Parameters:
      pattern - The pattern to use, null for none.
      localeInfo - The locale information instance, or null for none.
      Returns:
      The formatter to use, or null for none.
      Throws:
      IllegalArgumentException - For pattern errors.
    • toString

      public final String toString(TYPE value)
      The default "toString" representation.
      Specified by:
      toString in interface IValueObject<TYPE extends Number>
      Parameters:
      value - The value could be null.
      Returns:
      The String representation.
    • toString

      public final String toString(TYPE value, String pattern, LocaleInfo localeInfo) throws Exception
      Formats the value to a String.
      Specified by:
      toString in interface IValueObject<TYPE extends Number>
      Parameters:
      value - The value is never null.
      pattern - Pattern to use, null for none.
      localeInfo - The locale information instance, or null for none.
      Returns:
      The String representation using specified pattern and locale information.
      Throws:
      Exception - For formatter errors.
    • parse

      public final TYPE parse(String value, String pattern, LocaleInfo localeInfo) throws ParseException, Exception
      Creates a Value object from a String using a specified pattern and locale.
      Specified by:
      parse in interface IValueObject<TYPE extends Number>
      Parameters:
      value - The value, never null.
      pattern - The pattern, null for none.
      localeInfo - The locale info, null for default.
      Returns:
      The value object, never null.
      Throws:
      ParseException - If the value could not be parsed.
      Exception - For other errors.
    • parse

      public abstract TYPE parse(String value) throws NumberFormatException
      Creates a Value object from a String using a specified pattern and locale.
      Specified by:
      parse in interface IValueObject<TYPE extends Number>
      Parameters:
      value - The value, never null.
      Returns:
      The value object, never null.
      Throws:
      NumberFormatException - For number format errors.
    • load

      public final TYPE load(String value, Element element) throws ParseException
      Loads the value from an Element.
      Specified by:
      load in interface IValueObject<TYPE extends Number>
      Parameters:
      value - The String value in the element.
      element - The element holding the information for advanced processing using JAXB.
      Throws:
      ParseException - An exception due to parsing, bean unmarshalling, etc.
    • save

      public final String save(TYPE value, Element element)
      Saves the value to an Element.
      Specified by:
      save in interface IValueObject<TYPE extends Number>
      Parameters:
      value - The value to save.
      element - The element to save information into for advanced processing using JAXB.
      Returns:
      The String value to set in the Element, or null if advanced processing is used.
    • asLong

      public final long asLong(TYPE value, LocaleInfo localeInfo)
      Returns a long value if possible.
      Specified by:
      asLong in interface IValueObject<TYPE extends Number>
      Parameters:
      value - The Value object.
      localeInfo - The locale information, null to look-up from current thread or if not found, for system default.
      Returns:
      The long value, e.g. in milliseconds, or Long.MIN_VALUE for not possible.