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="ObjectValuePairComparator.java.html"><b><i>View Source</i></b></a>
21   *
22   * @author Brian Wing Shun Chan
23   */
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  }