diff --git a/playground/java/src/org/broadinstitute/sting/indels/CountedObjectComparatorAdapter.java b/playground/java/src/org/broadinstitute/sting/indels/CountedObjectComparatorAdapter.java new file mode 100755 index 000000000..75e4f2678 --- /dev/null +++ b/playground/java/src/org/broadinstitute/sting/indels/CountedObjectComparatorAdapter.java @@ -0,0 +1,41 @@ +package org.broadinstitute.sting.indels; + +/** Support class for counted objects. This comparator is an adapter: it is initialized with an arbitrary + * comparator for objects of type T and can be used to directly compare counted objects of type CountedObject + * (the underlying Comparator will be used to compare the "object" part of the counted objects, the counter values + * will be ignored). This comparator also provides additional, non-standard methods that allow direct + * comparison between a CountedObject and "raw" object of type T (the same underlying Comparator will be used, + * and the value of the counter in the counted object wil be ignored). + * @param + */ +public class CountedObjectComparatorAdapter implements java.util.Comparator> { + + private java.util.Comparator mComp; + + /** Initializes comparator adapter with a comparator for objects of trype T */ + public CountedObjectComparatorAdapter(java.util.Comparator adaptee) { + mComp = adaptee; + } + + @Override + public int compare(CountedObject o1, CountedObject o2) { + return mComp.compare(o1.getObject(),o2.getObject()); + } + + public int compare(T o1, CountedObject o2) { + return mComp.compare(o1,o2.getObject()); + } + + public int compare(CountedObject o1, T o2) { + return mComp.compare(o1.getObject(),o2); + } + + @Override + public boolean equals(Object o) { + if ( o instanceof CountedObjectComparatorAdapter) { + if ( ((CountedObjectComparatorAdapter) o).mComp.getClass() == mComp.getClass() ) return true; + } + return false; + } + +}