1 9 package com.vladium.emma.report; 10 11 import java.util.Comparator ; 12 13 17 public 18 interface ItemComparator extends Comparator 19 { 20 22 23 ItemComparator NULL_COMPARATOR = new Factory.NullComparator (); 24 25 26 abstract class Factory 27 { 28 public static ItemComparator create (final int [] attributeIDsWithDir, final int unitsID) 29 { 30 if (attributeIDsWithDir == null) 31 throw new IllegalArgumentException ("null input: attributeIDsWithDir"); 32 33 if (attributeIDsWithDir.length == 0) 34 return NULL_COMPARATOR; 35 36 39 final Comparator [] comparators = new Comparator [attributeIDsWithDir.length >> 1]; 41 for (int a = 0; a < attributeIDsWithDir.length; a += 2) 42 { 43 final int attributeID = attributeIDsWithDir [a]; 44 45 final Comparator comparator = IItemAttribute.Factory.getAttribute (attributeID, unitsID).comparator (); 46 comparators [a >> 1] = attributeIDsWithDir [a + 1] < 0 ? new ReverseComparator (comparator) : comparator; 47 } 48 49 return new CompositeComparator (comparators); 50 } 51 52 private static final class NullComparator implements ItemComparator 53 { 54 public int compare (final Object l, final Object g) 55 { 56 return 0; 57 } 58 59 } 61 62 private static final class ReverseComparator implements ItemComparator 63 { 64 public int compare (final Object l, final Object g) 65 { 66 return m_comparator.compare (g, l); 67 } 68 69 70 ReverseComparator (final Comparator comparator) 71 { 72 m_comparator = comparator; 73 } 74 75 76 private final Comparator m_comparator; 77 78 } 80 81 private static final class CompositeComparator implements ItemComparator 82 { 83 public int compare (final Object l, final Object g) 84 { 85 87 for (int c = 0; c < m_comparators.length; ++ c) 88 { 89 final int diff = m_comparators [c].compare (l, g); 90 if (diff != 0) return diff; 91 } 92 93 return 0; 94 } 95 96 97 CompositeComparator (final Comparator [] comparators) 98 { 99 m_comparators = comparators; 100 } 101 102 103 private final Comparator [] m_comparators; 104 105 } 107 } 109 } | Popular Tags |