From ab9ef8e12267ac1cb1776a86e185cc68ed8ca71d Mon Sep 17 00:00:00 2001
From: ribeaudc <ribeaudc>
Date: Fri, 6 Jul 2007 08:22:31 +0000
Subject: [PATCH] add: - More intelligent 'Comparator' for well codes

SVN: 906
---
 .../common/utilities/MatrixComparator.java    | 84 +++++++++++++++++++
 .../utilities/MatrixComparatorTest.java       | 66 +++++++++++++++
 2 files changed, 150 insertions(+)
 create mode 100644 common/source/java/ch/systemsx/cisd/common/utilities/MatrixComparator.java
 create mode 100644 common/sourceTest/java/ch/systemsx/cisd/common/utilities/MatrixComparatorTest.java

diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/MatrixComparator.java b/common/source/java/ch/systemsx/cisd/common/utilities/MatrixComparator.java
new file mode 100644
index 00000000000..921a9786111
--- /dev/null
+++ b/common/source/java/ch/systemsx/cisd/common/utilities/MatrixComparator.java
@@ -0,0 +1,84 @@
+/*
+ * 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.common.utilities;
+
+import java.util.Comparator;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * A <code>Comparator</code> implementation that is based on matrix labelling having the following form:
+ * <code>[a-zA-Z]+[0-9]+</code>.
+ * <p>
+ * A default natural sorting will place, for instance, <code>A3</code> before <code>A10</code>. This comparator can
+ * also sort by letter first or by number first.
+ * </p>
+ * 
+ * @author Christian Ribeaud
+ */
+public final class MatrixComparator implements Comparator<String>
+{
+
+    private final static Pattern pattern = Pattern.compile("([a-zA-Z]+)([0-9]+)");
+
+    private final boolean letterFirst;
+
+    public MatrixComparator()
+    {
+        this(true);
+    }
+
+    public MatrixComparator(final boolean letterFirst)
+    {
+        this.letterFirst = letterFirst;
+    }
+
+    private final static String[] decompose(String entry)
+    {
+        Matcher matcher = pattern.matcher(entry);
+        if (matcher.matches())
+        {
+            return new String[]
+                { matcher.group(1), matcher.group(2) };
+        }
+        return null;
+    }
+
+    //
+    // Comparator
+    //
+
+    public final int compare(String o1, String o2)
+    {
+        String[] s1 = decompose(o1);
+        String[] s2 = decompose(o2);
+        if (s1 != null && s2 != null)
+        {
+            int sCompare = s1[0].compareTo(s2[0]);
+            // No check for NumberFormatException as we are sure we have numbers here.
+            int iCompare = Integer.parseInt(s1[1]) - Integer.parseInt(s2[1]);
+            if (letterFirst)
+            {
+                return sCompare == 0 ? iCompare : sCompare;
+            } else
+            {
+                return iCompare == 0 ? sCompare : iCompare;
+            }
+        }
+        return o1.compareTo(o2);
+    }
+}
\ No newline at end of file
diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/MatrixComparatorTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/MatrixComparatorTest.java
new file mode 100644
index 00000000000..5f4d5c377f2
--- /dev/null
+++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/MatrixComparatorTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.common.utilities;
+
+import java.util.Arrays;
+
+import org.apache.commons.lang.ArrayUtils;
+import org.testng.annotations.Test;
+
+/**
+ * Test cases for the {@link MatrixComparator}.
+ * 
+ * @author Christian Ribeaud
+ */
+public final class MatrixComparatorTest
+{
+
+    private final static String[] createData()
+    {
+        return new String[]
+            { "a10", "a3", "A11", "B4", "C6", "c23", "a1", "a01" };
+    }
+
+    @Test
+    public final void testLetterFirst()
+    {
+        String[] s = createData();
+        Arrays.sort(s, new MatrixComparator());
+        assert Arrays.equals(s, new String[]
+            { "A11", "B4", "C6", "a1", "a01", "a3", "a10", "c23" });
+    }
+
+    @Test
+    public final void testDigitFirst()
+    {
+        String[] s = createData();
+        Arrays.sort(s, new MatrixComparator(false));
+        assert Arrays.equals(s, new String[]
+            { "a1", "a01", "a3", "B4", "C6", "a10", "A11", "c23" });
+    }
+
+    @Test
+    public final void testWrongPattern()
+    {
+        String[] s =
+            { "12", "3", "aa", "bb", "1c" };
+        String[] clone = (String[]) ArrayUtils.clone(s);
+        Arrays.sort(s, new MatrixComparator());
+        Arrays.sort(clone);
+        Arrays.equals(s, clone);
+    }
+}
\ No newline at end of file
-- 
GitLab