JAL-2526 additional tests for findPosition with/without cursor/edit
[jalview.git] / src / jalview / datamodel / SequenceCursor.java
1 package jalview.datamodel;
2
3 /**
4  * An immutable object representing one or more residue and corresponding
5  * alignment column positions for a sequence
6  */
7 public class SequenceCursor
8 {
9   /**
10    * the aligned sequence this cursor applies to
11    */
12   public final SequenceI sequence;
13
14   /**
15    * residue position in sequence (start...), 0 if undefined
16    */
17   public final int residuePosition;
18
19   /**
20    * column position (1...) corresponding to residuePosition, or 0 if undefined
21    */
22   public final int columnPosition;
23
24   /**
25    * a token which may be used to check whether this cursor is still valid for
26    * its sequence (allowing it to be ignored if the sequence has changed)
27    */
28   public final int token;
29
30   /**
31    * Constructor
32    * 
33    * @param seq
34    *          sequence this cursor applies to
35    * @param resPos
36    *          residue position in sequence (start..)
37    * @param column
38    *          column position in alignment (1..)
39    * @param tok
40    *          a token that may be validated by the sequence to check the cursor
41    *          is not stale
42    */
43   public SequenceCursor(SequenceI seq, int resPos, int column, int tok)
44   {
45     sequence = seq;
46     residuePosition = resPos;
47     columnPosition = column;
48     token = tok;
49   }
50
51   @Override
52   public int hashCode()
53   {
54     int hash = 31 * residuePosition;
55     hash = 31 * hash + columnPosition;
56     hash = 31 * hash + token;
57     if (sequence != null)
58     {
59       hash += sequence.hashCode();
60     }
61     return hash;
62   }
63
64   /**
65    * Two cursors are equal if they refer to the same sequence object and have
66    * the same residue position, column position and token value
67    */
68   @Override
69   public boolean equals(Object obj)
70   {
71     if (!(obj instanceof SequenceCursor))
72     {
73       return false;
74     }
75     SequenceCursor sc = (SequenceCursor) obj;
76     return sequence == sc.sequence && residuePosition == sc.residuePosition
77             && columnPosition == sc.columnPosition && token == sc.token;
78   }
79
80   @Override
81   public String toString()
82   {
83     return (sequence == null ? "" : sequence.getName()) + ":Pos"
84             + residuePosition + ":Col" + columnPosition + ":tok" + token;
85   }
86 }