X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FMapList.java;h=4658724e0f58cbda48e61745e9263109a7e59fc6;hb=3d0101179759ef157b088ea135423cd909512d9f;hp=8a014c65f16bca5d71df1289e5c12a0ea2140f74;hpb=18425bf4b285d3d8aeefd8ddbe77b1d6fd434f60;p=jalview.git diff --git a/src/jalview/util/MapList.java b/src/jalview/util/MapList.java index 8a014c6..4658724 100644 --- a/src/jalview/util/MapList.java +++ b/src/jalview/util/MapList.java @@ -88,8 +88,6 @@ 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; @@ -105,10 +103,22 @@ public class MapList { return false; } - return Arrays - .deepEquals(fromShifts.toArray(), obj.fromShifts.toArray()) - && Arrays - .deepEquals(toShifts.toArray(), obj.toShifts.toArray()); + return Arrays.deepEquals(fromShifts.toArray(), obj.fromShifts.toArray()) + && Arrays.deepEquals(toShifts.toArray(), + obj.toShifts.toArray()); + } + + /** + * Returns a hashcode made from the fromRatio, toRatio, and from/to ranges + */ + @Override + public int hashCode() + { + int hashCode = 31 * fromRatio; + hashCode = 31 * hashCode + toRatio; + hashCode = 31 * hashCode + fromShifts.toArray().hashCode(); + hashCode = 31 * hashCode + toShifts.toArray().hashCode(); + return hashCode; } /** @@ -215,7 +225,7 @@ public class MapList { /* * note lowest and highest values - bearing in mind the - * direction may be revesed + * direction may be reversed */ fromLowest = Math.min(fromLowest, Math.min(from[i], from[i + 1])); fromHighest = Math.max(fromHighest, Math.max(from[i], from[i + 1])); @@ -331,7 +341,8 @@ public class MapList */ public static List coalesceRanges(final List ranges) { - if (ranges == null || ranges.size() < 2) { + if (ranges == null || ranges.size() < 2) + { return ranges; } @@ -342,7 +353,7 @@ public class MapList lastRange = new int[] { lastRange[0], lastRange[1] }; merged.add(lastRange); boolean first = true; - + for (final int[] range : ranges) { if (first) @@ -361,10 +372,12 @@ public class MapList * drop this range if it lies within the last range */ if ((lastDirection == 1 && range[0] >= lastRange[0] - && range[0] <= lastRange[1] && range[1] >= lastRange[0] && range[1] <= lastRange[1]) + && range[0] <= lastRange[1] && range[1] >= lastRange[0] + && range[1] <= lastRange[1]) || (lastDirection == -1 && range[0] <= lastRange[0] && range[0] >= lastRange[1] - && range[1] <= lastRange[0] && range[1] >= lastRange[1])) + && range[1] <= lastRange[0] + && range[1] >= lastRange[1])) { changed = true; continue; @@ -376,10 +389,13 @@ public class MapList * if next range is in the same direction as last and contiguous, * just update the end position of the last range */ - boolean sameDirection = range[1] == range[0] || direction == lastDirection; + boolean sameDirection = range[1] == range[0] + || direction == lastDirection; boolean extending = range[0] == lastRange[1] + lastDirection; - boolean overlapping = (lastDirection == 1 && range[0] >= lastRange[0] && range[0] <= lastRange[1]) - || (lastDirection == -1 && range[0] <= lastRange[0] && range[0] >= lastRange[1]); + boolean overlapping = (lastDirection == 1 && range[0] >= lastRange[0] + && range[0] <= lastRange[1]) + || (lastDirection == -1 && range[0] <= lastRange[0] + && range[0] >= lastRange[1]); if (sameDirection && (overlapping || extending)) { lastRange[1] = range[1]; @@ -393,7 +409,7 @@ public class MapList lastDirection = (range[1] == range[0]) ? lastDirection : direction; } } - + return changed ? merged : ranges; } @@ -948,15 +964,17 @@ public class MapList // TODO not used - remove? if (local) { - return ((getFromLowest() >= map.getFromLowest() && getFromHighest() <= map - .getFromHighest()) || (getFromLowest() <= map.getFromLowest() && getFromHighest() >= map - .getFromHighest())); + return ((getFromLowest() >= map.getFromLowest() + && getFromHighest() <= map.getFromHighest()) + || (getFromLowest() <= map.getFromLowest() + && getFromHighest() >= map.getFromHighest())); } else { - return ((getToLowest() >= map.getToLowest() && getToHighest() <= map - .getToHighest()) || (getToLowest() <= map.getToLowest() && getToHighest() >= map - .getToHighest())); + return ((getToLowest() >= map.getToLowest() + && getToHighest() <= map.getToHighest()) + || (getToLowest() <= map.getToLowest() + && getToHighest() >= map.getToHighest())); } } @@ -972,7 +990,9 @@ public class MapList { sb.append(" ").append(Arrays.toString(shift)); } - sb.append(" ] To ["); + sb.append(" ] "); + sb.append(fromRatio).append(":").append(toRatio); + sb.append(" to ["); for (int[] shift : toShifts) { sb.append(" ").append(Arrays.toString(shift)); @@ -990,6 +1010,10 @@ public class MapList */ public void addMapList(MapList map) { + if (this.equals(map)) + { + return; + } this.fromLowest = Math.min(fromLowest, map.fromLowest); this.toLowest = Math.min(toLowest, map.toLowest); this.fromHighest = Math.max(fromHighest, map.fromHighest); @@ -1085,4 +1109,15 @@ public class MapList } return forwardStrand; } + + /** + * + * @return true if from, or to is a three to 1 mapping + */ + public boolean isTripletMap() + { + return (toRatio == 3 && fromRatio == 1) + || (fromRatio == 3 && toRatio == 1); + } + }