org.apache.wicket.util.lang
Class Bytes

java.lang.Object
  extended by org.apache.wicket.util.value.LongValue
      extended by org.apache.wicket.util.lang.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):

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

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:

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:

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 Summary
static Bytes MAX
          Maximum bytes value
 
Fields inherited from class org.apache.wicket.util.value.LongValue
value
 
Method Summary
 long bytes()
          Gets the byte count represented by this value object.
static Bytes bytes(double bytes)
          Instantiate immutable Bytes value object..
static Bytes bytes(long bytes)
          Instantiate immutable Bytes value object..
 double gigabytes()
          Gets the byte count in gigabytes.
static Bytes gigabytes(double gigabytes)
          Instantiate immutable Bytes value object..
static Bytes gigabytes(long gigabytes)
          Instantiate immutable Bytes value object..
 double kilobytes()
          Gets the byte count in kilobytes.
static Bytes kilobytes(double kilobytes)
          Instantiate immutable Bytes value object..
static Bytes kilobytes(long kilobytes)
          Instantiate immutable Bytes value object..
 double megabytes()
          Gets the byte count in megabytes.
static Bytes megabytes(double megabytes)
          Instantiate immutable Bytes value object..
static Bytes megabytes(long megabytes)
          Instantiate immutable Bytes value object..
 double terabytes()
          Gets the byte count in terabytes.
static Bytes terabytes(double terabytes)
          Instantiate immutable Bytes value object..
static Bytes terabytes(long terabytes)
          Instantiate immutable Bytes value object..
 String toString()
          Converts this byte count to a string using the default locale.
 String toString(Locale locale)
          Converts this byte count to a string using the given locale.
static Bytes valueOf(String string)
          Converts a string to a number of bytes.
static Bytes valueOf(String string, Locale locale)
          Converts a string to a number of bytes.
 
Methods inherited from class org.apache.wicket.util.value.LongValue
compareTo, equals, greaterThan, greaterThan, hashCode, lessThan, lessThan
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

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


Copyright © 2004-2011 Apache Software Foundation. All Rights Reserved.