From 716b4738cd3f5e95050fcd8898f40a0441999fbf Mon Sep 17 00:00:00 2001 From: brinn <brinn> Date: Mon, 23 Jun 2008 15:06:37 +0000 Subject: [PATCH] [LMS-460] change r6673 such that the library writes out TRUE and FALSE but accepts all capitalizations when reading SVN: 6774 --- .../java/ch/systemsx/cisd/bds/Utilities.java | 35 +++++++++++++++---- .../cisd/bds/hcs/FormatParameterFactory.java | 4 +-- .../ch/systemsx/cisd/bds/UtilitiesTest.java | 29 ++++++++++++++- 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/bds/source/java/ch/systemsx/cisd/bds/Utilities.java b/bds/source/java/ch/systemsx/cisd/bds/Utilities.java index 51e8822f802..305fc25175b 100644 --- a/bds/source/java/ch/systemsx/cisd/bds/Utilities.java +++ b/bds/source/java/ch/systemsx/cisd/bds/Utilities.java @@ -141,6 +141,8 @@ public class Utilities /** * Return the string content of a file from the given <var>directory</var> as boolean (<code>TRUE</code> * or <code>FALSE</code>). + * + * @throws DataStructureException If the value of <var>name</var> is not a boolean. */ public static Boolean getBoolean(final IDirectory directory, final String name) throws DataStructureException @@ -149,7 +151,7 @@ public class Utilities final String value = getTrimmedString(directory, name); try { - return Boolean.valueOf(value); + return Boolean.fromString(value); } catch (final IllegalArgumentException ex) { throw new DataStructureException("Value of '" + name @@ -239,23 +241,44 @@ public class Utilities // /** - * A boolean object that only accepts <code>TRUE</code> or <code>FALSE</code> as value - * (case-sensitive). + * A boolean object that uses <code>TRUE</code> and <code>FALSE</code> as text + * representation but accepts also <code>true</code> and <code>false</code> when converting + * from strings. */ public static enum Boolean { TRUE, FALSE; /** Converts this object to corresponding <code>boolean</code>. */ - public final boolean toBoolean() + public boolean toBoolean() { - return this == TRUE ? true : false; + return (this == TRUE) ? true : false; } /** Converts given <code>boolean</code> to this enumeration item. */ - public final static Boolean fromBoolean(final boolean bool) + public static Boolean fromBoolean(final boolean bool) { return bool ? TRUE : FALSE; } + + /** + * Converts a string value to a {@link Boolean}. Accepts either <code>true</code>, + * <code>TRUE</code>, <code>false</code> or <code>FALSE</code>. + * + * @throws IllegalArgumentException if <var>value</var> is not one of the values listed + * above. + */ + public static Boolean fromString(final String value) + { + if ("true".equalsIgnoreCase(value)) + { + return TRUE; + } + if ("false".equalsIgnoreCase(value)) + { + return FALSE; + } + throw new IllegalArgumentException("" + value); + } } } \ No newline at end of file diff --git a/bds/source/java/ch/systemsx/cisd/bds/hcs/FormatParameterFactory.java b/bds/source/java/ch/systemsx/cisd/bds/hcs/FormatParameterFactory.java index d3641d1b086..9e8d1144eab 100644 --- a/bds/source/java/ch/systemsx/cisd/bds/hcs/FormatParameterFactory.java +++ b/bds/source/java/ch/systemsx/cisd/bds/hcs/FormatParameterFactory.java @@ -78,7 +78,7 @@ public final class FormatParameterFactory implements IFormatParameterFactory } else if (nodeName.equals(HCSImageFormatV1_0.CONTAINS_ORIGINAL_DATA)) { return new FormatParameter(HCSImageFormatV1_0.CONTAINS_ORIGINAL_DATA, Utilities.Boolean - .valueOf(value)); + .fromString(value)); } return formatParameter; } @@ -104,7 +104,7 @@ public final class FormatParameterFactory implements IFormatParameterFactory return new FormatParameter(name, Integer.parseInt(value)); } else if (name.equals(HCSImageFormatV1_0.CONTAINS_ORIGINAL_DATA)) { - return new FormatParameter(name, Utilities.Boolean.valueOf(value)); + return new FormatParameter(name, Utilities.Boolean.fromString(value)); } return IFormatParameterFactory.DEFAULT_FORMAT_PARAMETER_FACTORY.createFormatParameter(name, value); diff --git a/bds/sourceTest/java/ch/systemsx/cisd/bds/UtilitiesTest.java b/bds/sourceTest/java/ch/systemsx/cisd/bds/UtilitiesTest.java index 34670ad1872..7cd9cdbccf3 100644 --- a/bds/sourceTest/java/ch/systemsx/cisd/bds/UtilitiesTest.java +++ b/bds/sourceTest/java/ch/systemsx/cisd/bds/UtilitiesTest.java @@ -138,7 +138,7 @@ public class UtilitiesTest extends AbstractFileSystemTestCase assertEquals(key, listFiles[0].getName()); assertEquals(value, file.getStringContent().trim()); assertTrue(Utilities.getBoolean(directory, key).toBoolean()); - directory.addKeyValuePair(key, "true"); + directory.addKeyValuePair(key, "yes"); try { Utilities.getBoolean(directory, key); @@ -151,4 +151,31 @@ public class UtilitiesTest extends AbstractFileSystemTestCase assertFalse(Utilities.getBoolean(directory, key).toBoolean()); } + @Test + public void testBooleanToString() + { + assertEquals("TRUE", Utilities.Boolean.TRUE.toString()); + assertEquals("FALSE", Utilities.Boolean.FALSE.toString()); + } + @Test + public void testBooleanFromString() + { + assertEquals(Utilities.Boolean.TRUE, Utilities.Boolean.fromString("TRUE")); + assertEquals(Utilities.Boolean.TRUE, Utilities.Boolean.fromString("true")); + assertEquals(Utilities.Boolean.FALSE, Utilities.Boolean.fromString("FALSE")); + assertEquals(Utilities.Boolean.FALSE, Utilities.Boolean.fromString("false")); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testBooleanFromStringError() + { + Utilities.Boolean.fromString("yes"); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testBooleanFromStringNull() + { + Utilities.Boolean.fromString(null); + } + } \ No newline at end of file -- GitLab