d0e62a1f94f8ed48b4783da8f8b455691b974949
[jalview.git] / src / jalview / datamodel / AlignedCodon.java
1 package jalview.datamodel;
2
3 /**
4  * Holds the aligned column positions (base 0) for one codon in a nucleotide
5  * sequence. The object is immutable once created.
6  * 
7  * Example: in "G-AT-C-GA" the aligned codons are (0, 2, 3) and (5, 7, 8).
8  * 
9  * @author gmcarstairs
10  *
11  */
12 public final class AlignedCodon
13 {
14   public final int pos1;
15
16   public final int pos2;
17
18   public final int pos3;
19
20   public AlignedCodon(int i, int j, int k)
21   {
22     pos1 = i;
23     pos2 = j;
24     pos3 = k;
25   }
26
27   /**
28    * Returns the column position for the given base (1, 2, 3).
29    * 
30    * @param base
31    * @return
32    * @throws IllegalArgumentException
33    *           if an argument value other than 1, 2 or 3 is supplied
34    */
35   public int getBaseColumn(int base)
36   {
37     if (base < 1 || base > 3)
38     {
39       throw new IllegalArgumentException(Integer.toString(base));
40     }
41     return base == 1 ? pos1 : (base == 2 ? pos2 : pos3);
42   }
43
44   /**
45    * Two aligned codons are equal if all their base positions are the same.
46    */
47   @Override
48   public boolean equals(Object o)
49   {
50     /*
51      * Equality with null value required for consistency with
52      * Dna.compareCodonPos
53      */
54     if (o == null)
55     {
56       return true;
57     }
58     if (!(o instanceof AlignedCodon))
59     {
60       return false;
61     }
62     AlignedCodon ac = (AlignedCodon) o;
63     return (pos1 == ac.pos1 && pos2 == ac.pos2 && pos3 == ac.pos3);
64   }
65
66   @Override
67   public String toString()
68   {
69     return "[" + pos1 + ", " + pos2 + ", " + pos3 + "]";
70   }
71 }