From 598c5d045fc950b189251b966482f3d62d70ea09 Mon Sep 17 00:00:00 2001 From: Havoc Pennington <hp@pobox.com> Date: Tue, 15 Nov 2011 16:31:37 -0500 Subject: [PATCH] support terabytes when parsing a memory size --- src/main/java/com/typesafe/config/Config.java | 6 ++++-- .../com/typesafe/config/impl/UnitParserTest.scala | 10 +++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/typesafe/config/Config.java b/src/main/java/com/typesafe/config/Config.java index de1d48e3..3ecc0578 100644 --- a/src/main/java/com/typesafe/config/Config.java +++ b/src/main/java/com/typesafe/config/Config.java @@ -235,7 +235,7 @@ public final class Config { private static enum MemoryUnit { BYTES(1), KILOBYTES(1024), MEGABYTES(1024 * 1024), GIGABYTES( - 1024 * 1024 * 1024); + 1024 * 1024 * 1024), TERABYTES(1024 * 1024 * 1024 * 1024); int bytes; MemoryUnit(int bytes) { @@ -293,10 +293,12 @@ public final class Config { units = MemoryUnit.MEGABYTES; } else if (unitStringLower.equals("g") || unitString.equals("gigabyte")) { units = MemoryUnit.GIGABYTES; + } else if (unitStringLower.equals("t") || unitString.equals("terabyte")) { + units = MemoryUnit.TERABYTES; } else { throw new ConfigException.BadValue(originForException, pathForException, "Could not parse size unit '" - + unitStringMaybePlural + "' (try b, k, m, g)"); + + unitStringMaybePlural + "' (try b, k, m, g, t)"); } try { diff --git a/src/test/scala/com/typesafe/config/impl/UnitParserTest.scala b/src/test/scala/com/typesafe/config/impl/UnitParserTest.scala index d4f4744f..51d0af61 100644 --- a/src/test/scala/com/typesafe/config/impl/UnitParserTest.scala +++ b/src/test/scala/com/typesafe/config/impl/UnitParserTest.scala @@ -46,11 +46,19 @@ class UnitParserTest extends TestUtils { "1024k", "1024K", "1024 kilobytes", "1024 kilobyte", "1m", "1M", "1 M", "1 megabytes", "1 megabyte", "0.0009765625g", "0.0009765625G", "0.0009765625 gigabytes", "0.0009765625 gigabyte") + + def parseMem(s: String) = Config.parseMemorySize(s, fakeOrigin(), "test") + for (s <- oneMegs) { - val result = Config.parseMemorySize(s, fakeOrigin(), "test") + val result = parseMem(s) assertEquals(1024 * 1024, result) } + assertEquals(1024 * 1024 * 1024 * 1024, parseMem("1t")) + assertEquals(1024 * 1024 * 1024 * 1024, parseMem(" 1 T ")) + assertEquals(1024 * 1024 * 1024 * 1024, parseMem("1 terabyte")) + assertEquals(1024 * 1024 * 1024 * 1024, parseMem(" 1 terabytes ")) + // bad units val e = intercept[ConfigException.BadValue] { Config.parseMemorySize("100 dollars", fakeOrigin(), "test")