org.pfsw.text
Class ByteSizeValue

java.lang.Object
  extended by org.pfsw.text.ByteSizeValue

public class ByteSizeValue
extends java.lang.Object

Represents a number-of-bytes value and provides parsing of strings and various different getters for various byte size units.


Field Summary
static java.math.BigDecimal KB_FACTOR
          The factor 1024.
 
Constructor Summary
protected ByteSizeValue()
          Creates a new instance with the value: 0 bytes.
  ByteSizeValue(java.math.BigDecimal numBytes)
          Initialize the new instance with a value in bytes.
  ByteSizeValue(java.math.BigDecimal value, ByteSizeUnit unit)
          Initialize the new instance with a value and unit.
  ByteSizeValue(long numBytes)
          Initialize the new instance with a value in bytes.
  ByteSizeValue(long value, ByteSizeUnit unit)
          Initialize the new instance with a value and unit.
  ByteSizeValue(java.lang.String strValue)
          Initialize the new instance with a string that must contain digits, an optional decimal point and optionally followed by the prefix name of a unit (ByteSizeUnit).
  ByteSizeValue(java.lang.String strValue, ByteSizeUnit defaultUnit)
          Initialize the new instance with a string that must contain digits, an optional decimal point and optionally followed by the prefix name of a unit (ByteSizeUnit).
 
Method Summary
 java.math.BigDecimal asBytes()
          Returns the value in BYTES.
 java.math.BigDecimal asExaBytes()
          Returns the value in EXA_BYTES.
 java.math.BigDecimal asGigaBytes()
          Returns the value in GIGA_BYTES.
 java.math.BigDecimal asKiloBytes()
          Returns the value in KILO_BYTES.
 java.math.BigDecimal asMegaBytes()
          Returns the value in MEGA_BYTES.
 java.math.BigDecimal asPetaBytes()
          Returns the value in PETA_BYTES.
 java.math.BigDecimal asTeraBytes()
          Returns the value in TERA_BYTES.
 java.math.BigDecimal convertTo(ByteSizeUnit unit)
          Returns the value converted to the specified unit.
 boolean equals(java.lang.Object obj)
           
protected  ByteSizeUnit getCurrentUnit()
           
 long getSizeInBytes()
          Returns the value in BYTES.
protected  java.math.BigDecimal getValue()
           
 int hashCode()
           
protected  void parseAndInit(java.lang.String strValue, ByteSizeUnit defaultUnit)
          Parse the given string, taking any optional unit string at the end into account.
protected  void setCurrentUnit(ByteSizeUnit currentUnit)
           
protected  void setValue(java.math.BigDecimal value)
           
protected  StringPair splitDigitsAndUnit(java.lang.String str)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

KB_FACTOR

public static final java.math.BigDecimal KB_FACTOR
The factor 1024.

Constructor Detail

ByteSizeValue

public ByteSizeValue(java.math.BigDecimal numBytes)
Initialize the new instance with a value in bytes.


ByteSizeValue

public ByteSizeValue(long numBytes)
Initialize the new instance with a value in bytes.


ByteSizeValue

public ByteSizeValue(java.math.BigDecimal value,
                     ByteSizeUnit unit)
Initialize the new instance with a value and unit.


ByteSizeValue

public ByteSizeValue(long value,
                     ByteSizeUnit unit)
Initialize the new instance with a value and unit.


ByteSizeValue

public ByteSizeValue(java.lang.String strValue,
                     ByteSizeUnit defaultUnit)
Initialize the new instance with a string that must contain digits, an optional decimal point and optionally followed by the prefix name of a unit (ByteSizeUnit).
If no explicit unit is set in the string then the given default unit will be used.

Parameters:
strValue - The value with the optional unit definition (must not be null).
defaultUnit - The unit to be used if no explicit unit is defined in the string (must not be null).
Throws:
java.lang.NumberFormatException - If the string does not contain a valid value or unit.

ByteSizeValue

public ByteSizeValue(java.lang.String strValue)
Initialize the new instance with a string that must contain digits, an optional decimal point and optionally followed by the prefix name of a unit (ByteSizeUnit).
The default unit is "B" (ByteSizeUnit.BYTES) if no other unit is explicitly set in the string.

Parameters:
strValue - The value with the optional unit definition (must not be null).
Throws:
java.lang.NumberFormatException - If the string does not contain a valid value or unit.

ByteSizeValue

protected ByteSizeValue()
Creates a new instance with the value: 0 bytes.

Method Detail

getSizeInBytes

public long getSizeInBytes()
Returns the value in BYTES.


asBytes

public java.math.BigDecimal asBytes()
Returns the value in BYTES.


asKiloBytes

public java.math.BigDecimal asKiloBytes()
Returns the value in KILO_BYTES.


asMegaBytes

public java.math.BigDecimal asMegaBytes()
Returns the value in MEGA_BYTES.


asGigaBytes

public java.math.BigDecimal asGigaBytes()
Returns the value in GIGA_BYTES.


asTeraBytes

public java.math.BigDecimal asTeraBytes()
Returns the value in TERA_BYTES.


asPetaBytes

public java.math.BigDecimal asPetaBytes()
Returns the value in PETA_BYTES.


asExaBytes

public java.math.BigDecimal asExaBytes()
Returns the value in EXA_BYTES.


convertTo

public java.math.BigDecimal convertTo(ByteSizeUnit unit)
Returns the value converted to the specified unit.

Parameters:
unit - The unit of the result value (must not be null).

toString

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

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

hashCode

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

parseAndInit

protected void parseAndInit(java.lang.String strValue,
                            ByteSizeUnit defaultUnit)
Parse the given string, taking any optional unit string at the end into account. Allowed unit strings are the short names or IEEE-1541 prefixes defined in ByteSizeUnit (e.g. "B", "KB", "MB", "KiB", "GiB").

Parameters:
strValue - The string to parse (may contain decimal point - except for unit "bytes").
defaultUnit - The unit to use if the string does not contain any explicit short name.
Throws:
java.lang.NumberFormatException - If the string does not contain a valid decimal value or an invalid unit.

splitDigitsAndUnit

protected StringPair splitDigitsAndUnit(java.lang.String str)

getValue

protected java.math.BigDecimal getValue()

setValue

protected void setValue(java.math.BigDecimal value)

getCurrentUnit

protected ByteSizeUnit getCurrentUnit()

setCurrentUnit

protected void setCurrentUnit(ByteSizeUnit currentUnit)