From d089f63a16f55874f0fd22065f71024b9ea5242c Mon Sep 17 00:00:00 2001
From: ribeaudc <ribeaudc>
Date: Wed, 5 Dec 2007 07:37:38 +0000
Subject: [PATCH] [LMS-117] add: - Method 'createFromString(String)'.

SVN: 2925
---
 .../ch/systemsx/cisd/bds/hcs/Geometry.java    | 30 ++++++++++++++++++-
 .../systemsx/cisd/bds/hcs/GeometryTest.java   | 27 +++++++++++++++++
 2 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/bds/source/java/ch/systemsx/cisd/bds/hcs/Geometry.java b/bds/source/java/ch/systemsx/cisd/bds/hcs/Geometry.java
index e9c2fbd4c65..7f8c409349d 100644
--- a/bds/source/java/ch/systemsx/cisd/bds/hcs/Geometry.java
+++ b/bds/source/java/ch/systemsx/cisd/bds/hcs/Geometry.java
@@ -35,6 +35,9 @@ import ch.systemsx.cisd.bds.storage.IDirectory;
  */
 public class Geometry implements IStorable
 {
+    /** The <code>rows</code>-<code>columns</code> separator in the string representation of this object. */
+    private static final String X = "x";
+
     static final String NOT_POSITIVE = "Given geometry component '%s' must be > 0 (%d <= 0).";
 
     final static String ROWS = "rows";
@@ -70,6 +73,31 @@ public class Geometry implements IStorable
         return Integer.toString(number);
     }
 
+    /**
+     * Loads a <code>Geometry</code> from given <var>toString</var>.
+     * 
+     * @param toString the output you get when calling {@link #toString()}.
+     * @return <code>null</code> if operation fails.
+     */
+    public final static Geometry createFromString(final String toString)
+    {
+        assert toString != null : "Given string can not be null.";
+        final int index = toString.indexOf(X);
+        if (index > -1)
+        {
+            try
+            {
+                int rows = Integer.parseInt(toString.substring(0, index));
+                int columns = Integer.parseInt(toString.substring(index + X.length()));
+                return new Geometry(rows, columns);
+            } catch (NumberFormatException ex)
+            {
+                // Nothing to do here.
+            }
+        }
+        return null;
+    }
+
     /**
      * Loads the geometry from the specified directory.
      * 
@@ -147,6 +175,6 @@ public class Geometry implements IStorable
     @Override
     public final String toString()
     {
-        return getRows() + "x" + getColumns();
+        return getRows() + X + getColumns();
     }
 }
\ No newline at end of file
diff --git a/bds/sourceTest/java/ch/systemsx/cisd/bds/hcs/GeometryTest.java b/bds/sourceTest/java/ch/systemsx/cisd/bds/hcs/GeometryTest.java
index 5f440dcf6d5..18f4c6e70fd 100644
--- a/bds/sourceTest/java/ch/systemsx/cisd/bds/hcs/GeometryTest.java
+++ b/bds/sourceTest/java/ch/systemsx/cisd/bds/hcs/GeometryTest.java
@@ -44,4 +44,31 @@ public final class GeometryTest
         assertTrue(geometry.contains(new Location(3, 1)));
         assertFalse(geometry.contains(new Location(4, 1)));
     }
+
+    @Test
+    public final void testCreateFromString()
+    {
+        try
+        {
+            Geometry.createFromString(null);
+            fail("Null value not allowed here.");
+        } catch (AssertionError e)
+        {
+            // Nothing to do here.
+        }
+        assertNull(Geometry.createFromString("a"));
+        assertNull(Geometry.createFromString("x4"));
+        final Geometry geometry = Geometry.createFromString("1x4");
+        assertNotNull(geometry);
+        assertEquals(1, geometry.getRows());
+        assertEquals(4, geometry.getColumns());
+        try
+        {
+            Geometry.createFromString("0x4");
+            fail("0 row not possible.");
+        } catch (AssertionError ex)
+        {
+            // Nothing to do here.
+        }
+    }
 }
\ No newline at end of file
-- 
GitLab