From 4dc04ddf33f1e7fe8cc09de361bcb2790053a9ee Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Mon, 7 Sep 2009 10:53:07 +0000
Subject: [PATCH] SE-137 Abundance columns sorted.

SVN: 12495
---
 .../phosphonetx/server/PhosphoNetXServer.java |   6 +-
 .../basic/dto/AbundanceColumnDefinition.java  |  32 ++++-
 .../shared/basic/dto/Treatment.java           |  28 ++++-
 .../dto/AbundanceColumnDefinitionTest.java    | 119 ++++++++++++++++++
 4 files changed, 182 insertions(+), 3 deletions(-)
 create mode 100644 rtd_phosphonetx/sourceTest/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/shared/basic/dto/AbundanceColumnDefinitionTest.java

diff --git a/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/server/PhosphoNetXServer.java b/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/server/PhosphoNetXServer.java
index 0f964cb130b..b67e5efdf3b 100644
--- a/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/server/PhosphoNetXServer.java
+++ b/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/server/PhosphoNetXServer.java
@@ -18,6 +18,7 @@ package ch.systemsx.cisd.openbis.plugin.phosphonetx.server;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -125,7 +126,10 @@ public class PhosphoNetXServer extends AbstractServer<IPhosphoNetXServer> implem
                     columnDefinitions.put(sampleID, columnDefinition);
                 }
             }
