JWS-29 patch fixes bug - wierd workaround since "^\\s*>" caused scanner to advance...
[jabaws.git] / datamodel / compbio / data / sequence / ScoreManager.java
index f384308..d16a059 100644 (file)
@@ -1,22 +1,39 @@
+/* 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.util.ArrayList;\r
-import java.util.HashMap;\r
-import java.util.HashSet;\r
 import java.util.List;\r
 import java.util.Map;\r
 import java.util.Set;\r
+import java.util.TreeMap;\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.XmlTransient;\r
 \r
 @XmlAccessorType(XmlAccessType.FIELD)\r
 public class ScoreManager {\r
 \r
+       @XmlTransient\r
        public static final String SINGLE_ENTRY_KEY = "Alignment";\r
 \r
        private List<ScoreHolder> seqScores;\r
@@ -45,9 +62,9 @@ public class ScoreManager {
 \r
        public static ScoreManager newInstanceSingleScore(\r
                        Map<String, Score> seqScoresMap) {\r
-               Map<String, Set<Score>> multipleScoresMap = new HashMap<String, Set<Score>>();\r
+               Map<String, Set<Score>> multipleScoresMap = new TreeMap<String, Set<Score>>();\r
                for (Map.Entry<String, Score> seqScore : seqScoresMap.entrySet()) {\r
-                       Set<Score> scores = new HashSet<Score>();\r
+                       Set<Score> scores = new TreeSet<Score>();\r
                        scores.add(seqScore.getValue());\r
                        multipleScoresMap.put(seqScore.getKey(), scores);\r
                }\r
@@ -55,13 +72,15 @@ public class ScoreManager {
        }\r
 \r
        public static ScoreManager newInstanceSingleSequence(Set<Score> data) {\r
-               return new ScoreManager(ScoreManager.SINGLE_ENTRY_KEY, data);\r
+               return new ScoreManager(ScoreManager.SINGLE_ENTRY_KEY,\r
+                               new TreeSet(data));\r
        }\r
 \r
-       public Map<String, Set<Score>> asMap() {\r
-               Map<String, Set<Score>> seqScoresMap = new HashMap<String, Set<Score>>();\r
+       public Map<String, TreeSet<Score>> asMap() {\r
+               Map<String, TreeSet<Score>> seqScoresMap = new TreeMap<String, TreeSet<Score>>();\r
                for (ScoreHolder sch : this.seqScores) {\r
-                       Set<Score> oldValue = seqScoresMap.put(sch.id, sch.scores);\r
+                       TreeSet<Score> oldValue = seqScoresMap.put(sch.id, new TreeSet(\r
+                                       sch.scores));\r
                        if (oldValue != null) {\r
                                throw new IllegalStateException(\r
                                                "Cannot represent this ScoreManager instance "\r
@@ -96,15 +115,17 @@ public class ScoreManager {
                return null;\r
        }\r
 \r
-       public void writeOut(OutputStream outStream) throws IOException {\r
-               BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(\r
-                               outStream));\r
+       public void writeOut(Writer outStream) throws IOException {\r
+               assert outStream != null : "Output steam is not defined!";\r
+               if (seqScores == null) {\r
+                       return;\r
+               }\r
                for (ScoreHolder oneSeqScores : seqScores) {\r
+                       if (oneSeqScores == null)\r
+                               continue;\r
                        oneSeqScores.writeOut(outStream);\r
                }\r
-               writer.flush();\r
        }\r
-       \r
 \r
        @Override\r
        public int hashCode() {\r
@@ -132,13 +153,11 @@ public class ScoreManager {
                return true;\r
        }\r
 \r
-\r
-\r
        @XmlAccessorType(XmlAccessType.FIELD)\r
        public static class ScoreHolder {\r
 \r
                public String id;\r
-               public Set<Score> scores;\r
+               public TreeSet<Score> scores;\r
 \r
                private ScoreHolder() {\r
                        // JAXB Default constructor should not be used otherwise\r
@@ -146,25 +165,30 @@ public class ScoreManager {
 \r
                ScoreHolder(String id, Set<Score> scores) {\r
                        this.id = id;\r
-                       this.scores = scores;\r
+                       this.scores = new TreeSet<Score>(scores);\r
                }\r
 \r
-               public void writeOut(OutputStream outStream) throws IOException {\r
-                       BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(\r
-                                       outStream));\r
+               public void writeOut(Writer writer) throws IOException {\r
                        writer.write(">" + id + "\n");\r
-                       Score.write(scores, outStream);\r
+                       Score.write(scores, writer);\r
                }\r
 \r
                public Score getScoreByMethod(Enum<?> method) {\r
                        for (Score sc : scores) {\r
-                               if (method == sc.getMethod()) {\r
+                               if (method.toString().equals(sc.getMethod())) {\r
+                                       return sc;\r
+                               }\r
+                       }\r
+                       return null;\r
+               }\r
+               public Score getScoreByMethod(String method) {\r
+                       for (Score sc : scores) {\r
+                               if (method.toString().equals(sc.getMethod())) {\r
                                        return sc;\r
                                }\r
                        }\r
                        return null;\r
                }\r
-\r
                public int getNumberOfScores() {\r
                        return scores.size();\r
                }\r
@@ -200,7 +224,7 @@ public class ScoreManager {
                                return false;\r
                        return true;\r
                }\r
-               \r
-               \r
+\r
        }\r
+\r
 }\r