Merge branch 'feature/JAL-3187linkedFeatures' into feature/JAL-3251biotypedMappings
[jalview.git] / src / jalview / datamodel / SequenceMapping.java
1 package jalview.datamodel;
2
3 /**
4  * Data bean that holds a mapping from one sequence to another
5  */
6 public class SequenceMapping
7 {
8   SequenceI fromSeq;
9
10   Mapping mapping;
11
12   private MappingType type;
13
14   SequenceMapping(SequenceI from, Mapping map)
15   {
16     this.fromSeq = from;
17     this.mapping = map;
18   }
19
20   /**
21    * Readable representation for debugging only, not guaranteed not to change
22    */
23   @Override
24   public String toString()
25   {
26     return String.format("From %s %s", fromSeq.getName(),
27             mapping.toString());
28   }
29
30   /**
31    * Returns a hashCode derived from the hashcodes of the mappings and fromSeq
32    * 
33    * @see SequenceMapping#hashCode()
34    */
35   @Override
36   public int hashCode()
37   {
38     return (fromSeq == null ? 0 : fromSeq.hashCode() * 31)
39             + mapping.hashCode();
40   }
41
42   /**
43    * Answers true if the objects hold the same mapping between the same two
44    * sequences
45    * 
46    * @see Mapping#equals
47    */
48   @Override
49   public boolean equals(Object obj)
50   {
51     if (!(obj instanceof SequenceMapping))
52     {
53       return false;
54     }
55     SequenceMapping that = (SequenceMapping) obj;
56     if (this.mapping == null)
57     {
58       return that.mapping == null;
59     }
60     // TODO: can simplify by asserting fromSeq is a dataset sequence
61     return (this.fromSeq == that.fromSeq
62             || (this.fromSeq != null && that.fromSeq != null
63                     && this.fromSeq.getDatasetSequence() != null
64                     && this.fromSeq.getDatasetSequence() == that.fromSeq
65                             .getDatasetSequence()))
66             && this.mapping.equals(that.mapping);
67   }
68
69   public SequenceI getFromSeq()
70   {
71     return fromSeq;
72   }
73
74   public Mapping getMapping()
75   {
76     return mapping;
77   }
78
79   public MappingType getType()
80   {
81     return type;
82   }
83 }