X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FSearchResults.java;fp=src%2Fjalview%2Fdatamodel%2FSearchResults.java;h=ad0e472f36f9bb9bf1deafd3389f1b4e3272e83d;hb=9623cea766a766683243235557ad48e6f7659e6a;hp=2a33f6c9afc3f61e400a2f2aada814b1ade65a78;hpb=6066400ed6b0fe288ebbea82389f59838a534706;p=jalview.git diff --git a/src/jalview/datamodel/SearchResults.java b/src/jalview/datamodel/SearchResults.java index 2a33f6c..ad0e472 100755 --- a/src/jalview/datamodel/SearchResults.java +++ b/src/jalview/datamodel/SearchResults.java @@ -36,6 +36,10 @@ public class SearchResults private List matches = new ArrayList(); + /** + * One match consists of a sequence reference, start and end positions. + * Discontiguous ranges in a sequence require two or more Match objects. + */ public class Match { SequenceI sequence; @@ -83,11 +87,22 @@ public class SearchResults } /** - * Returns the string of characters in the matched region. + * Returns the string of characters in the matched region, prefixed by the + * start position, e.g. "12CGT" or "208K" */ @Override public String toString() { + final int from = Math.max(start - 1, 0); + String startPosition = String.valueOf(from); + return startPosition + getCharacters(); + } + + /** + * Returns the string of characters in the matched region. + */ + public String getCharacters() + { char[] chars = sequence.getSequence(); // convert start/end to base 0 (with bounds check) final int from = Math.max(start - 1, 0); @@ -99,6 +114,35 @@ public class SearchResults { this.sequence = seq; } + + /** + * Hashcode is the hashcode of the matched sequence plus a hash of start and + * end positions. Match objects that pass the test for equals are guaranteed + * to have the same hashcode. + */ + @Override + public int hashCode() + { + int hash = sequence == null ? 0 : sequence.hashCode(); + hash += 31 * start; + hash += 67 * end; + return hash; + } + + /** + * Two Match objects are equal if they are for the same sequence, start and + * end positions + */ + @Override + public boolean equals(Object obj) + { + if (obj == null || !(obj instanceof Match)) + { + return false; + } + Match m = (Match) obj; + return (this.sequence == m.sequence && this.start == m.start && this.end == m.end); + } } /** @@ -188,8 +232,7 @@ public class SearchResults if (result == null) { - result = new int[] - { matchStart, matchEnd }; + result = new int[] { matchStart, matchEnd }; } else { @@ -265,9 +308,9 @@ public class SearchResults } /** - * 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 the results as a string of characters (bases) prefixed by start + * position(s). Meant for use when the context ensures that all matches are to + * regions of the same sequence (otherwise the result is meaningless). * * @return */ @@ -281,4 +324,47 @@ public class SearchResults } return result.toString(); } + + /** + * Return the results as a string of characters (bases). Meant for use when + * the context ensures that all matches are to regions of the same sequence + * (otherwise the result is meaningless). + * + * @return + */ + public String getCharacters() + { + StringBuilder result = new StringBuilder(256); + for (Match m : matches) + { + result.append(m.getCharacters()); + } + return result.toString(); + } + + /** + * Hashcode is has derived from the list of matches. This ensures that when + * two SearchResults objects satisfy the test for equals(), then they have the + * same hashcode. + */ + @Override + public int hashCode() + { + return matches.hashCode(); + } + + /** + * Two SearchResults are considered equal if they contain the same matches in + * the same order. + */ + @Override + public boolean equals(Object obj) + { + if (obj == null || !(obj instanceof SearchResults)) + { + return false; + } + SearchResults sr = (SearchResults) obj; + return ((ArrayList) this.matches).equals(sr.matches); + } }