diff --git a/bds/source/java/ch/systemsx/cisd/bds/Format.java b/bds/source/java/ch/systemsx/cisd/bds/Format.java index fd5d21f968684d489a1d2c3db9070805eaf23741..f85493a67348bb2639aa157635cca7379c2d793f 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 73322214091b1cf79b55e559ca1ced00524143ab..80c19700b435b3a960c0bac6376b81245cdb4397 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 0000000000000000000000000000000000000000..4125fae934aa60df6025de4bbdda986db85d745a --- /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()); + } +}