Enum DataUnit

  • All Implemented Interfaces:
    Serializable, Comparable<DataUnit>

    public enum DataUnit
    extends Enum<DataUnit>

    A DataUnit offers representation and conversion of various quantity of bytes/bits. Release 2.0.0 introduces exa, zetta, and yotta units.

    Decimal vs binary notations

    As of 2.0.0, this class aligns with the international standard system of units by treating decimal and binary notations distinctively. Each are represented as:

    Data unit notations
    DecimalBinary
    kkilo103 Kikibi210
    Mmega106 Mimebi220
    Ggiga109 Gigibi230
    Ttera1012 Titebi240
    Ppeta1015 Pipebi250
    Eexa1018 Eiexbi260
    Zzetta1021 Zizebi270
    Yyotta1024 Yiyobi280

    Bytes vs bits

    As of 2.0.0, this class supports bits in addition to bytes. They are represented as:

    Bytes and bits notations
    Bbytes (8-bit)
    bitbits

    Usage examples

       // how many kibibyte in a gibiabyte amount (binary notation).
       long kibibyte = DataUnit.GIB.to(3, KIB).longValue(); // results = 3072
    
       // how many megabyte in a kilobyte amount (decimal notation)
       float megabyte = DataUnit.KB.to(2500, MB).floatValue(); // results = 2.5
     
    Since:
    1.4.0
    Author:
    Pascal Essiembre
    See Also:
    DataUnitFormatter
    • Field Detail

      • DECIMAL_BIT_UNITS

        public static final List<DataUnit> DECIMAL_BIT_UNITS
      • DECIMAL_BYTE_UNITS

        public static final List<DataUnit> DECIMAL_BYTE_UNITS
      • BINARY_BIT_UNITS

        public static final List<DataUnit> BINARY_BIT_UNITS
      • BINARY_BYTE_UNITS

        public static final List<DataUnit> BINARY_BYTE_UNITS
    • Method Detail

      • values

        public static DataUnit[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (DataUnit c : DataUnit.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static DataUnit valueOf​(String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
        Parameters:
        name - the name of the enum constant to be returned.
        Returns:
        the enum constant with the specified name
        Throws:
        IllegalArgumentException - if this enum type has no constant with the specified name
        NullPointerException - if the argument is null
      • getSymbol

        public String getSymbol()
      • getPrefix

        public String getPrefix()
      • getName

        public String getName()
      • getGroupIndex

        public int getGroupIndex()
      • isBitUnit

        public boolean isBitUnit()
      • isByteUnit

        public boolean isByteUnit()
      • isBinary

        public boolean isBinary()
      • isDecimal

        public boolean isDecimal()
      • toBits

        public BigDecimal toBits​(double amount)
      • toBytes

        public BigDecimal toBytes​(double amount)
      • fromBits

        public BigDecimal fromBits​(double amount)
      • fromBytes

        public BigDecimal fromBytes​(double amount)
      • from

        public BigDecimal from​(double sourceAmount,
                               DataUnit sourceUnit)
        Converts an amount of a specific unit to this unit.
        Parameters:
        sourceAmount - source amount to convert
        sourceUnit - source unit to convert
        Returns:
        converted value
      • from

        public BigDecimal from​(BigDecimal sourceAmount,
                               DataUnit sourceUnit)
        Converts an amount of a specific unit to this unit.
        Parameters:
        sourceAmount - source amount to convert
        sourceUnit - source unit to convert
        Returns:
        converted value
      • from

        public static DataUnit from​(String dataUnit)
        Gets the unit value representing the given text. Both symbols or full names are supported (English or French, case-insensitive, ignoring accents).
        Parameters:
        dataUnit - the textual representation of the unit.
        Returns:
        DataUnit instance or null if no match.
      • convert

        @Deprecated
        public long convert​(long sourceAmount,
                            DataUnit sourceUnit)
        Deprecated.
        Converts a given source data amount and type to this type.
        Parameters:
        sourceAmount - source data amount
        sourceUnit - source data unit
        Returns:
        converted value
      • toKilobytes

        @Deprecated
        public long toKilobytes​(long amount)
        Deprecated.
      • toMegabytes

        @Deprecated
        public long toMegabytes​(long amount)
        Deprecated.
      • toGigabytes

        @Deprecated
        public long toGigabytes​(long amount)
        Deprecated.
      • toTerabytes

        @Deprecated
        public long toTerabytes​(long amount)
        Deprecated.
      • toPetabytes

        @Deprecated
        public long toPetabytes​(long amount)
        Deprecated.