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 asc) {
32 this(true, asc);
33 }
34
35 public ObjectValuePairComparator(boolean byKey, boolean asc) {
36 _byKey = byKey;
37 _asc = asc;
38 }
39
40 public int compare(ObjectValuePair<K, V> ovp1, ObjectValuePair<K, V> ovp2) {
41 if (_byKey) {
42 Comparable key1 = (Comparable)ovp1.getKey();
43 Comparable key2 = (Comparable)ovp2.getKey();
44
45 if (_asc) {
46 return key1.compareTo(key2);
47 }
48 else {
49 return -(key1.compareTo(key2));
50 }
51 }
52 else {
53 Comparable value1 = (Comparable)ovp1.getValue();
54 Comparable value2 = (Comparable)ovp2.getValue();
55
56 if (_asc) {
57 return value1.compareTo(value2);
58 }
59 else {
60 return -(value1.compareTo(value2));
61 }
62 }
63 }
64
65 private boolean _byKey;
66 private boolean _asc;
67
68 }