1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  }