Class Bytes

  • All Implemented Interfaces:
    Serializable, Comparable<LongValue>

    public final class Bytes
    extends LongValue
    Represents an immutable byte count. These static factory methods allow easy construction of value objects using either long values like bytes(2034) or megabytes(3):

    • Bytes.bytes(long)
    • Bytes.kilobytes(long)
    • Bytes.megabytes(long)
    • Bytes.gigabytes(long)
    • Bytes.terabytes(long)

    or double precision floating point values like megabytes(3.2):

    • Bytes.bytes(double)
    • Bytes.kilobytes(double)
    • Bytes.megabytes(double)
    • Bytes.gigabytes(double)
    • Bytes.terabytes(double)

    In the case of bytes(double), the value will be rounded off to the nearest integer byte count using Math.round().

    The precise number of bytes in a Bytes object can be retrieved by calling bytes(). Approximate values for different units can be retrieved as double precision values using these methods:

    • kilobytes()
    • megabytes()
    • gigabytes()
    • terabytes()

    Also, value objects can be constructed from strings, optionally using a Locale with valueOf(String) and valueOf(String,Locale). The string may contain a decimal or floating point number followed by optional whitespace followed by a unit (nothing for bytes, K for kilobyte, M for megabytes, G for gigabytes or T for terabytes) optionally followed by a B (for bytes). Any of these letters can be any case. So, examples of permissible string values are:

    • 37 (37 bytes)
    • 2.3K (2.3 kilobytes)
    • 2.5 kb (2.5 kilobytes)
    • 4k (4 kilobytes)
    • 35.2GB (35.2 gigabytes)
    • 1024M (1024 megabytes)

    Note that if the Locale was not US, the values might substitute "," for "." as that is the custom in Euroland.

    The toString() and toString(Locale) methods are smart enough to convert a given value object to the most appropriate units for the given value.

    Author:
    Jonathan Locke
    See Also:
    Serialized Form
    • Field Detail

      • MAX

        public static final Bytes MAX
        Maximum bytes value
    • Method Detail

      • bytes

        public static Bytes bytes​(long bytes)
        Instantiate immutable Bytes value object..
        Parameters:
        bytes - Value to convert
        Returns:
        Input as Bytes
      • kilobytes

        public static Bytes kilobytes​(long kilobytes)
        Instantiate immutable Bytes value object..
        Parameters:
        kilobytes - Value to convert
        Returns:
        Input as Bytes
      • megabytes

        public static Bytes megabytes​(long megabytes)
        Instantiate immutable Bytes value object..
        Parameters:
        megabytes - Value to convert
        Returns:
        Input as Bytes
      • gigabytes

        public static Bytes gigabytes​(long gigabytes)
        Instantiate immutable Bytes value object..
        Parameters:
        gigabytes - Value to convert
        Returns:
        Input as Bytes
      • terabytes

        public static Bytes terabytes​(long terabytes)
        Instantiate immutable Bytes value object..
        Parameters:
        terabytes - Value to convert
        Returns:
        Input as Bytes
      • bytes

        public static Bytes bytes​(double bytes)
        Instantiate immutable Bytes value object..
        Parameters:
        bytes - Value to convert
        Returns:
        Input as Bytes
      • kilobytes

        public static Bytes kilobytes​(double kilobytes)
        Instantiate immutable Bytes value object..
        Parameters:
        kilobytes - Value to convert
        Returns:
        Input as Bytes
      • megabytes

        public static Bytes megabytes​(double megabytes)
        Instantiate immutable Bytes value object..
        Parameters:
        megabytes - Value to convert
        Returns:
        Input as Bytes
      • gigabytes

        public static Bytes gigabytes​(double gigabytes)
        Instantiate immutable Bytes value object..
        Parameters:
        gigabytes - Value to convert
        Returns:
        Input as Bytes
      • terabytes

        public static Bytes terabytes​(double terabytes)
        Instantiate immutable Bytes value object..
        Parameters:
        terabytes - Value to convert
        Returns:
        Input as Bytes
      • bytes

        public final long bytes()
        Gets the byte count represented by this value object.
        Returns:
        Byte count
      • kilobytes

        public final double kilobytes()
        Gets the byte count in kilobytes.
        Returns:
        The value in kilobytes
      • megabytes

        public final double megabytes()
        Gets the byte count in megabytes.
        Returns:
        The value in megabytes
      • gigabytes

        public final double gigabytes()
        Gets the byte count in gigabytes.
        Returns:
        The value in gigabytes
      • terabytes

        public final double terabytes()
        Gets the byte count in terabytes.
        Returns:
        The value in terabytes
      • valueOf

        public static Bytes valueOf​(String string,
                                    Locale locale)
                             throws StringValueConversionException
        Converts a string to a number of bytes. Strings consist of a floating point value followed by K, M, G or T for kilobytes, megabytes, gigabytes or terabytes, respectively. The abbreviations KB, MB, GB and TB are also accepted. Matching is case insensitive.
        Parameters:
        string - The string to convert
        locale - The Locale to be used for transformation
        Returns:
        The Bytes value for the string
        Throws:
        StringValueConversionException
      • valueOf

        public static Bytes valueOf​(String string)
                             throws StringValueConversionException
        Converts a string to a number of bytes. Strings consist of a floating point value followed by K, M, G or T for kilobytes, megabytes, gigabytes or terabytes, respectively. The abbreviations KB, MB, GB and TB are also accepted. Matching is case insensitive.
        Parameters:
        string - The string to convert
        Returns:
        The Bytes value for the string
        Throws:
        StringValueConversionException
      • toString

        public String toString()
        Converts this byte count to a string using the default locale.
        Overrides:
        toString in class LongValue
        Returns:
        The string for this byte count
      • toString

        public String toString​(Locale locale)
        Converts this byte count to a string using the given locale.
        Parameters:
        locale - Locale to use for conversion
        Returns:
        The string for this byte count
      • greaterThan

        public boolean greaterThan​(Bytes other)
        Compares this Bytes with another Bytes instance.
        Parameters:
        other - the Bytes instance to compare with
        Returns:
        true if this Bytes is greater than the given Bytes instance