1 package org.jahia.content.comparators; 2 3 import java.util.Comparator ; 4 import java.util.ArrayList ; 5 import java.util.Iterator ; 6 7 20 21 public class OrderedCompositeComparator implements Comparator { 22 23 private static org.apache.log4j.Logger logger = 24 org.apache.log4j.Logger.getLogger(OrderedCompositeComparator.class); 25 26 ArrayList orderedComparators = new ArrayList (); 27 28 public OrderedCompositeComparator() { 29 } 30 31 public void addComparator(Comparator comparator) { 32 orderedComparators.add(comparator); 33 } 34 35 public void addComparator(int index, Comparator comparator) { 36 orderedComparators.add(index, comparator); 37 } 38 39 public int compare(Object o1, Object o2) { 40 if (orderedComparators.size() < 1) { 41 logger.warn("No configured comparators in ordered comparator, returning objects by toString comparison"); 42 return o1.toString().compareTo(o2.toString()); 43 } 44 Iterator comparatorIter = orderedComparators.iterator(); 45 int curCompareResult = 0; 46 while (comparatorIter.hasNext()) { 47 Comparator curComparator = (Comparator ) comparatorIter.next(); 48 curCompareResult = curComparator.compare(o1, o2); 49 if (curCompareResult != 0) { 50 return curCompareResult; 51 } 52 } 53 return curCompareResult; 54 } 55 56 public boolean equals(Object obj) { 57 if (obj instanceof OrderedCompositeComparator) { 58 OrderedCompositeComparator otherComparator = (OrderedCompositeComparator) obj; 59 return orderedComparators.equals(otherComparator.orderedComparators); 60 } else { 61 return false; 62 } 63 } 64 65 } | Popular Tags |