Skip to content

Commit db8fa4d

Browse files
committed
Polish DataSize
1 parent 525621c commit db8fa4d

File tree

1 file changed

+22
-21
lines changed
  • spring-core/src/main/java/org/springframework/util/unit

1 file changed

+22
-21
lines changed

spring-core/src/main/java/org/springframework/util/unit/DataSize.java

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private DataSize(long bytes) {
8484
/**
8585
* Obtain a {@link DataSize} representing the specified number of bytes.
8686
* @param bytes the number of bytes, positive or negative
87-
* @return a {@link DataSize}
87+
* @return a {@code DataSize}
8888
*/
8989
public static DataSize ofBytes(long bytes) {
9090
return new DataSize(bytes);
@@ -93,7 +93,7 @@ public static DataSize ofBytes(long bytes) {
9393
/**
9494
* Obtain a {@link DataSize} representing the specified number of kilobytes.
9595
* @param kilobytes the number of kilobytes, positive or negative
96-
* @return a {@link DataSize}
96+
* @return a {@code DataSize}
9797
*/
9898
public static DataSize ofKilobytes(long kilobytes) {
9999
return new DataSize(Math.multiplyExact(kilobytes, BYTES_PER_KB));
@@ -102,7 +102,7 @@ public static DataSize ofKilobytes(long kilobytes) {
102102
/**
103103
* Obtain a {@link DataSize} representing the specified number of megabytes.
104104
* @param megabytes the number of megabytes, positive or negative
105-
* @return a {@link DataSize}
105+
* @return a {@code DataSize}
106106
*/
107107
public static DataSize ofMegabytes(long megabytes) {
108108
return new DataSize(Math.multiplyExact(megabytes, BYTES_PER_MB));
@@ -111,7 +111,7 @@ public static DataSize ofMegabytes(long megabytes) {
111111
/**
112112
* Obtain a {@link DataSize} representing the specified number of gigabytes.
113113
* @param gigabytes the number of gigabytes, positive or negative
114-
* @return a {@link DataSize}
114+
* @return a {@code DataSize}
115115
*/
116116
public static DataSize ofGigabytes(long gigabytes) {
117117
return new DataSize(Math.multiplyExact(gigabytes, BYTES_PER_GB));
@@ -120,7 +120,7 @@ public static DataSize ofGigabytes(long gigabytes) {
120120
/**
121121
* Obtain a {@link DataSize} representing the specified number of terabytes.
122122
* @param terabytes the number of terabytes, positive or negative
123-
* @return a {@link DataSize}
123+
* @return a {@code DataSize}
124124
*/
125125
public static DataSize ofTerabytes(long terabytes) {
126126
return new DataSize(Math.multiplyExact(terabytes, BYTES_PER_TB));
@@ -130,7 +130,7 @@ public static DataSize ofTerabytes(long terabytes) {
130130
* Obtain a {@link DataSize} representing an amount in the specified {@link DataUnit}.
131131
* @param amount the amount of the size, measured in terms of the unit,
132132
* positive or negative
133-
* @return a corresponding {@link DataSize}
133+
* @return a corresponding {@code DataSize}
134134
*/
135135
public static DataSize of(long amount, DataUnit unit) {
136136
Assert.notNull(unit, "Unit must not be null");
@@ -140,15 +140,14 @@ public static DataSize of(long amount, DataUnit unit) {
140140
/**
141141
* Obtain a {@link DataSize} from a text string such as {@code 12MB} using
142142
* {@link DataUnit#BYTES} if no unit is specified.
143-
* <p>
144-
* Examples:
143+
* <p>Examples:
145144
* <pre>
146145
* "12KB" -- parses as "12 kilobytes"
147146
* "5MB" -- parses as "5 megabytes"
148147
* "20" -- parses as "20 bytes"
149148
* </pre>
150149
* @param text the text to parse
151-
* @return the parsed {@link DataSize}
150+
* @return the parsed {@code DataSize}
152151
* @see #parse(CharSequence, DataUnit)
153152
*/
154153
public static DataSize parse(CharSequence text) {
@@ -158,25 +157,27 @@ public static DataSize parse(CharSequence text) {
158157
/**
159158
* Obtain a {@link DataSize} from a text string such as {@code 12MB} using
160159
* the specified default {@link DataUnit} if no unit is specified.
161-
* <p>
162-
* The string starts with a number followed optionally by a unit matching one of the
163-
* supported {@linkplain DataUnit suffixes}.
164-
* <p>
165-
* Examples:
160+
* <p>The string starts with a number followed optionally by a unit matching
161+
* one of the supported {@linkplain DataUnit suffixes}.
162+
* <p>If neither a unit nor a default {@code DataUnit} is specified,
163+
* {@link DataUnit#BYTES} will be inferred.
164+
* <p>Examples:
166165
* <pre>
167166
* "12KB" -- parses as "12 kilobytes"
168167
* "5MB" -- parses as "5 megabytes"
169168
* "20" -- parses as "20 kilobytes" (where the {@code defaultUnit} is {@link DataUnit#KILOBYTES})
169+
* "20" -- parses as "20 bytes" (if the {@code defaultUnit} is {@code null})
170170
* </pre>
171171
* @param text the text to parse
172-
* @return the parsed {@link DataSize}
172+
* @param defaultUnit the default {@code DataUnit} to use
173+
* @return the parsed {@code DataSize}
173174
*/
174175
public static DataSize parse(CharSequence text, @Nullable DataUnit defaultUnit) {
175176
Assert.notNull(text, "Text must not be null");
176177
try {
177178
CharSequence trimmedText = StringUtils.trimAllWhitespace(text);
178179
Matcher matcher = DataSizeUtils.PATTERN.matcher(trimmedText);
179-
Assert.state(matcher.matches(), "Does not match data size pattern");
180+
Assert.state(matcher.matches(), () -> "'" + text + "' does not match data size pattern");
180181
DataUnit unit = DataSizeUtils.determineDataUnit(matcher.group(2), defaultUnit);
181182
long amount = Long.parseLong(trimmedText, matcher.start(1), matcher.end(1), 10);
182183
return DataSize.of(amount, unit);
@@ -246,15 +247,15 @@ public String toString() {
246247

247248

248249
@Override
249-
public boolean equals(@Nullable Object other) {
250-
if (this == other) {
250+
public boolean equals(@Nullable Object obj) {
251+
if (this == obj) {
251252
return true;
252253
}
253-
if (other == null || getClass() != other.getClass()) {
254+
if (obj == null || getClass() != obj.getClass()) {
254255
return false;
255256
}
256-
DataSize otherSize = (DataSize) other;
257-
return (this.bytes == otherSize.bytes);
257+
DataSize that = (DataSize) obj;
258+
return (this.bytes == that.bytes);
258259
}
259260

260261
@Override

0 commit comments

Comments
 (0)