001: package prefuse.util.collections;
002:
003: import java.util.Comparator;
004:
005: /**
006: * Comparator that makes comparison using an ordered list of
007: * individual comparators;
008: *
009: * @author <a href="http://jheer.org">jeffrey heer</a>
010: */
011: public class CompositeComparator implements Comparator {
012:
013: private static final int INCREMENT = 2;
014: private Comparator[] m_cmp;
015: private int m_rev = 1;
016: private int m_size = 0;
017:
018: /**
019: * Creates an empty CompositeComparator with the given capacity.
020: * @param size the starting capacity of this comparator
021: */
022: public CompositeComparator(int size) {
023: this (size, false);
024: }
025:
026: /**
027: * Creates an empty CompositeComparator with the given capacity.
028: * @param size the starting capacity of this comparator
029: * @param reverse when true, reverses the sort order of the included
030: * comparators, when false, objects are sorted as usual
031: */
032: public CompositeComparator(int size, boolean reverse) {
033: m_cmp = new Comparator[size];
034: m_rev = reverse ? -1 : 1;
035: }
036:
037: /**
038: * Creates a new CompositeComparator.
039: * @param cmp the constituent comparators of this composite
040: */
041: public CompositeComparator(Comparator[] cmp) {
042: this (cmp, false);
043: }
044:
045: /**
046: * Creates a new CompositeComparator.
047: * @param cmp the constituent comparators of this composite
048: * @param reverse when true, reverses the sort order of the included
049: * comparators, when false, objects are sorted as usual
050: */
051: public CompositeComparator(Comparator[] cmp, boolean reverse) {
052: this (cmp.length, reverse);
053: System.arraycopy(cmp, 0, m_cmp, 0, cmp.length);
054: m_size = cmp.length;
055: }
056:
057: /**
058: * Adds an additional comparator to this composite.
059: * @param c the Comparator to add
060: */
061: public void add(Comparator c) {
062: if (c == null)
063: return;
064: if (m_cmp.length == m_size) {
065: Comparator[] cmp = new Comparator[m_size + INCREMENT];
066: System.arraycopy(m_cmp, 0, cmp, 0, m_size);
067: m_cmp = cmp;
068: }
069: m_cmp[m_size++] = c;
070: }
071:
072: /**
073: * Removes a comparator from this composite.
074: * @param c the Comparator to remove
075: * @return true if the comparator was successfully removed,
076: * false otherwise
077: */
078: public boolean remove(Comparator c) {
079: for (int i = 0; i < m_size; ++i) {
080: if (m_cmp[i].equals(c)) {
081: System.arraycopy(m_cmp, i + 1, m_cmp, i, m_size - i);
082: --m_size;
083: return true;
084: }
085: }
086: return false;
087: }
088:
089: // ------------------------------------------------------------------------
090:
091: /**
092: * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
093: */
094: public int compare(Object o1, Object o2) {
095: for (int i = 0; i < m_cmp.length; ++i) {
096: int c = m_cmp[i].compare(o1, o2);
097: if (c != 0) {
098: return m_rev * c;
099: }
100: }
101: return 0;
102: }
103:
104: } // end of class CompositeComparator
|