1
1
/*
2
- * Copyright 2002-2020 the original author or authors.
2
+ * Copyright 2002-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
52
52
@ SuppressWarnings ("serial" )
53
53
public final class DataSize implements Comparable <DataSize >, Serializable {
54
54
55
- /**
56
- * The pattern for parsing.
57
- */
58
- private static final Pattern PATTERN = Pattern .compile ("^([+\\ -]?\\ d+)([a-zA-Z]{0,2})$" );
59
-
60
55
/**
61
56
* Bytes per Kilobyte.
62
57
*/
@@ -179,9 +174,9 @@ public static DataSize parse(CharSequence text) {
179
174
public static DataSize parse (CharSequence text , @ Nullable DataUnit defaultUnit ) {
180
175
Assert .notNull (text , "Text must not be null" );
181
176
try {
182
- Matcher matcher = PATTERN .matcher (text );
177
+ Matcher matcher = DataSizeUtils . PATTERN .matcher (text );
183
178
Assert .state (matcher .matches (), "Does not match data size pattern" );
184
- DataUnit unit = determineDataUnit (matcher .group (2 ), defaultUnit );
179
+ DataUnit unit = DataSizeUtils . determineDataUnit (matcher .group (2 ), defaultUnit );
185
180
long amount = Long .parseLong (matcher .group (1 ));
186
181
return DataSize .of (amount , unit );
187
182
}
@@ -190,11 +185,6 @@ public static DataSize parse(CharSequence text, @Nullable DataUnit defaultUnit)
190
185
}
191
186
}
192
187
193
- private static DataUnit determineDataUnit (String suffix , @ Nullable DataUnit defaultUnit ) {
194
- DataUnit defaultUnitToUse = (defaultUnit != null ? defaultUnit : DataUnit .BYTES );
195
- return (StringUtils .hasLength (suffix ) ? DataUnit .fromSuffix (suffix ) : defaultUnitToUse );
196
- }
197
-
198
188
/**
199
189
* Checks if this size is negative, excluding zero.
200
190
* @return true if this size has a size less than zero bytes
@@ -271,4 +261,23 @@ public int hashCode() {
271
261
return Long .hashCode (this .bytes );
272
262
}
273
263
264
+
265
+ /**
266
+ * Static nested class to support lazy loading of the {@link #PATTERN}.
267
+ * @since 5.3.21
268
+ */
269
+ private static class DataSizeUtils {
270
+
271
+ /**
272
+ * The pattern for parsing.
273
+ */
274
+ private static final Pattern PATTERN = Pattern .compile ("^([+\\ -]?\\ d+)([a-zA-Z]{0,2})$" );
275
+
276
+ private static DataUnit determineDataUnit (String suffix , @ Nullable DataUnit defaultUnit ) {
277
+ DataUnit defaultUnitToUse = (defaultUnit != null ? defaultUnit : DataUnit .BYTES );
278
+ return (StringUtils .hasLength (suffix ) ? DataUnit .fromSuffix (suffix ) : defaultUnitToUse );
279
+ }
280
+
281
+ }
282
+
274
283
}
0 commit comments