an accessory for CountedObject: builds a comparator for CountedObject<T> given a comparator for T; compares the underlying objects T themselves, *not* the associated counters

git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@91 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
asivache 2009-03-18 21:45:54 +00:00
parent 37101045af
commit 708ada3e99
1 changed files with 41 additions and 0 deletions

View File

@ -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<T>
* (the underlying Comparator<T> 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<T> and "raw" object of type T (the same underlying Comparator<T> will be used,
* and the value of the counter in the counted object wil be ignored).
* @param <T>
*/
public class CountedObjectComparatorAdapter<T> implements java.util.Comparator<CountedObject<T>> {
private java.util.Comparator<T> mComp;
/** Initializes comparator adapter with a comparator for objects of trype T */
public CountedObjectComparatorAdapter(java.util.Comparator<T> adaptee) {
mComp = adaptee;
}
@Override
public int compare(CountedObject<T> o1, CountedObject<T> o2) {
return mComp.compare(o1.getObject(),o2.getObject());
}
public int compare(T o1, CountedObject<T> o2) {
return mComp.compare(o1,o2.getObject());
}
public int compare(CountedObject<T> 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;
}
}