Skip to content
Snippets Groups Projects
Commit 8e8ccba7 authored by ribeaudc's avatar ribeaudc
Browse files

add:

- Method 'createXXXFromString' added.
- Unit test for 'Format'.

SVN: 2978
parent c05ad3eb
No related branches found
No related tags found
No related merge requests found
......@@ -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;
......
......@@ -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;
......
/*
* 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());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment