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