1
14
15 package com.liferay.portal.kernel.util;
16
17 import java.util.Comparator;
18
19
24 public class ObjectValuePairComparator<K, V>
25 implements Comparator<ObjectValuePair<K, V>> {
26
27 public ObjectValuePairComparator() {
28 this(true);
29 }
30
31 public ObjectValuePairComparator(boolean ascending) {
32 this(true, ascending);
33 }
34
35 public ObjectValuePairComparator(boolean byKey, boolean ascending) {
36 _byKey = byKey;
37 _ascending = ascending;
38 }
39
40 public int compare(ObjectValuePair<K, V> ovp1, ObjectValuePair<K, V> ovp2) {
41 if (_byKey) {
42 Comparable<K> key1 = (Comparable<K>)ovp1.getKey();
43 Comparable<K> key2 = (Comparable<K>)ovp2.getKey();
44
45 if (_ascending) {
46 return key1.compareTo((K)key2);
47 }
48 else {
49 return -(key1.compareTo((K)key2));
50 }
51 }
52 else {
53 Comparable<V> value1 = (Comparable<V>)ovp1.getValue();
54 Comparable<V> value2 = (Comparable<V>)ovp2.getValue();
55
56 if (_ascending) {
57 return value1.compareTo((V)value2);
58 }
59 else {
60 return -(value1.compareTo((V)value2));
61 }
62 }
63 }
64
65 private boolean _byKey;
66 private boolean _ascending;
67
68 }