From 708ada3e99302cb855083e62cced2e1a10aeba36 Mon Sep 17 00:00:00 2001 From: asivache Date: Wed, 18 Mar 2009 21:45:54 +0000 Subject: [PATCH] an accessory for CountedObject: builds a comparator for CountedObject 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 --- .../CountedObjectComparatorAdapter.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100755 playground/java/src/org/broadinstitute/sting/indels/CountedObjectComparatorAdapter.java 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; + } + +}