b5929bfb2b56debcda051078a003b78ea125c8b0
[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    * column position (1...) of first residue in the sequence, or 0 if undefined
26    */
27   public final int firstColumnPosition;
28
29   /**
30    * column position (1...) of last residue in the sequence, or 0 if undefined
31    */
32   public final int lastColumnPosition;
33
34   /**
35    * a token which may be used to check whether this cursor is still valid for
36    * its sequence (allowing it to be ignored if the sequence has changed)
37    */
38   public final int token;
39
40   /**
41    * Constructor
42    * 
43    * @param seq
44    *          sequence this cursor applies to
45    * @param resPos
46    *          residue position in sequence (start..)
47    * @param column
48    *          column position in alignment (1..)
49    * @param tok
50    *          a token that may be validated by the sequence to check the cursor
51    *          is not stale
52    */
53   public SequenceCursor(SequenceI seq, int resPos, int column, int tok)
54   {
55     this(seq, resPos, column, 0, 0, tok);
56   }
57
58   /**
59    * Constructor
60    * 
61    * @param seq
62    *          sequence this cursor applies to
63    * @param resPos
64    *          residue position in sequence (start..)
65    * @param column
66    *          column position in alignment (1..)
67    * @param firstResCol
68    *          column position of the first residue in the sequence (1..), or 0
69    *          if not known
70    * @param lastResCol
71    *          column position of the last residue in the sequence (1..), or 0 if
72    *          not known
73    * @param tok
74    *          a token that may be validated by the sequence to check the cursor
75    *          is not stale
76    */
77   public SequenceCursor(SequenceI seq, int resPos, int column, int firstResCol,
78           int lastResCol, int tok)
79   {
80     sequence = seq;
81     residuePosition = resPos;
82     columnPosition = column;
83     firstColumnPosition = firstResCol;
84     lastColumnPosition = lastResCol;
85     token = tok;
86   }
87
88   @Override
89   public int hashCode()
90   {
91     int hash = 31 * residuePosition;
92     hash = 31 * hash + columnPosition;
93     hash = 31 * hash + token;
94     if (sequence != null)
95     {
96       hash += sequence.hashCode();
97     }
98     return hash;
99   }
100
101   /**
102    * Two cursors are equal if they refer to the same sequence object and have
103    * the same residue position, column position and token value
104    */
105   @Override
106   public boolean equals(Object obj)
107   {
108     if (!(obj instanceof SequenceCursor))
109     {
110       return false;
111     }
112     SequenceCursor sc = (SequenceCursor) obj;
113     return sequence == sc.sequence && residuePosition == sc.residuePosition
114             && columnPosition == sc.columnPosition && token == sc.token;
115   }
116
117   @Override
118   public String toString()
119   {
120     String name = sequence == null ? "" : sequence.getName();
121     return String.format("%s:Pos%d:Col%d:startCol%d:endCol%d:tok%d", name,
122             residuePosition, columnPosition, firstColumnPosition,
123             lastColumnPosition, token);
124   }
125 }