From be5b8333d32e6efc02a73f4a83fbf10f1c5a9018 Mon Sep 17 00:00:00 2001 From: wisberg Date: Tue, 17 May 2005 00:03:14 +0000 Subject: parseBoolean utility --- .../src/org/aspectj/testing/util/TestUtil.java | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'testing-util/src') diff --git a/testing-util/src/org/aspectj/testing/util/TestUtil.java b/testing-util/src/org/aspectj/testing/util/TestUtil.java index 4b62f9a90..3095b6e2d 100644 --- a/testing-util/src/org/aspectj/testing/util/TestUtil.java +++ b/testing-util/src/org/aspectj/testing/util/TestUtil.java @@ -179,6 +179,52 @@ public final class TestUtil { return path.toString(); } + /** + * @param s the String to parse for [on|off|true|false] + * @throws IllegalArgumentException if input is bad + **/ + public static boolean parseBoolean(String input) { + return parseBoolean(input, true); + } + + /** + * @param s the String to parse for [on|off|true|false] + * @param iaxOnError if true and input is bad, throw + * IllegalArgumentException + * @return true if input is true, false otherwise + * @throws IllegalArgumentException if iaxOnError and input is bad + */ + public static boolean parseBoolean(final String input, boolean iaxOnError) { + final String syntax = ": [on|off|true|false]"; + if (null == input) { + return false; + } + String lc = input.trim().toLowerCase(); + boolean result = false; + boolean valid = false; + switch (lc.length()) { + case 2: + if (valid = "on".equals(lc)) { + result = true; + } + break; + case 3: + valid = "off".equals(lc); + break; + case 4: + if (valid = "true".equals(lc)) { + result = true; + } + break; + case 5: + valid = "false".equals(lc); + break; + } + if (iaxOnError && !valid) { + throw new IllegalArgumentException(input + syntax); + } + return result; + } public static File aspectjrtJarFile() { return (File) LIB_ENTRIES.get(ASPECTJRT_KEY + ".file"); -- cgit v1.2.3