-            return new ArrayList<AbundanceColumnDefinition>(columnDefinitions.values());
+            List<AbundanceColumnDefinition> definitions =
+                    new ArrayList<AbundanceColumnDefinition>(columnDefinitions.values());
+            Collections.sort(definitions);
+            return definitions;
         } finally
         {
             samplePermIDs.close();
diff --git a/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/shared/basic/dto/AbundanceColumnDefinition.java b/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/shared/basic/dto/AbundanceColumnDefinition.java
index 19b39f9fe68..cdf41fe62d4 100644
--- a/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/shared/basic/dto/AbundanceColumnDefinition.java
+++ b/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/shared/basic/dto/AbundanceColumnDefinition.java
@@ -17,6 +17,7 @@
 package ch.systemsx.cisd.openbis.plugin.phosphonetx.shared.basic.dto;
 
 import java.io.Serializable;
+import java.util.Collections;
 import java.util.List;
 
 import com.google.gwt.user.client.rpc.IsSerializable;
@@ -28,7 +29,7 @@ import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ServiceVersionHolder;
  *
  * @author Franz-Josef Elmer
  */
-public class AbundanceColumnDefinition implements Serializable, IsSerializable
+public class AbundanceColumnDefinition implements Serializable, IsSerializable, Comparable<AbundanceColumnDefinition>
 {
     private static final long serialVersionUID = ServiceVersionHolder.VERSION;
     
@@ -67,4 +68,33 @@ public class AbundanceColumnDefinition implements Serializable, IsSerializable
     {
         this.treatments = treatments;
     }
+
+    public int compareTo(AbundanceColumnDefinition that)
+    {
+        if (this.treatments != null && that.treatments != null)
+        {
+            int sizeDiff = this.treatments.size() - that.treatments.size();
+            if (sizeDiff != 0)
+            {
+                return sizeDiff;
+            }
+            Collections.sort(this.treatments);
+            Collections.sort(that.treatments);
+            for (int i = 0, n = treatments.size(); i < n; i++)
+            {
+                Treatment thisTreatment = this.treatments.get(i);
+                Treatment thatTreatment = that.treatments.get(i);
+                int diff = thisTreatment.compareTo(thatTreatment);
+                if (diff != 0)
+                {
+                    return diff;
+                }
+            }
+        }
+        if (this.sampleCode != null && that.sampleCode != null)
+        {
+            return this.sampleCode.compareTo(that.sampleCode);
+        }
+        return 0;
+    }
 }
diff --git a/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/shared/basic/dto/Treatment.java b/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/shared/basic/dto/Treatment.java
index c34f7909b28..1ad181f3068 100644
--- a/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/shared/basic/dto/Treatment.java
+++ b/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/shared/basic/dto/Treatment.java
@@ -27,7 +27,7 @@ import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ServiceVersionHolder;
  *
  * @author Franz-Josef Elmer
  */
-public class Treatment implements Serializable, IsSerializable
+public class Treatment implements Serializable, IsSerializable, Comparable<Treatment>
 {
     private static final long serialVersionUID = ServiceVersionHolder.VERSION;
 
@@ -72,6 +72,32 @@ public class Treatment implements Serializable, IsSerializable
         return value + " " + type;
     }
 
+    public int compareTo(Treatment that)
+    {
+        if (this.type.compareTo(that.type) != 0)
+        {
+            return this.type.compareTo(that.type);
+        }
+        double thisNumber = convert(this.value);
+        double thatNumber = convert(that.value);
+        if (Double.isNaN(thisNumber) || Double.isNaN(thatNumber))
+        {
+            return this.value.compareTo(that.value);
+        }
+        return Double.compare(thisNumber, thatNumber);
+    }
+    
+    private double convert(String numberOrString)
+    {
+        try
+        {
+            return Double.parseDouble(numberOrString);
+        } catch (NumberFormatException e)
+        {
+            return Double.NaN;
+        }
+    }
+
     @Override
     public String toString()
     {
diff --git a/rtd_phosphonetx/sourceTest/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/shared/basic/dto/AbundanceColumnDefinitionTest.java b/rtd_phosphonetx/sourceTest/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/shared/basic/dto/AbundanceColumnDefinitionTest.java
new file mode 100644
index 00000000000..2baae805ce3
--- /dev/null
+++ b/rtd_phosphonetx/sourceTest/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/shared/basic/dto/AbundanceColumnDefinitionTest.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2009 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.openbis.plugin.phosphonetx.shared.basic.dto;
+
+import java.util.Arrays;
+
+import org.testng.AssertJUnit;
+import org.testng.annotations.Test;
+
+/**
+ * 
+ *
+ * @author Franz-Josef Elmer
+ */
+public class AbundanceColumnDefinitionTest extends AssertJUnit
+{
+    @Test
+    public void testCompareTwoDefinitonsWithoutTreatmentsAndSampleCodes()
+    {
+        AbundanceColumnDefinition d1 = new AbundanceColumnDefinition();
+        AbundanceColumnDefinition d2 = create(null);
+        
+        assertEquals(0, d1.compareTo(d2));
+        
+        d1.setSampleCode("bla");
+        
+        assertEquals(0, d1.compareTo(d2));
+    }
+    
+    @Test
+    public void testCompareTwoDefinitonsWithoutTreatmentsButSampleCodes()
+    {
+        AbundanceColumnDefinition d1 = create("abc");
+        AbundanceColumnDefinition d2 = create("abc");
+        
+        assertEquals(0, d1.compareTo(d2));
+        
+        d2.setSampleCode("def");
+        
+        assertEquals(true, d1.compareTo(d2) < 0);
+    }
+    
+    @Test
+    public void testCompareTwoDefinitonsWithDifferentNumberOfTreatments()
+    {
+        AbundanceColumnDefinition d1 = create("abc");
+        d1.setSampleCode("abc");
+        AbundanceColumnDefinition d2 = create("abc", new Treatment());
+        d2.setSampleCode("abc");
+        
+        assertEquals(true, d1.compareTo(d2) < 0);
+    }
+    
+    @Test
+    public void testCompareTwoDefinitonsWithOneNonNumericalTreatment()
+    {
+        AbundanceColumnDefinition d1 = create("abc", treatment("light", "red"));
+        AbundanceColumnDefinition d2 = create("abc", treatment("light", "blue"));
+        
+        assertEquals(true, d1.compareTo(d2) > 0);
+    }
+    
+    @Test
+    public void testCompareTwoDefinitonsWithOneNumericalTreatment()
+    {
+        AbundanceColumnDefinition d1 = create("abc", treatment("pH", "7.5"));
+        AbundanceColumnDefinition d2 = create("abc", treatment("pH", "9e-1"));
+        
+        assertEquals(true, d1.compareTo(d2) > 0);
+    }
+    
+    @Test
+    public void testCompareTwoDefinitonsWithOneNumericalTreatmentButDifferentTypes()
+    {
+        AbundanceColumnDefinition d1 = create("abc", treatment("K", "7.5"));
+        AbundanceColumnDefinition d2 = create("abc", treatment("pH", "9e-1"));
+        
+        assertEquals(true, d1.compareTo(d2) < 0);
+    }
+    
+    @Test
+    public void testCompareTwoDefinitonsWithTwoNumericalTreatment()
+    {
+        AbundanceColumnDefinition d1 = create("abc", treatment("pH", "7.50"), treatment("T", "13"));
+        AbundanceColumnDefinition d2 = create("abc", treatment("pH", "0.75e1"), treatment("T", "8"));
+        
+        assertEquals(true, d1.compareTo(d2) > 0);
+    }
+    
+    private AbundanceColumnDefinition create(String sampleCode, Treatment... treatments)
+    {
+        AbundanceColumnDefinition definition = new AbundanceColumnDefinition();
+        definition.setSampleCode(sampleCode);
+        definition.setTreatments(Arrays.asList(treatments));
+        return definition;
+    }
+    
+    private Treatment treatment(String type, String value)
+    {
+        Treatment treatment = new Treatment();
+        treatment.setType(type);
+        treatment.setValue(value);
+        return treatment;
+    }
+}
-- 
GitLab