JAL-1686 @Override and/or TODO added to equals(Object) methods
[jalview.git] / src / jalview / util / MapList.java
index 32f0256..2641659 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
@@ -79,6 +79,8 @@ public class MapList
   @Override
   public boolean equals(Object o)
   {
+    // TODO should also override hashCode to ensure equal objects have equal
+    // hashcodes
     if (o == null || !(o instanceof MapList))
     {
       return false;
@@ -101,23 +103,23 @@ public class MapList
   }
 
   /**
-   * Returns the flattened 'from' ranges as [start1, end1, start2, end2, ...]
+   * Returns the 'from' ranges as {[start1, end1], [start2, end2], ...}
    * 
    * @return
    */
-  public int[] getFromRanges()
+  public List<int[]> getFromRanges()
   {
-    return getRanges(fromShifts);
+    return fromShifts;
   }
 
   /**
-   * Returns the flattened 'to' ranges as [start1, end1, start2, end2, ...]
+   * Returns the 'to' ranges as {[start1, end1], [start2, end2], ...}
    * 
    * @return
    */
-  public int[] getToRanges()
+  public List<int[]> getToRanges()
   {
-    return getRanges(toShifts);
+    return toShifts;
   }
 
   /**
@@ -191,6 +193,8 @@ public class MapList
    */
   public MapList(int from[], int to[], int fromRatio, int toRatio)
   {
+    this.fromRatio = fromRatio;
+    this.toRatio = toRatio;
     fromLowest = from[0];
     fromHighest = from[1];
     for (int i = 0; i < from.length; i += 2)
@@ -211,8 +215,6 @@ public class MapList
       toShifts.add(new int[]
       { to[i], to[i + 1] });
     }
-    this.fromRatio = fromRatio;
-    this.toRatio = toRatio;
   }
 
   /**
@@ -249,6 +251,38 @@ public class MapList
   }
 
   /**
+   * Constructor given ranges as lists of [start, end] positions
+   * 
+   * @param fromRange
+   * @param toRange
+   * @param fromRatio
+   * @param toRatio
+   */
+  public MapList(List<int[]> fromRange, List<int[]> toRange,
+          int fromRatio, int toRatio)
+  {
+    this.fromShifts = fromRange;
+    this.toShifts = toRange;
+    this.fromRatio = fromRatio;
+    this.toRatio = toRatio;
+
+    fromLowest = Integer.MAX_VALUE;
+    fromHighest = 0;
+    for (int[] range : fromRange) {
+      fromLowest = Math.min(fromLowest, range[0]);
+      fromHighest = Math.max(fromHighest, range[1]);
+    }
+
+    toLowest = Integer.MAX_VALUE;
+    toHighest = 0;
+    for (int[] range : toRange)
+    {
+      toLowest = Math.min(toLowest, range[0]);
+      toHighest = Math.max(toHighest, range[1]);
+    }
+  }
+
+  /**
    * get all mapped positions from 'from' to 'to'
    * 
    * @return int[][] { int[] { fromStart, fromFinish, toStart, toFinish }, int