001
014
015 package com.liferay.portal.kernel.util;
016
017 import java.util.Comparator;
018
019
022 public class ObjectValuePairComparator<K, V>
023 implements Comparator<ObjectValuePair<K, V>> {
024
025 public ObjectValuePairComparator() {
026 this(true);
027 }
028
029 public ObjectValuePairComparator(boolean ascending) {
030 this(true, ascending);
031 }
032
033 public ObjectValuePairComparator(boolean byKey, boolean ascending) {
034 _byKey = byKey;
035 _ascending = ascending;
036 }
037
038 public int compare(ObjectValuePair<K, V> ovp1, ObjectValuePair<K, V> ovp2) {
039 if (_byKey) {
040 Comparable key1 = (Comparable)ovp1.getKey();
041 Comparable key2 = (Comparable)ovp2.getKey();
042
043 if (_ascending) {
044 return key1.compareTo(key2);
045 }
046 else {
047 return -(key1.compareTo(key2));
048 }
049 }
050 else {
051 Comparable value1 = (Comparable)ovp1.getValue();
052 Comparable value2 = (Comparable)ovp2.getValue();
053
054 if (_ascending) {
055 return value1.compareTo(value2);
056 }
057 else {
058 return -(value1.compareTo(value2));
059 }
060 }
061 }
062
063 private boolean _byKey;
064 private boolean _ascending;
065
066 }