org.springframework.util.unit

Class DataSize

  • java.lang.Object
    • org.springframework.util.unit.DataSize
  • All Implemented Interfaces:
    java.lang.Comparable<DataSize>


    public final class DataSize
    extends java.lang.Object
    implements java.lang.Comparable<DataSize>
    A data size, such as '12MB'.

    This class models a size in terms of bytes and is immutable and thread-safe.

    Since:
    5.1
    Author:
    Stephane Nicoll
    • Method Detail

      • ofBytes

        public static DataSize ofBytes(long bytes)
        Obtain a DataSize representing the specified number of bytes.
        Parameters:
        bytes - the number of bytes, positive or negative
        Returns:
        a DataSize
      • ofKilobytes

        public static DataSize ofKilobytes(long kilobytes)
        Obtain a DataSize representing the specified number of kilobytes.
        Parameters:
        kilobytes - the number of kilobytes, positive or negative
        Returns:
        a DataSize
      • ofMegabytes

        public static DataSize ofMegabytes(long megabytes)
        Obtain a DataSize representing the specified number of megabytes.
        Parameters:
        megabytes - the number of megabytes, positive or negative
        Returns:
        a DataSize
      • ofGigabytes

        public static DataSize ofGigabytes(long gigabytes)
        Obtain a DataSize representing the specified number of gigabytes.
        Parameters:
        gigabytes - the number of gigabytes, positive or negative
        Returns:
        a DataSize
      • ofTerabytes

        public static DataSize ofTerabytes(long terabytes)
        Obtain a DataSize representing the specified number of terabytes.
        Parameters:
        terabytes - the number of terabytes, positive or negative
        Returns:
        a DataSize
      • of

        public static DataSize of(long amount,
                                  DataUnit unit)
        Obtain a DataSize representing an amount in the specified DataUnit.
        Parameters:
        amount - the amount of the size, measured in terms of the unit, positive or negative
        Returns:
        a corresponding DataSize
      • parse

        public static DataSize parse(java.lang.CharSequence text)
        Obtain a DataSize from a text string such as 12MB using DataUnit.BYTES if no unit is specified.

        Examples:

         "12KB" -- parses as "12 kilobytes"
         "5MB"  -- parses as "5 megabytes"
         "20"   -- parses as "20 bytes"
         
        Parameters:
        text - the text to parse
        Returns:
        the parsed DataSize
        See Also:
        parse(CharSequence, DataUnit)
      • parse

        public static DataSize parse(java.lang.CharSequence text,
                                     @Nullable
                                     DataUnit defaultUnit)
        Obtain a DataSize from a text string such as 12MB using the specified default DataUnit if no unit is specified.

        The string starts with a number followed optionally by a unit matching one of the supported suffixes.

        Examples:

         "12KB" -- parses as "12 kilobytes"
         "5MB"  -- parses as "5 megabytes"
         "20"   -- parses as "20 kilobytes" (where the  defaultUnit is DataUnit.KILOBYTES)
         
        Parameters:
        text - the text to parse
        Returns:
        the parsed DataSize
      • isNegative

        public boolean isNegative()
        Checks if this size is negative, excluding zero.
        Returns:
        true if this size has a size less than zero bytes
      • toBytes

        public long toBytes()
        Return the number of bytes in this instance.
        Returns:
        the number of bytes
      • toKilobytes

        public long toKilobytes()
        Return the number of kilobytes in this instance.
        Returns:
        the number of kilobytes
      • toMegabytes

        public long toMegabytes()
        Return the number of megabytes in this instance.
        Returns:
        the number of megabytes
      • toGigabytes

        public long toGigabytes()
        Return the number of gigabytes in this instance.
        Returns:
        the number of gigabytes
      • toTerabytes

        public long toTerabytes()
        Return the number of terabytes in this instance.
        Returns:
        the number of terabytes
      • compareTo

        public int compareTo(DataSize other)
        Specified by:
        compareTo in interface java.lang.Comparable<DataSize>
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • equals

        public boolean equals(java.lang.Object other)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object