|
|
|
@ -84,7 +84,7 @@ public final class DataSize implements Comparable<DataSize>, Serializable { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Obtain a {@link DataSize} representing the specified number of bytes. |
|
|
|
* Obtain a {@link DataSize} representing the specified number of bytes. |
|
|
|
* @param bytes the number of bytes, positive or negative |
|
|
|
* @param bytes the number of bytes, positive or negative |
|
|
|
* @return a {@link DataSize} |
|
|
|
* @return a {@code DataSize} |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static DataSize ofBytes(long bytes) { |
|
|
|
public static DataSize ofBytes(long bytes) { |
|
|
|
return new DataSize(bytes); |
|
|
|
return new DataSize(bytes); |
|
|
|
@ -93,7 +93,7 @@ public final class DataSize implements Comparable<DataSize>, Serializable { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Obtain a {@link DataSize} representing the specified number of kilobytes. |
|
|
|
* Obtain a {@link DataSize} representing the specified number of kilobytes. |
|
|
|
* @param kilobytes the number of kilobytes, positive or negative |
|
|
|
* @param kilobytes the number of kilobytes, positive or negative |
|
|
|
* @return a {@link DataSize} |
|
|
|
* @return a {@code DataSize} |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static DataSize ofKilobytes(long kilobytes) { |
|
|
|
public static DataSize ofKilobytes(long kilobytes) { |
|
|
|
return new DataSize(Math.multiplyExact(kilobytes, BYTES_PER_KB)); |
|
|
|
return new DataSize(Math.multiplyExact(kilobytes, BYTES_PER_KB)); |
|
|
|
@ -102,7 +102,7 @@ public final class DataSize implements Comparable<DataSize>, Serializable { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Obtain a {@link DataSize} representing the specified number of megabytes. |
|
|
|
* Obtain a {@link DataSize} representing the specified number of megabytes. |
|
|
|
* @param megabytes the number of megabytes, positive or negative |
|
|
|
* @param megabytes the number of megabytes, positive or negative |
|
|
|
* @return a {@link DataSize} |
|
|
|
* @return a {@code DataSize} |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static DataSize ofMegabytes(long megabytes) { |
|
|
|
public static DataSize ofMegabytes(long megabytes) { |
|
|
|
return new DataSize(Math.multiplyExact(megabytes, BYTES_PER_MB)); |
|
|
|
return new DataSize(Math.multiplyExact(megabytes, BYTES_PER_MB)); |
|
|
|
@ -111,7 +111,7 @@ public final class DataSize implements Comparable<DataSize>, Serializable { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Obtain a {@link DataSize} representing the specified number of gigabytes. |
|
|
|
* Obtain a {@link DataSize} representing the specified number of gigabytes. |
|
|
|
* @param gigabytes the number of gigabytes, positive or negative |
|
|
|
* @param gigabytes the number of gigabytes, positive or negative |
|
|
|
* @return a {@link DataSize} |
|
|
|
* @return a {@code DataSize} |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static DataSize ofGigabytes(long gigabytes) { |
|
|
|
public static DataSize ofGigabytes(long gigabytes) { |
|
|
|
return new DataSize(Math.multiplyExact(gigabytes, BYTES_PER_GB)); |
|
|
|
return new DataSize(Math.multiplyExact(gigabytes, BYTES_PER_GB)); |
|
|
|
@ -120,7 +120,7 @@ public final class DataSize implements Comparable<DataSize>, Serializable { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Obtain a {@link DataSize} representing the specified number of terabytes. |
|
|
|
* Obtain a {@link DataSize} representing the specified number of terabytes. |
|
|
|
* @param terabytes the number of terabytes, positive or negative |
|
|
|
* @param terabytes the number of terabytes, positive or negative |
|
|
|
* @return a {@link DataSize} |
|
|
|
* @return a {@code DataSize} |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static DataSize ofTerabytes(long terabytes) { |
|
|
|
public static DataSize ofTerabytes(long terabytes) { |
|
|
|
return new DataSize(Math.multiplyExact(terabytes, BYTES_PER_TB)); |
|
|
|
return new DataSize(Math.multiplyExact(terabytes, BYTES_PER_TB)); |
|
|
|
@ -130,7 +130,7 @@ public final class DataSize implements Comparable<DataSize>, Serializable { |
|
|
|
* Obtain a {@link DataSize} representing an amount in the specified {@link DataUnit}. |
|
|
|
* Obtain a {@link DataSize} representing an amount in the specified {@link DataUnit}. |
|
|
|
* @param amount the amount of the size, measured in terms of the unit, |
|
|
|
* @param amount the amount of the size, measured in terms of the unit, |
|
|
|
* positive or negative |
|
|
|
* positive or negative |
|
|
|
* @return a corresponding {@link DataSize} |
|
|
|
* @return a corresponding {@code DataSize} |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static DataSize of(long amount, DataUnit unit) { |
|
|
|
public static DataSize of(long amount, DataUnit unit) { |
|
|
|
Assert.notNull(unit, "Unit must not be null"); |
|
|
|
Assert.notNull(unit, "Unit must not be null"); |
|
|
|
@ -140,15 +140,14 @@ public final class DataSize implements Comparable<DataSize>, Serializable { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Obtain a {@link DataSize} from a text string such as {@code 12MB} using |
|
|
|
* Obtain a {@link DataSize} from a text string such as {@code 12MB} using |
|
|
|
* {@link DataUnit#BYTES} if no unit is specified. |
|
|
|
* {@link DataUnit#BYTES} if no unit is specified. |
|
|
|
* <p> |
|
|
|
* <p>Examples: |
|
|
|
* Examples: |
|
|
|
|
|
|
|
* <pre> |
|
|
|
* <pre> |
|
|
|
* "12KB" -- parses as "12 kilobytes" |
|
|
|
* "12KB" -- parses as "12 kilobytes" |
|
|
|
* "5MB" -- parses as "5 megabytes" |
|
|
|
* "5MB" -- parses as "5 megabytes" |
|
|
|
* "20" -- parses as "20 bytes" |
|
|
|
* "20" -- parses as "20 bytes" |
|
|
|
* </pre> |
|
|
|
* </pre> |
|
|
|
* @param text the text to parse |
|
|
|
* @param text the text to parse |
|
|
|
* @return the parsed {@link DataSize} |
|
|
|
* @return the parsed {@code DataSize} |
|
|
|
* @see #parse(CharSequence, DataUnit) |
|
|
|
* @see #parse(CharSequence, DataUnit) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static DataSize parse(CharSequence text) { |
|
|
|
public static DataSize parse(CharSequence text) { |
|
|
|
@ -158,25 +157,27 @@ public final class DataSize implements Comparable<DataSize>, Serializable { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Obtain a {@link DataSize} from a text string such as {@code 12MB} using |
|
|
|
* Obtain a {@link DataSize} from a text string such as {@code 12MB} using |
|
|
|
* the specified default {@link DataUnit} if no unit is specified. |
|
|
|
* the specified default {@link DataUnit} if no unit is specified. |
|
|
|
* <p> |
|
|
|
* <p>The string starts with a number followed optionally by a unit matching |
|
|
|
* The string starts with a number followed optionally by a unit matching one of the |
|
|
|
* one of the supported {@linkplain DataUnit suffixes}. |
|
|
|
* supported {@linkplain DataUnit suffixes}. |
|
|
|
* <p>If neither a unit nor a default {@code DataUnit} is specified, |
|
|
|
* <p> |
|
|
|
* {@link DataUnit#BYTES} will be inferred. |
|
|
|
* Examples: |
|
|
|
* <p>Examples: |
|
|
|
* <pre> |
|
|
|
* <pre> |
|
|
|
* "12KB" -- parses as "12 kilobytes" |
|
|
|
* "12KB" -- parses as "12 kilobytes" |
|
|
|
* "5MB" -- parses as "5 megabytes" |
|
|
|
* "5MB" -- parses as "5 megabytes" |
|
|
|
* "20" -- parses as "20 kilobytes" (where the {@code defaultUnit} is {@link DataUnit#KILOBYTES}) |
|
|
|
* "20" -- parses as "20 kilobytes" (where the {@code defaultUnit} is {@link DataUnit#KILOBYTES}) |
|
|
|
|
|
|
|
* "20" -- parses as "20 bytes" (if the {@code defaultUnit} is {@code null}) |
|
|
|
* </pre> |
|
|
|
* </pre> |
|
|
|
* @param text the text to parse |
|
|
|
* @param text the text to parse |
|
|
|
* @return the parsed {@link DataSize} |
|
|
|
* @param defaultUnit the default {@code DataUnit} to use |
|
|
|
|
|
|
|
* @return the parsed {@code DataSize} |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static DataSize parse(CharSequence text, @Nullable DataUnit defaultUnit) { |
|
|
|
public static DataSize parse(CharSequence text, @Nullable DataUnit defaultUnit) { |
|
|
|
Assert.notNull(text, "Text must not be null"); |
|
|
|
Assert.notNull(text, "Text must not be null"); |
|
|
|
try { |
|
|
|
try { |
|
|
|
CharSequence trimmedText = StringUtils.trimAllWhitespace(text); |
|
|
|
CharSequence trimmedText = StringUtils.trimAllWhitespace(text); |
|
|
|
Matcher matcher = DataSizeUtils.PATTERN.matcher(trimmedText); |
|
|
|
Matcher matcher = DataSizeUtils.PATTERN.matcher(trimmedText); |
|
|
|
Assert.state(matcher.matches(), "Does not match data size pattern"); |
|
|
|
Assert.state(matcher.matches(), () -> "'" + text + "' does not match data size pattern"); |
|
|
|
DataUnit unit = DataSizeUtils.determineDataUnit(matcher.group(2), defaultUnit); |
|
|
|
DataUnit unit = DataSizeUtils.determineDataUnit(matcher.group(2), defaultUnit); |
|
|
|
long amount = Long.parseLong(trimmedText, matcher.start(1), matcher.end(1), 10); |
|
|
|
long amount = Long.parseLong(trimmedText, matcher.start(1), matcher.end(1), 10); |
|
|
|
return DataSize.of(amount, unit); |
|
|
|
return DataSize.of(amount, unit); |
|
|
|
@ -246,15 +247,15 @@ public final class DataSize implements Comparable<DataSize>, Serializable { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public boolean equals(@Nullable Object other) { |
|
|
|
public boolean equals(@Nullable Object obj) { |
|
|
|
if (this == other) { |
|
|
|
if (this == obj) { |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
if (other == null || getClass() != other.getClass()) { |
|
|
|
if (obj == null || getClass() != obj.getClass()) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
DataSize otherSize = (DataSize) other; |
|
|
|
DataSize that = (DataSize) obj; |
|
|
|
return (this.bytes == otherSize.bytes); |
|
|
|
return (this.bytes == that.bytes); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
|