From 8e8ccba7208fee6eef3cf7e41736a16a6bab230d Mon Sep 17 00:00:00 2001 From: ribeaudc <ribeaudc> Date: Thu, 6 Dec 2007 16:17:06 +0000 Subject: [PATCH] add: - Method 'createXXXFromString' added. - Unit test for 'Format'. SVN: 2978 --- .../java/ch/systemsx/cisd/bds/Format.java | 37 +++++++++++++++++ .../java/ch/systemsx/cisd/bds/Version.java | 24 +++++++++++ .../java/ch/systemsx/cisd/bds/FormatTest.java | 40 +++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 bds/sourceTest/java/ch/systemsx/cisd/bds/FormatTest.java diff --git a/bds/source/java/ch/systemsx/cisd/bds/Format.java b/bds/source/java/ch/systemsx/cisd/bds/Format.java index fd5d21f9686..f85493a6734 100644 --- a/bds/source/java/ch/systemsx/cisd/bds/Format.java +++ b/bds/source/java/ch/systemsx/cisd/bds/Format.java @@ -76,6 +76,43 @@ public class Format implements IStorable return format; } + /** + * Creates a <code>Format</code> from given <var>value</var>. + * + * @param value an example: <code>UNKNOWN [A] V1.2</code>. + * @return <code>null</code> if operation fails. + */ + public static final Format createFormatFromString(final String value) + { + assert value != null : "Format string is not expected to be null."; + int index = value.lastIndexOf('V'); + if (index > -1) + { + final Version version = Version.createVersionFromString(value.substring(index + 1)); + if (version != null) + { + String firstPart = value.substring(0, index).trim(); + String variant = null; + if (firstPart.endsWith("]")) + { + index = firstPart.indexOf('['); + if (index > -1) + { + variant = firstPart.substring(index + 1, firstPart.length() - 1); + firstPart = firstPart.substring(0, index).trim(); + } + } + Format format = FormatStore.getFormat(firstPart, version, variant); + if (format == null) + { + format = new Format(firstPart, version, variant); + } + return format; + } + } + return null; + } + private final String code; private final Version version; diff --git a/bds/source/java/ch/systemsx/cisd/bds/Version.java b/bds/source/java/ch/systemsx/cisd/bds/Version.java index 73322214091..80c19700b43 100644 --- a/bds/source/java/ch/systemsx/cisd/bds/Version.java +++ b/bds/source/java/ch/systemsx/cisd/bds/Version.java @@ -41,6 +41,30 @@ public final class Version implements IStorable return new Version(Utilities.getNumber(versionFolder, MAJOR), Utilities.getNumber(versionFolder, MINOR)); } + /** + * Creates a <code>Version</code> from given <var>value</var>. + * + * @param value an example: <code>2.3</code>. + * @return <code>null</code> if operation fails. + */ + public final static Version createVersionFromString(final String value) + { + assert value != null : "Given value can not be null."; + final int index = value.indexOf("."); + if (index > -1) + { + final String strMajor = value.substring(0, index); + final String strMinor = value.substring(index + 1); + try + { + return new Version(Integer.parseInt(strMajor), Integer.parseInt(strMinor)); + } catch (NumberFormatException ex) + { + } + } + return null; + } + private final int major; private final int minor; diff --git a/bds/sourceTest/java/ch/systemsx/cisd/bds/FormatTest.java b/bds/sourceTest/java/ch/systemsx/cisd/bds/FormatTest.java new file mode 100644 index 00000000000..4125fae934a --- /dev/null +++ b/bds/sourceTest/java/ch/systemsx/cisd/bds/FormatTest.java @@ -0,0 +1,40 @@ +/* + * Copyright 2007 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.bds; + +import org.testng.annotations.Test; +import static org.testng.AssertJUnit.*; + +/** + * Test cases for corresponding {@link Format} class. + * + * @author Christian Ribeaud + */ +public final class FormatTest +{ + + @Test + public final void testCreateFormatFromString() + { + final Format format = Format.createFormatFromString("UNKNOWN [A] V1.2"); + final Version version = format.getVersion(); + assertEquals(1, version.getMajor()); + assertEquals(2, version.getMinor()); + assertEquals("A", format.getVariant()); + assertEquals("UNKNOWN", format.getCode()); + } +} -- GitLab