JAL-845 SplitFrame for "show product" and after aligning from SplitFrame
[jalview.git] / src / jalview / datamodel / SearchResults.java
index d36c872..a53d103 100755 (executable)
 package jalview.datamodel;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
+/**
+ * Holds a list of search result matches, where each match is a contiguous
+ * stretch of a single sequence.
+ * 
+ * @author gmcarstairs
+ *
+ */
 public class SearchResults
 {
 
   private List<Match> matches = new ArrayList<Match>();
 
+  public class Match
+  {
+    SequenceI sequence;
+
+    /*
+     * Start position of match in sequence (base 1)
+     */
+    int start;
+
+    /*
+     * End position (inclusive) (base 1)
+     */
+    int end;
+
+    public Match(SequenceI seq, int start, int end)
+    {
+      sequence = seq;
+      this.start = start;
+      this.end = end;
+    }
+
+    public SequenceI getSequence()
+    {
+      return sequence;
+    }
+
+    public int getStart()
+    {
+      return start;
+    }
+
+    public int getEnd()
+    {
+      return end;
+    }
+
+    /**
+     * Returns the string of characters in the matched region.
+     */
+    @Override
+    public String toString()
+    {
+      char[] chars = sequence.getSequence();
+      return String.valueOf(Arrays.copyOfRange(chars, start - 1, end));
+    }
+  }
+
   /**
    * This method replaces the old search results which merely held an alignment
    * index of search matches. This broke when sequences were moved around the
@@ -149,39 +204,63 @@ public class SearchResults
     return matches.get(index).sequence;
   }
 
-  public int getResultStart(int index)
+  /**
+   * Returns the start position of the i'th match in the search results.
+   * 
+   * @param i
+   * @return
+   */
+  public int getResultStart(int i)
   {
-    return matches.get(index).start;
+    return matches.get(i).start;
   }
 
-  public int getResultEnd(int index)
+  /**
+   * Returns the end position of the i'th match in the search results.
+   * 
+   * @param i
+   * @return
+   */
+  public int getResultEnd(int i)
   {
-    return matches.get(index).end;
+    return matches.get(i).end;
   }
 
-  class Match
+  /**
+   * Returns true if no search result matches are held.
+   * 
+   * @return
+   */
+  public boolean isEmpty()
   {
-    SequenceI sequence;
-
-    int start;
-
-    int end;
+    return matches.isEmpty();
+  }
 
-    public Match(SequenceI seq, int start, int end)
-    {
-      sequence = seq;
-      this.start = start;
-      this.end = end;
-    }
+  /**
+   * Returns the list of matches.
+   * 
+   * @return
+   */
+  public List<Match> getResults()
+  {
+    return matches;
   }
 
   /**
-   * Returns true if no search result matches are held.
+   * Return the results as a string of characters. Meant for use when the
+   * context ensures that all matches are to regions of the same sequence
+   * (otherwise the result is meaningless).
    * 
    * @return
    */
-  public boolean isEmpty()
+  @Override
+  public String toString()
   {
-    return matches.isEmpty();
+    StringBuilder result = new StringBuilder(256);
+    for (Match m : matches)
+    {
+      result.append(m.toString());
+    }
+    return result.toString();
   }
 }