@@ -84,7 +84,7 @@ private DataSize(long bytes) {
84
84
/**
85
85
* Obtain a {@link DataSize} representing the specified number of bytes.
86
86
* @param bytes the number of bytes, positive or negative
87
- * @return a {@link DataSize}
87
+ * @return a {@code DataSize}
88
88
*/
89
89
public static DataSize ofBytes (long bytes ) {
90
90
return new DataSize (bytes );
@@ -93,7 +93,7 @@ public static DataSize ofBytes(long bytes) {
93
93
/**
94
94
* Obtain a {@link DataSize} representing the specified number of kilobytes.
95
95
* @param kilobytes the number of kilobytes, positive or negative
96
- * @return a {@link DataSize}
96
+ * @return a {@code DataSize}
97
97
*/
98
98
public static DataSize ofKilobytes (long kilobytes ) {
99
99
return new DataSize (Math .multiplyExact (kilobytes , BYTES_PER_KB ));
@@ -102,7 +102,7 @@ public static DataSize ofKilobytes(long kilobytes) {
102
102
/**
103
103
* Obtain a {@link DataSize} representing the specified number of megabytes.
104
104
* @param megabytes the number of megabytes, positive or negative
105
- * @return a {@link DataSize}
105
+ * @return a {@code DataSize}
106
106
*/
107
107
public static DataSize ofMegabytes (long megabytes ) {
108
108
return new DataSize (Math .multiplyExact (megabytes , BYTES_PER_MB ));
@@ -111,7 +111,7 @@ public static DataSize ofMegabytes(long megabytes) {
111
111
/**
112
112
* Obtain a {@link DataSize} representing the specified number of gigabytes.
113
113
* @param gigabytes the number of gigabytes, positive or negative
114
- * @return a {@link DataSize}
114
+ * @return a {@code DataSize}
115
115
*/
116
116
public static DataSize ofGigabytes (long gigabytes ) {
117
117
return new DataSize (Math .multiplyExact (gigabytes , BYTES_PER_GB ));
@@ -120,7 +120,7 @@ public static DataSize ofGigabytes(long gigabytes) {
120
120
/**
121
121
* Obtain a {@link DataSize} representing the specified number of terabytes.
122
122
* @param terabytes the number of terabytes, positive or negative
123
- * @return a {@link DataSize}
123
+ * @return a {@code DataSize}
124
124
*/
125
125
public static DataSize ofTerabytes (long terabytes ) {
126
126
return new DataSize (Math .multiplyExact (terabytes , BYTES_PER_TB ));
@@ -130,7 +130,7 @@ public static DataSize ofTerabytes(long terabytes) {
130
130
* Obtain a {@link DataSize} representing an amount in the specified {@link DataUnit}.
131
131
* @param amount the amount of the size, measured in terms of the unit,
132
132
* positive or negative
133
- * @return a corresponding {@link DataSize}
133
+ * @return a corresponding {@code DataSize}
134
134
*/
135
135
public static DataSize of (long amount , DataUnit unit ) {
136
136
Assert .notNull (unit , "Unit must not be null" );
@@ -140,15 +140,14 @@ public static DataSize of(long amount, DataUnit unit) {
140
140
/**
141
141
* Obtain a {@link DataSize} from a text string such as {@code 12MB} using
142
142
* {@link DataUnit#BYTES} if no unit is specified.
143
- * <p>
144
- * Examples:
143
+ * <p>Examples:
145
144
* <pre>
146
145
* "12KB" -- parses as "12 kilobytes"
147
146
* "5MB" -- parses as "5 megabytes"
148
147
* "20" -- parses as "20 bytes"
149
148
* </pre>
150
149
* @param text the text to parse
151
- * @return the parsed {@link DataSize}
150
+ * @return the parsed {@code DataSize}
152
151
* @see #parse(CharSequence, DataUnit)
153
152
*/
154
153
public static DataSize parse (CharSequence text ) {
@@ -158,25 +157,27 @@ public static DataSize parse(CharSequence text) {
158
157
/**
159
158
* Obtain a {@link DataSize} from a text string such as {@code 12MB} using
160
159
* 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:
166
165
* <pre>
167
166
* "12KB" -- parses as "12 kilobytes"
168
167
* "5MB" -- parses as "5 megabytes"
169
168
* "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})
170
170
* </pre>
171
171
* @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}
173
174
*/
174
175
public static DataSize parse (CharSequence text , @ Nullable DataUnit defaultUnit ) {
175
176
Assert .notNull (text , "Text must not be null" );
176
177
try {
177
178
CharSequence trimmedText = StringUtils .trimAllWhitespace (text );
178
179
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" );
180
181
DataUnit unit = DataSizeUtils .determineDataUnit (matcher .group (2 ), defaultUnit );
181
182
long amount = Long .parseLong (trimmedText , matcher .start (1 ), matcher .end (1 ), 10 );
182
183
return DataSize .of (amount , unit );
@@ -246,15 +247,15 @@ public String toString() {
246
247
247
248
248
249
@ Override
249
- public boolean equals (@ Nullable Object other ) {
250
- if (this == other ) {
250
+ public boolean equals (@ Nullable Object obj ) {
251
+ if (this == obj ) {
251
252
return true ;
252
253
}
253
- if (other == null || getClass () != other .getClass ()) {
254
+ if (obj == null || getClass () != obj .getClass ()) {
254
255
return false ;
255
256
}
256
- DataSize otherSize = (DataSize ) other ;
257
- return (this .bytes == otherSize .bytes );
257
+ DataSize that = (DataSize ) obj ;
258
+ return (this .bytes == that .bytes );
258
259
}
259
260
260
261
@ Override
0 commit comments