Top Description Inners Fields Constructors Methods
org.springframework.util.unit

public final Class DataSize

extends Object
implements Comparable<DataSize>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.Comparable
Annotations
@SuppressWarnings:serial
Imports
java.io.Serializable, java.util.regex.Matcher, .Pattern, org.springframework.lang.Nullable, org.springframework.util.Assert, .StringUtils

A data size, such as '12MB'.

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

The terms and units used in this class are based on binary prefixes indicating multiplication by powers of 2. Consult the following table and the Javadoc for DataUnit for details.

TermData SizeSize in Bytes
byte1B1
kilobyte1KB1,024
megabyte1MB1,048,576
gigabyte1GB1,073,741,824
terabyte1TB1,099,511,627,776
Authors
Stephane Nicoll, Sam Brannen
Since
5.1
See Also
DataUnit

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
DataSize.DataSizeUtils

Static nested class to support lazy loading of the PATTERN.

Field Summary

Modifier and TypeField and Description
private final long
private static final long
BYTES_PER_GB

Bytes per Gigabyte.

private static final long
BYTES_PER_KB

Bytes per Kilobyte.

private static final long
BYTES_PER_MB

Bytes per Megabyte.

private static final long
BYTES_PER_TB

Bytes per Terabyte.

Constructor Summary

AccessConstructor and Description
private
DataSize(long bytes)

Method Summary

Modifier and TypeMethod and Description
public int
compareTo(DataSize
the object to be compared.
other
)

Implements java.lang.Comparable.compareTo.

Compares this object with the specified object for order.
public boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public boolean

Returns:

true if this size has a size less than zero bytes
isNegative
()

Checks if this size is negative, excluding zero.

public static DataSize

Returns:

a corresponding DataSize
of
(long
the amount of the size, measured in terms of the unit, positive or negative
amount
,
DataUnit unit)

Obtain a DataSize representing an amount in the specified DataUnit.

public static DataSize

Returns:

a DataSize
ofBytes
(long
the number of bytes, positive or negative
bytes
)

Obtain a DataSize representing the specified number of bytes.

public static DataSize

Returns:

a DataSize
ofGigabytes
(long
the number of gigabytes, positive or negative
gigabytes
)

Obtain a DataSize representing the specified number of gigabytes.

public static DataSize

Returns:

a DataSize
ofKilobytes
(long
the number of kilobytes, positive or negative
kilobytes
)

Obtain a DataSize representing the specified number of kilobytes.

public static DataSize

Returns:

a DataSize
ofMegabytes
(long
the number of megabytes, positive or negative
megabytes
)

Obtain a DataSize representing the specified number of megabytes.

public static DataSize

Returns:

a DataSize
ofTerabytes
(long
the number of terabytes, positive or negative
terabytes
)

Obtain a DataSize representing the specified number of terabytes.

public static DataSize

Returns:

the parsed DataSize
parse
(CharSequence
the text to parse
text
)

Obtain a DataSize from a text string such as 12MB using DataUnit#BYTES if no unit is specified.

public static DataSize

Returns:

the parsed DataSize
parse
(CharSequence
the text to parse
text
,
DataUnit
the default DataUnit to use
defaultUnit
)

Obtain a DataSize from a text string such as 12MB using the specified default DataUnit if no unit is specified.

public long

Returns:

the number of bytes
toBytes
()

Return the number of bytes in this instance.

public long

Returns:

the number of gigabytes
toGigabytes
()

Return the number of gigabytes in this instance.

public long

Returns:

the number of kilobytes
toKilobytes
()

Return the number of kilobytes in this instance.

public long

Returns:

the number of megabytes
toMegabytes
()

Return the number of megabytes in this instance.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public long

Returns:

the number of terabytes
toTerabytes
()

Return the number of terabytes in this instance.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

bytesback to summary
private final long bytes
BYTES_PER_GBback to summary
private static final long BYTES_PER_GB

Bytes per Gigabyte.

BYTES_PER_KBback to summary
private static final long BYTES_PER_KB

Bytes per Kilobyte.

BYTES_PER_MBback to summary
private static final long BYTES_PER_MB

Bytes per Megabyte.

BYTES_PER_TBback to summary
private static final long BYTES_PER_TB

Bytes per Terabyte.

Constructor Detail

DataSizeback to summary
private DataSize(long bytes)

Method Detail

