Class DataUnitFormatter

  • All Implemented Interfaces:
    Serializable

    public class DataUnitFormatter
    extends Object
    implements Serializable

    Formats a data unit to a human-readable string.

    As of 2.0.0, a distinction is made between decimal (default) and binary units. Also, units are now supported up to yotta (10008) or yobi (10248).

    Since:
    1.4.0
    Author:
    Pascal Essiembre
    See Also:
    Serialized Form
    • Constructor Detail

      • DataUnitFormatter

        public DataUnitFormatter()
        Creates a new DataUnit formatter using decimal notation, with default system locale, and without decimals.
      • DataUnitFormatter

        public DataUnitFormatter​(DataUnitFormatter formatter)
        Copy constructor. Passing a null formatter is the same as invoking DataUnitFormatter() without arguments
        Parameters:
        formatter - formatter to copy
      • DataUnitFormatter

        @Deprecated
        public DataUnitFormatter​(Locale locale)
        Deprecated.
        Since 2.0.0 use setter methods.
        Creates a new DataUnit formatter without decimals. The decimal and thousand separator symbols are set according to supplied locale.
        Parameters:
        locale - a locale
      • DataUnitFormatter

        @Deprecated
        public DataUnitFormatter​(int decimalPrecision)
        Deprecated.
        Since 2.0.0 use setter methods.
        Creates a new DataUnit formatter with default system locale with the maximum number of decimals returned when applicable.
        Parameters:
        decimalPrecision - number of decimals
      • DataUnitFormatter

        @Deprecated
        public DataUnitFormatter​(Locale locale,
                                 int decimalPrecision)
        Deprecated.
        Since 2.0.0 use setter methods.
        Creates a new DataUnit formatter with the maximum number of decimals returned when applicable. The decimal and thousand separator symbols are set according to supplied locale.
        Parameters:
        locale - a locale
        decimalPrecision - number of decimals
      • DataUnitFormatter

        @Deprecated
        public DataUnitFormatter​(Locale locale,
                                 int decimalPrecision,
                                 boolean fixedUnit)
        Deprecated.
        Since 2.0.0 use setter methods.
        Creates a new DataUnit formatter with the maximum number of decimals returned when applicable. The decimal and thousand separator symbols are set according to supplied locale. Set the fixedUnit to true to ensure the data units supplied are not change in the formatting.
        Parameters:
        locale - a locale
        decimalPrecision - number of decimals
        fixedUnit - true to keep original unit in formatting
    • Method Detail

      • getLocale

        public Locale getLocale()
      • getDecimalPrecision

        public int getDecimalPrecision()
      • setDecimalPrecision

        public DataUnitFormatter setDecimalPrecision​(int decimalPrecision)
      • withDecimalPrecision

        public DataUnitFormatter withDecimalPrecision​(int decimalPrecision)
      • isFixedUnit

        public boolean isFixedUnit()
      • isBinaryNotation

        public boolean isBinaryNotation()
      • setBinaryNotation

        public DataUnitFormatter setBinaryNotation​(boolean binaryNotation)
      • withBinaryNotation

        public DataUnitFormatter withBinaryNotation​(boolean binaryNotation)
      • format

        public String format​(double amount,
                             DataUnit unit)
        Formats a data amount of the given unit to a human-readable representation.
        Parameters:
        amount - the amount to format
        unit - the data unit type of the amount
        Returns:
        formatted string
      • format

        public String format​(BigDecimal amount,
                             DataUnit unit)
        Formats a data amount of the given unit to a human-readable representation.
        Parameters:
        amount - the amount to format
        unit - the data unit type of the amount
        Returns:
        formatted string
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object