Skip to content

Commit e276737

Browse files
committed
Lazily initialize DataSize.PATTERN
To avoid unnecessary eager initialization of DataSize.PATTERN, this commit initializes it lazily in the first invocation of DataSize.parse by moving PATTERN to a private static nested class. Closes gh-28560
1 parent aab9da0 commit e276737

File tree

1 file changed

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

1 file changed

+22
-13
lines changed

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,11 +52,6 @@
5252
@SuppressWarnings("serial")
5353
public final class DataSize implements Comparable<DataSize>, Serializable {
5454

55-
/**
56-
* The pattern for parsing.
57-
*/
58-
private static final Pattern PATTERN = Pattern.compile("^([+\\-]?\\d+)([a-zA-Z]{0,2})$");
59-
6055
/**
6156
* Bytes per Kilobyte.
6257
*/
@@ -179,9 +174,9 @@ public static DataSize parse(CharSequence text) {
179174
public static DataSize parse(CharSequence text, @Nullable DataUnit defaultUnit) {
180175
Assert.notNull(text, "Text must not be null");
181176
try {
182-
Matcher matcher = PATTERN.matcher(text);
177+
Matcher matcher = DataSizeUtils.PATTERN.matcher(text);
183178
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);
185180
long amount = Long.parseLong(matcher.group(1));
186181
return DataSize.of(amount, unit);
187182
}
@@ -190,11 +185,6 @@ public static DataSize parse(CharSequence text, @Nullable DataUnit defaultUnit)
190185
}
191186
}
192187

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-
198188
/**
199189
* Checks if this size is negative, excluding zero.
200190
* @return true if this size has a size less than zero bytes
@@ -271,4 +261,23 @@ public int hashCode() {
271261
return Long.hashCode(this.bytes);
272262
}
273263

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+
274283
}

0 commit comments

Comments
 (0)