Merge branch 'releases/Release_2_11_3_Branch'
[jalview.git] / src / jalview / datamodel / SequenceCursor.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.datamodel;
22
23 /**
24  * An immutable object representing one or more residue and corresponding
25  * alignment column positions for a sequence
26  */
27 public class SequenceCursor
28 {
29   /**
30    * the aligned sequence this cursor applies to
31    */
32   public final SequenceI sequence;
33
34   /**
35    * residue position in sequence (start...), 0 if undefined
36    */
37   public final int residuePosition;
38
39   /**
40    * column position (1...) corresponding to residuePosition, or 0 if undefined
41    */
42   public final int columnPosition;
43
44   /**
45    * column position (1...) of first residue in the sequence, or 0 if undefined
46    */
47   public final int firstColumnPosition;
48
49   /**
50    * column position (1...) of last residue in the sequence, or 0 if undefined
51    */
52   public final int lastColumnPosition;
53
54   /**
55    * a token which may be used to check whether this cursor is still valid for
56    * its sequence (allowing it to be ignored if the sequence has changed)
57    */
58   public final int token;
59
60   /**
61    * Constructor
62    * 
63    * @param seq
64    *          sequence this cursor applies to
65    * @param resPos
66    *          residue position in sequence (start..)
67    * @param column
68    *          column position in alignment (1..)
69    * @param tok
70    *          a token that may be validated by the sequence to check the cursor
71    *          is not stale
72    */
73   public SequenceCursor(SequenceI seq, int resPos, int column, int tok)
74   {
75     this(seq, resPos, column, 0, 0, tok);
76   }
77
78   /**
79    * Constructor
80    * 
81    * @param seq
82    *          sequence this cursor applies to
83    * @param resPos
84    *          residue position in sequence (start..)
85    * @param column
86    *          column position in alignment (1..)
87    * @param firstResCol
88    *          column position of the first residue in the sequence (1..), or 0
89    *          if not known
90    * @param lastResCol
91    *          column position of the last residue in the sequence (1..), or 0 if
92    *          not known
93    * @param tok
94    *          a token that may be validated by the sequence to check the cursor
95    *          is not stale
96    */
97   public SequenceCursor(SequenceI seq, int resPos, int column,
98           int firstResCol, int lastResCol, int tok)
99   {
100     sequence = seq;
101     residuePosition = resPos;
102     columnPosition = column;
103     firstColumnPosition = firstResCol;
104     lastColumnPosition = lastResCol;
105     token = tok;
106   }
107
108   @Override
109   public int hashCode()
110   {
111     int hash = 31 * residuePosition;
112     hash = 31 * hash + columnPosition;
113     hash = 31 * hash + token;
114     if (sequence != null)
115     {
116       hash += sequence.hashCode();
117     }
118     return hash;
119   }
120
121   /**
122    * Two cursors are equal if they refer to the same sequence object and have
123    * the same residue position, column position and token value
124    */
125   @Override
126   public boolean equals(Object obj)
127   {
128     if (!(obj instanceof SequenceCursor))
129     {
130       return false;
131     }
132     SequenceCursor sc = (SequenceCursor) obj;
133     return sequence == sc.sequence && residuePosition == sc.residuePosition
134             && columnPosition == sc.columnPosition && token == sc.token;
135   }
136
137   @Override
138   public String toString()
139   {
140     String name = sequence == null ? "" : sequence.getName();
141     return String.format("%s:Pos%d:Col%d:startCol%d:endCol%d:tok%d", name,
142             residuePosition, columnPosition, firstColumnPosition,
143             lastColumnPosition, token);
144   }
145 }