| KeyValuePairComparator.java |
1 /**
2 * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3 *
4 * The contents of this file are subject to the terms of the Liferay Enterprise
5 * Subscription License ("License"). You may not use this file except in
6 * compliance with the License. You can obtain a copy of the License by
7 * contacting Liferay, Inc. See the License for the specific language governing
8 * permissions and limitations under the License, including but not limited to
9 * distribution rights of the Software.
10 *
11 *
12 *
13 */
14
15 package com.liferay.portal.kernel.util;
16
17 import java.util.Comparator;
18
19 /**
20 * <a href="KeyValuePairComparator.java.html"><b><i>View Source</i></b></a>
21 *
22 * @author Brian Wing Shun Chan
23 */
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 }