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="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  }