This is an automated email from the ASF dual-hosted git repository. rcordier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 748b9c972f1a470e5c39c074def58f2fdf90279d Author: Benoit Tellier <[email protected]> AuthorDate: Wed Jul 7 14:51:00 2021 +0700 JAMES-3610 Allow specify a default unit for Size parsing when none is specified --- .../src/main/java/org/apache/james/util/Size.java | 32 ++++++++++++++++------ .../test/java/org/apache/james/util/SizeTest.java | 10 +++++++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/server/container/util/src/main/java/org/apache/james/util/Size.java b/server/container/util/src/main/java/org/apache/james/util/Size.java index 263fb54..617fb1c 100644 --- a/server/container/util/src/main/java/org/apache/james/util/Size.java +++ b/server/container/util/src/main/java/org/apache/james/util/Size.java @@ -55,6 +55,10 @@ public class Size { } public static Size parse(String providedLongWithUnitString) throws Exception { + return parse(providedLongWithUnitString, Unit.NoUnit); + } + + public static Size parse(String providedLongWithUnitString, Unit defaultUnit) throws Exception { if (providedLongWithUnitString.equalsIgnoreCase(UNKNOWN)) { return new Size(Unit.NoUnit, UNKNOWN_VALUE); } @@ -62,8 +66,8 @@ public class Size { return new Size(Unit.NoUnit, UNLIMITED_VALUE); } char lastChar = providedLongWithUnitString.charAt(providedLongWithUnitString.length() - 1); - Unit unit = getUnit(lastChar); - String argWithoutUnit = removeLastCharIfNeeded(providedLongWithUnitString, unit); + Unit unit = getUnit(lastChar, defaultUnit); + String argWithoutUnit = removeLastCharIfNeeded(providedLongWithUnitString, lastChar); return new Size(unit, Long.parseLong(argWithoutUnit)); } @@ -93,15 +97,25 @@ public class Size { } } - private static String removeLastCharIfNeeded(String providedLongWithUnitString, Unit unit) { - if (unit != Unit.NoUnit) { - return providedLongWithUnitString.substring(0, providedLongWithUnitString.length() - 1); - } else { - return providedLongWithUnitString; + private static String removeLastCharIfNeeded(String providedLongWithUnitString, char lastChar) { + switch (lastChar) { + case '1' : + case '2' : + case '3' : + case '4' : + case '5' : + case '6' : + case '7' : + case '8' : + case '9' : + case '0' : + return providedLongWithUnitString; + default: + return providedLongWithUnitString.substring(0, providedLongWithUnitString.length() - 1); } } - private static Unit getUnit(char lastChar) throws Exception { + private static Unit getUnit(char lastChar, Unit defaultUnit) throws Exception { switch (lastChar) { case 'K' : case 'k' : @@ -125,7 +139,7 @@ public class Size { case '8' : case '9' : case '0' : - return Unit.NoUnit; + return defaultUnit; default: throw new Exception("No unit corresponding to char : " + lastChar); } diff --git a/server/container/util/src/test/java/org/apache/james/util/SizeTest.java b/server/container/util/src/test/java/org/apache/james/util/SizeTest.java index 0392a63..e90b4da 100644 --- a/server/container/util/src/test/java/org/apache/james/util/SizeTest.java +++ b/server/container/util/src/test/java/org/apache/james/util/SizeTest.java @@ -32,6 +32,11 @@ class SizeTest { } @Test + void testNoUnitWithDefaultUnit() throws Exception { + assertThat(Size.parse("10", Size.Unit.K).asBytes()).isEqualTo(10240); + } + + @Test void testUnitB() throws Exception { assertThat(Size.parse("1024B").asBytes()).isEqualTo(1024); } @@ -47,6 +52,11 @@ class SizeTest { } @Test + void testUnitMWithDefaultUnit() throws Exception { + assertThat(Size.parse("5M", Size.Unit.K).asBytes()).isEqualTo(5 * 1024 * 1024); + } + + @Test void testUnitG() throws Exception { assertThat(Size.parse("1G").asBytes()).isEqualTo(1024 * 1024 * 1024); } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
