X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fintervalstore%2Fapi%2FIntervalI.java;h=c170a4324b7de63e02d4f6d64466ec1eee50ddc4;hb=28e9024f09a78a9625ed4defa2012bf342bec51e;hp=0dfd935a3937b3f54f34703671ad25a5f5b66ec2;hpb=0031ee4b6a42ad328e417cb65c7a840183e62e87;p=jalview.git diff --git a/src/intervalstore/api/IntervalI.java b/src/intervalstore/api/IntervalI.java index 0dfd935..c170a43 100644 --- a/src/intervalstore/api/IntervalI.java +++ b/src/intervalstore/api/IntervalI.java @@ -37,6 +37,33 @@ import java.util.List; public interface IntervalI { + + /** + * Compare intervals by start position ascending and end position descending. + */ + static Comparator COMPARATOR_BIGENDIAN = new Comparator() + { + @Override + public int compare(IntervalI o1, IntervalI o2) + { + int ret = Integer.signum(o1.getBegin() - o2.getBegin()); + return (ret == 0 ? Integer.signum(o2.getEnd() - o1.getEnd()) : ret); + } + }; + + /** + * Compare intervals by start position ascending and end position ascending. + */ + static Comparator COMPARATOR_LITTLEENDIAN = new Comparator() + { + @Override + public int compare(IntervalI o1, IntervalI o2) + { + int ret = Integer.signum(o1.getBegin() - o2.getBegin()); + return (ret == 0 ? Integer.signum(o1.getEnd() - o2.getEnd()) : ret); + } + }; + /** * a comparator for sorting intervals by start position ascending */ @@ -45,7 +72,7 @@ public interface IntervalI @Override public int compare(IntervalI o1, IntervalI o2) { - return Integer.compare(o1.getBegin(), o2.getBegin()); + return Integer.signum(o1.getBegin() - o2.getBegin()); } }; @@ -57,26 +84,30 @@ public interface IntervalI @Override public int compare(IntervalI o1, IntervalI o2) { - return Integer.compare(o2.getEnd(), o1.getEnd()); + return Integer.signum(o2.getEnd() - o1.getEnd()); } }; - int getBegin(); + static int NOT_CONTAINED = Integer.MIN_VALUE; + static int CONTAINMENT_UNKNOWN = 0; + int getBegin(); int getEnd(); /** * Answers true if this interval contains (or matches) the given interval + * based solely on start and end. * * @param i * @return */ default boolean containsInterval(IntervalI i) { - return i != null - && i.getBegin() >= getBegin() && i.getEnd() <= getEnd(); + return i != null && i.getBegin() >= getBegin() + && i.getEnd() <= getEnd(); } + /** * Answers true if this interval properly contains the given interval, that * is, it contains it and is larger than it @@ -90,11 +121,41 @@ public interface IntervalI && (i.getBegin() > getBegin() || i.getEnd() < getEnd()); } - default boolean equalsInterval(IntervalI i) - { - return i != null && i.getBegin() == getBegin() - && i.getEnd() == getEnd(); - } + /** + * Slower than equalsInterval; also includes type. + * + * Ensure that subclasses override equals(Object). For example: + * + * public boolean equals(Object o) { return o != null && o instanceof XXX && + * equalsInterval((XXX) i); } + * + * + * equalsInterval also must be overridden. + * + * public boolean equalsInterval(IntervalI i) {return ((SimpleFeature)i).start + * == start && ((SimpleFeature)i).end == end && ((SimpleFeature)i).description + * == this.description; } + * + * + * @param o + * @return true if equal, including a type check + */ + @Override + abstract boolean equals(Object o); + + + + /** + * Check that two intervals are equal, in terms of end points, descriptions, + * or any other distinguishing features. + * + * Used in IntervalStore in searches, since it is faster than equals(), as at + * that point we know that we have the correct type. + * + * @param i + * @return true if equal + */ + abstract boolean equalsInterval(IntervalI i); default boolean overlapsInterval(IntervalI i) { @@ -127,8 +188,5 @@ public interface IntervalI forwardStrand ? FORWARD_STRAND : REVERSE_STRAND); } - IntervalI getContainedBy(); - - void setContainedBy(IntervalI containedBy); }