compareToback to summary
public int compareTo(DataSize other)

Implements java.lang.Comparable.compareTo.

Doc from java.lang.Comparable.compareTo.

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

The implementor must ensure signum(x.compareTo(y)) == -signum(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception if and only if y.compareTo(x) throws an exception.)

The implementor must also ensure that the relation is transitive: (x.compareTo(y) > 0 && y.compareTo(z) > 0) implies x.compareTo(z) > 0.

Finally, the implementor must ensure that x.compareTo(y)==0 implies that signum(x.compareTo(z)) == signum(y.compareTo(z)), for all z.

Parameters
other:DataSize

the object to be compared.

Returns:int

a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Annotations
@Override
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
isNegativeback to summary
public boolean isNegative()

Checks if this size is negative, excluding zero.

Returns:boolean

true if this size has a size less than zero bytes

ofback to summary
public static DataSize of(long amount, DataUnit unit)

Obtain a DataSize representing an amount in the specified DataUnit.

Parameters
amount:long

the amount of the size, measured in terms of the unit, positive or negative

Returns:DataSize

a corresponding DataSize

ofBytesback to summary
public static DataSize ofBytes(long bytes)

Obtain a DataSize representing the specified number of bytes.

Parameters
bytes:long

the number of bytes, positive or negative

Returns:DataSize

a DataSize

ofGigabytesback to summary
public static DataSize ofGigabytes(long gigabytes)

Obtain a DataSize representing the specified number of gigabytes.

Parameters
gigabytes:long

the number of gigabytes, positive or negative

Returns:DataSize

a DataSize

ofKilobytesback to summary
public static DataSize ofKilobytes(long kilobytes)

Obtain a DataSize representing the specified number of kilobytes.

Parameters
kilobytes:long

the number of kilobytes, positive or negative

Returns:DataSize

a DataSize

ofMegabytesback to summary
public static DataSize ofMegabytes(long megabytes)

Obtain a DataSize representing the specified number of megabytes.

Parameters
megabytes:long

the number of megabytes, positive or negative

Returns:DataSize

a DataSize

ofTerabytesback to summary
public static DataSize ofTerabytes(long terabytes)

Obtain a DataSize representing the specified number of terabytes.

Parameters
terabytes:long

the number of terabytes, positive or negative

Returns:DataSize

a DataSize

parseback to summary
public static DataSize parse(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:CharSequence

the text to parse

Returns:DataSize

the parsed DataSize

See Also
parse(CharSequence, DataUnit)
parseback to summary
public static DataSize parse(CharSequence text, 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.

If neither a unit nor a default DataUnit is specified, DataUnit#BYTES will be inferred.

Examples:

"12KB" -- parses as "12 kilobytes"
"5MB"  -- parses as "5 megabytes"
"20"   -- parses as "20 kilobytes" (where the defaultUnit is DataUnit#KILOBYTES)
"20"   -- parses as "20 bytes" (if the defaultUnit is null)
Parameters
text:CharSequence

the text to parse

defaultUnit:DataUnit

the default DataUnit to use

Returns:DataSize

the parsed DataSize

toBytesback to summary
public long toBytes()

Return the number of bytes in this instance.

Returns:long

the number of bytes

toGigabytesback to summary
public long toGigabytes()

Return the number of gigabytes in this instance.

Returns:long

the number of gigabytes

toKilobytesback to summary
public long toKilobytes()

Return the number of kilobytes in this instance.

Returns:long

the number of kilobytes

toMegabytesback to summary
public long toMegabytes()

Return the number of megabytes in this instance.

Returns:long

the number of megabytes

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
toTerabytesback to summary
public long toTerabytes()

Return the number of terabytes in this instance.

Returns:long

the number of terabytes

org.springframework.util.unit back to summary

private Class DataSize.DataSizeUtils

extends Object
Class Inheritance

Static nested class to support lazy loading of the PATTERN.
Since
5.3.21

Field Summary

Modifier and TypeField and Description
private static final Pattern
PATTERN

The pattern for parsing.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static DataUnit
determineDataUnit(String suffix, DataUnit defaultUnit)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

PATTERNback to summary
private static final Pattern PATTERN

The pattern for parsing.

Constructor Detail

DataSizeUtilsback to summary
private DataSizeUtils()

Method Detail

determineDataUnitback to summary
private static DataUnit determineDataUnit(String suffix, DataUnit defaultUnit)