From e2767371b569eee237882f913916985e30374316 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 3 Jun 2022 13:30:45 +0200 Subject: [PATCH] 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 --- .../springframework/util/unit/DataSize.java | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 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 976ee1bc8d49..75183a18e05c 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -52,11 +52,6 @@ @SuppressWarnings("serial") public final class DataSize implements Comparable, Serializable { - /** - * The pattern for parsing. - */ - private static final Pattern PATTERN = Pattern.compile("^([+\\-]?\\d+)([a-zA-Z]{0,2})$"); - /** * Bytes per Kilobyte. */ @@ -179,9 +174,9 @@ public static DataSize parse(CharSequence text) { public static DataSize parse(CharSequence text, @Nullable DataUnit defaultUnit) { Assert.notNull(text, "Text must not be null"); try { - Matcher matcher = PATTERN.matcher(text); + Matcher matcher = DataSizeUtils.PATTERN.matcher(text); Assert.state(matcher.matches(), "Does not match data size pattern"); - DataUnit unit = determineDataUnit(matcher.group(2), defaultUnit); + DataUnit unit = DataSizeUtils.determineDataUnit(matcher.group(2), defaultUnit); long amount = Long.parseLong(matcher.group(1)); return DataSize.of(amount, unit); } @@ -190,11 +185,6 @@ public static DataSize parse(CharSequence text, @Nullable DataUnit defaultUnit) } } - private static DataUnit determineDataUnit(String suffix, @Nullable DataUnit defaultUnit) { - DataUnit defaultUnitToUse = (defaultUnit != null ? defaultUnit : DataUnit.BYTES); - return (StringUtils.hasLength(suffix) ? DataUnit.fromSuffix(suffix) : defaultUnitToUse); - } - /** * Checks if this size is negative, excluding zero. * @return true if this size has a size less than zero bytes @@ -271,4 +261,23 @@ public int hashCode() { return Long.hashCode(this.bytes); } + + /** + * Static nested class to support lazy loading of the {@link #PATTERN}. + * @since 5.3.21 + */ + private static class DataSizeUtils { + + /** + * The pattern for parsing. + */ + private static final Pattern PATTERN = Pattern.compile("^([+\\-]?\\d+)([a-zA-Z]{0,2})$"); + + private static DataUnit determineDataUnit(String suffix, @Nullable DataUnit defaultUnit) { + DataUnit defaultUnitToUse = (defaultUnit != null ? defaultUnit : DataUnit.BYTES); + return (StringUtils.hasLength(suffix) ? DataUnit.fromSuffix(suffix) : defaultUnitToUse); + } + + } + }