Enum MemoryUnit

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<MemoryUnit>

    public enum MemoryUnit
    extends java.lang.Enum<MemoryUnit>
    Memory unit: byte-based
    Author:
    Alex Snaps
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
      BYTES
      BYTES
      GIGABYTES
      GIGABYTES (1024 MEGABYTES)
      KILOBYTES
      KILOBYTES (1024 BYTES)
      MEGABYTES
      MEGABYTES (1024 KILOBYTES)
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      static MemoryUnit forUnit​(char unit)
      Returns the MemoryUnit instance based on provided char
      char getUnit()
      Retrieves the unit character for the MemoryUnit
      static long parseAmount​(java.lang.String value)
      Parses the amount represented by the string, without caring for the unit
      static long parseSizeInBytes​(java.lang.String value)
      Parses the string for its content, returning the represented value in bytes
      static MemoryUnit parseUnit​(java.lang.String value)
      Parses the unit part of a String, if no unit char available, returns BYTES
      abstract long toBytes​(long amount)
      returns the amount in bytes
      abstract long toGigaBytes​(long amount)
      returns the amount in gigabytes
      abstract long toKiloBytes​(long amount)
      returns the amount in kilobytes
      abstract long toMegaBytes​(long amount)
      returns the amount in megabytes
      java.lang.String toString​(long amount)
      Human readable value, with the added unit character as a suffix
      static MemoryUnit valueOf​(java.lang.String name)
      Returns the enum constant of this type with the specified name.
      static MemoryUnit[] values()
      Returns an array containing the constants of this enum type, in the order they are declared.
      • Methods inherited from class java.lang.Enum

        clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Enum Constant Detail

      • BYTES

        public static final MemoryUnit BYTES
        BYTES
      • KILOBYTES

        public static final MemoryUnit KILOBYTES
        KILOBYTES (1024 BYTES)
      • MEGABYTES

        public static final MemoryUnit MEGABYTES
        MEGABYTES (1024 KILOBYTES)
      • GIGABYTES

        public static final MemoryUnit GIGABYTES
        GIGABYTES (1024 MEGABYTES)
    • Method Detail

      • values

        public static MemoryUnit[] 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 (MemoryUnit c : MemoryUnit.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static MemoryUnit valueOf​(java.lang.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:
        java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
        java.lang.NullPointerException - if the argument is null
      • getUnit

        public char getUnit()
        Retrieves the unit character for the MemoryUnit
        Returns:
        the unit character
      • toBytes

        public abstract long toBytes​(long amount)
        returns the amount in bytes
        Parameters:
        amount - of the unit
        Returns:
        value in bytes
      • toKiloBytes

        public abstract long toKiloBytes​(long amount)
        returns the amount in kilobytes
        Parameters:
        amount - of the unit
        Returns:
        value in kilobytes
      • toMegaBytes

        public abstract long toMegaBytes​(long amount)
        returns the amount in megabytes
        Parameters:
        amount - of the unit
        Returns:
        value in megabytes
      • toGigaBytes

        public abstract long toGigaBytes​(long amount)
        returns the amount in gigabytes
        Parameters:
        amount - of the unit
        Returns:
        value in gigabytes
      • toString

        public java.lang.String toString​(long amount)
        Human readable value, with the added unit character as a suffix
        Parameters:
        amount - the amount to print
        Returns:
        the String representation
      • forUnit

        public static MemoryUnit forUnit​(char unit)
                                  throws java.lang.IllegalArgumentException
        Returns the MemoryUnit instance based on provided char
        Parameters:
        unit - the unit to look for
        Returns:
        the MemoryUnit instance matching the unit
        Throws:
        java.lang.IllegalArgumentException - if no matching MemoryUnit matching the char
      • parseUnit

        public static MemoryUnit parseUnit​(java.lang.String value)
        Parses the unit part of a String, if no unit char available, returns BYTES
        Parameters:
        value - the String representation of an amount of memory
        Returns:
        the MemoryUnit instance, or BYTES if none
      • parseAmount

        public static long parseAmount​(java.lang.String value)
                                throws java.lang.NumberFormatException
        Parses the amount represented by the string, without caring for the unit
        Parameters:
        value - the String representation of an amount of memory
        Returns:
        the amount of mem in the unit represented by the potential unit char
        Throws:
        java.lang.NumberFormatException - if not a number (with potential unit char stripped)
      • parseSizeInBytes

        public static long parseSizeInBytes​(java.lang.String value)
                                     throws java.lang.NumberFormatException,
                                            java.lang.IllegalArgumentException
        Parses the string for its content, returning the represented value in bytes
        Parameters:
        value - the String representation of an amount of memory
        Returns:
        the amount of bytes represented by the string
        Throws:
        java.lang.NumberFormatException - if not a number (with potential unit char stripped)
        java.lang.IllegalArgumentException - if no matching MemoryUnit matching the char