Javadoc fixes
[jabaws.git] / datamodel / compbio / data / sequence / Score.java
index b61c74e..ec4cadc 100644 (file)
@@ -1,19 +1,31 @@
+/* Copyright (c) 2011 Peter Troshin\r
+ *  \r
+ *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0     \r
+ * \r
+ *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
+ *  Apache License version 2 as published by the Apache Software Foundation\r
+ * \r
+ *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
+ *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
+ *  License for more details.\r
+ * \r
+ *  A copy of the license is in apache_license.txt. It is also available here:\r
+ * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
+ * \r
+ * Any republication or derived work distributed in source code form\r
+ * must include this copyright and license notice.\r
+ */\r
 package compbio.data.sequence;\r
 \r
-import java.io.BufferedWriter;\r
 import java.io.IOException;\r
-import java.io.OutputStream;\r
-import java.io.OutputStreamWriter;\r
+import java.io.Writer;\r
 import java.text.NumberFormat;\r
 import java.util.ArrayList;\r
 import java.util.Locale;\r
-import java.util.Set;\r
 import java.util.TreeSet;\r
 \r
 import javax.xml.bind.annotation.XmlAccessType;\r
 import javax.xml.bind.annotation.XmlAccessorType;\r
-import javax.xml.bind.annotation.XmlElement;\r
-import javax.xml.bind.annotation.XmlTransient;\r
 \r
 import compbio.util.annotation.Immutable;\r
 \r
@@ -26,27 +38,24 @@ import compbio.util.annotation.Immutable;
  */\r
 @XmlAccessorType(XmlAccessType.FIELD)\r
 @Immutable\r
-public class Score {\r
+public class Score implements Comparable<Score> {\r
 \r
-       @XmlTransient\r
        static final NumberFormat NUMBER_FORMAT = NumberFormat\r
                        .getNumberInstance(Locale.UK);\r
        static {\r
                NUMBER_FORMAT.setGroupingUsed(false);\r
                NUMBER_FORMAT.setMaximumFractionDigits(3);\r
        }\r
+       // This should be Enum<?> but JAXB cannot serialize it.\r
+       private final String method;\r
 \r
-       private Enum<?> method;\r
-       // private String method;\r
-\r
-       @XmlElement\r
        private TreeSet<Range> ranges = new TreeSet<Range>();\r
 \r
-       @XmlElement\r
        private ArrayList<Float> scores = new ArrayList<Float>(0);\r
 \r
        private Score() {\r
                // JaXB default constructor\r
+               method = "";\r
        }\r
 \r
        /**\r
@@ -60,7 +69,7 @@ public class Score {
         *            alignment\r
         */\r
        public Score(Enum<?> method, ArrayList<Float> scores) {\r
-               this.method = method;\r
+               this.method = method.toString();\r
                this.scores = new ArrayList<Float>(scores);\r
        }\r
 \r
@@ -76,18 +85,18 @@ public class Score {
         *            function, usually can be calculated based on scores\r
         */\r
        public Score(Enum<?> method, ArrayList<Float> scores, TreeSet<Range> ranges) {\r
-               this.method = method;\r
+               this.method = method.toString();\r
                this.ranges = ranges;\r
                this.scores = scores;\r
        }\r
 \r
        public Score(Enum<?> method, TreeSet<Range> ranges) {\r
-               this.method = method;\r
+               this.method = method.toString();\r
                this.ranges = ranges;\r
        }\r
 \r
        public Score(Enum<?> method, float[] scores) {\r
-               this.method = method;\r
+               this.method = method.toString();\r
                this.scores = toList(scores);\r
        }\r
 \r
@@ -103,7 +112,7 @@ public class Score {
         * \r
         * @return the ConservationMethod\r
         */\r
-       public Enum<?> getMethod() {\r
+       public String getMethod() {\r
                return method;\r
        }\r
 \r
@@ -119,7 +128,7 @@ public class Score {
        /**\r
         * Return Ranges if any Collections.EMPTY_SET otherwise\r
         * \r
-        * @return\r
+        * @return ordered set of Range\r
         */\r
        public TreeSet<Range> getRanges() {\r
                return ranges;\r
@@ -191,27 +200,38 @@ public class Score {
         * \r
         * @param scores\r
         *            the list of scores to output\r
-        * @param output\r
-        *            the stream to output the data to\r
+        * @param writer\r
         * @throws IOException\r
         *             if the OutputStream cannot be written into\r
         * @throws NullPointerException\r
         *             if the output stream is null\r
         */\r
-       public static void write(Set<Score> scores, OutputStream output)\r
+       public static void write(TreeSet<Score> scores, Writer writer)\r
                        throws IOException {\r
-               if (output == null) {\r
-                       throw new NullPointerException("OutputStream must be provided!");\r
+               if (writer == null) {\r
+                       throw new NullPointerException("Writer must be provided!");\r
                }\r
-               BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(\r
-                               output));\r
                for (Score score : scores) {\r
                        writer.write("#" + score.method + " ");\r
-                       for (Float scoreVal : score.getScores()) {\r
+                       int count = score.ranges.size();\r
+                       for (Range range : score.ranges) {\r
+                               count--;\r
+                               writer.write(range.toString());\r
+                               if (count != 0) {\r
+                                       writer.write(", ");\r
+                               }\r
+                       }\r
+                       for (Float scoreVal : score.scores) {\r
                                writer.write(NUMBER_FORMAT.format(scoreVal) + " ");\r
                        }\r
                        writer.write("\n");\r
+                       writer.flush();\r
                }\r
                writer.flush();\r
        }\r
+\r
+       @Override\r
+       public int compareTo(Score o) {\r
+               return this.method.compareTo(o.method);\r
+       }\r
 }\r