From db8fa4d50514867acf5dcd5771db7bc091aae135 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 1 Aug 2023 11:20:40 +0300 Subject: [PATCH] Polish DataSize --- .../springframework/util/unit/DataSize.java | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/unit/DataSize.java b/spring-core/src/main/java/org/springframework/util/unit/DataSize.java index 904ca331cd3..fa91266a444 100644 --- a/spring-core/src/main/java/org/springframework/util/unit/DataSize.java +++ b/spring-core/src/main/java/org/springframework/util/unit/DataSize.java @@ -84,7 +84,7 @@ public final class DataSize implements Comparable, Serializable { /** * Obtain a {@link DataSize} representing the specified number of bytes. * @param bytes the number of bytes, positive or negative - * @return a {@link DataSize} + * @return a {@code DataSize} */ public static DataSize ofBytes(long bytes) { return new DataSize(bytes); @@ -93,7 +93,7 @@ public final class DataSize implements Comparable, Serializable { /** * Obtain a {@link DataSize} representing the specified number of kilobytes. * @param kilobytes the number of kilobytes, positive or negative - * @return a {@link DataSize} + * @return a {@code DataSize} */ public static DataSize ofKilobytes(long kilobytes) { return new DataSize(Math.multiplyExact(kilobytes, BYTES_PER_KB)); @@ -102,7 +102,7 @@ public final class DataSize implements Comparable, Serializable { /** * Obtain a {@link DataSize} representing the specified number of megabytes. * @param megabytes the number of megabytes, positive or negative - * @return a {@link DataSize} + * @return a {@code DataSize} */ public static DataSize ofMegabytes(long megabytes) { return new DataSize(Math.multiplyExact(megabytes, BYTES_PER_MB)); @@ -111,7 +111,7 @@ public final class DataSize implements Comparable, Serializable { /** * Obtain a {@link DataSize} representing the specified number of gigabytes. * @param gigabytes the number of gigabytes, positive or negative - * @return a {@link DataSize} + * @return a {@code DataSize} */ public static DataSize ofGigabytes(long gigabytes) { return new DataSize(Math.multiplyExact(gigabytes, BYTES_PER_GB)); @@ -120,7 +120,7 @@ public final class DataSize implements Comparable, Serializable { /** * Obtain a {@link DataSize} representing the specified number of terabytes. * @param terabytes the number of terabytes, positive or negative - * @return a {@link DataSize} + * @return a {@code DataSize} */ public static DataSize ofTerabytes(long terabytes) { return new DataSize(Math.multiplyExact(terabytes, BYTES_PER_TB)); @@ -130,7 +130,7 @@ public final class DataSize implements Comparable, Serializable { * 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, * positive or negative - * @return a corresponding {@link DataSize} + * @return a corresponding {@code DataSize} */ public static DataSize of(long amount, DataUnit unit) { Assert.notNull(unit, "Unit must not be null"); @@ -140,15 +140,14 @@ public final class DataSize implements Comparable, Serializable { /** * Obtain a {@link DataSize} from a text string such as {@code 12MB} using * {@link DataUnit#BYTES} if no unit is specified. - *

- * Examples: + *

Examples: *

 	 * "12KB" -- parses as "12 kilobytes"
 	 * "5MB"  -- parses as "5 megabytes"
 	 * "20"   -- parses as "20 bytes"
 	 * 
* @param text the text to parse - * @return the parsed {@link DataSize} + * @return the parsed {@code DataSize} * @see #parse(CharSequence, DataUnit) */ public static DataSize parse(CharSequence text) { @@ -158,25 +157,27 @@ public final class DataSize implements Comparable, Serializable { /** * Obtain a {@link DataSize} from a text string such as {@code 12MB} using * the specified default {@link DataUnit} if no unit is specified. - *

- * The string starts with a number followed optionally by a unit matching one of the - * supported {@linkplain DataUnit suffixes}. - *

- * Examples: + *

The string starts with a number followed optionally by a unit matching + * one of the supported {@linkplain DataUnit suffixes}. + *

If neither a unit nor a default {@code DataUnit} is specified, + * {@link DataUnit#BYTES} will be inferred. + *

Examples: *

 	 * "12KB" -- parses as "12 kilobytes"
 	 * "5MB"  -- parses as "5 megabytes"
 	 * "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})
 	 * 
* @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) { Assert.notNull(text, "Text must not be null"); try { CharSequence trimmedText = StringUtils.trimAllWhitespace(text); 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); long amount = Long.parseLong(trimmedText, matcher.start(1), matcher.end(1), 10); return DataSize.of(amount, unit); @@ -246,15 +247,15 @@ public final class DataSize implements Comparable, Serializable { @Override - public boolean equals(@Nullable Object other) { - if (this == other) { + public boolean equals(@Nullable Object obj) { + if (this == obj) { return true; } - if (other == null || getClass() != other.getClass()) { + if (obj == null || getClass() != obj.getClass()) { return false; } - DataSize otherSize = (DataSize) other; - return (this.bytes == otherSize.bytes); + DataSize that = (DataSize) obj; + return (this.bytes == that.bytes); } @Override