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