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.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  
20  import java.lang.reflect.InvocationTargetException;
21  
22  import java.util.Comparator;
23  
24  import org.apache.commons.beanutils.PropertyUtils;
25  
26  /**
27   * <a href="PropertyComparator.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Patrick Brady
30   * @author Raymond Augé
31   */
32  public class PropertyComparator implements Comparator<Object> {
33  
34      public PropertyComparator(String propertyName) {
35          this(new String[] {propertyName}, true, false);
36      }
37  
38      public PropertyComparator(String[] propertyNames) {
39          this(propertyNames, true, false);
40      }
41  
42      public PropertyComparator(
43          String propertyName, boolean asc, boolean caseSensitive) {
44  
45          this(new String[] {propertyName}, asc, caseSensitive);
46      }
47  
48      public PropertyComparator(
49          String[] propertyNames, boolean asc, boolean caseSensitive) {
50  
51          _propertyNames = propertyNames;
52          _asc = asc;
53          _caseSensitive = caseSensitive;
54      }
55  
56      public int compare(Object obj1, Object obj2) {
57          try {
58              for (String propertyName : _propertyNames) {
59                  Object property1 = PropertyUtils.getProperty(
60                      obj1, propertyName);
61                  Object property2 = PropertyUtils.getProperty(
62                      obj2, propertyName);
63  
64                  if (!_asc) {
65                      Object temp = property1;
66  
67                      property1 = property2;
68                      property2 = temp;
69                  }
70  
71                  if (property1 instanceof String) {
72                      int result = 0;
73  
74                      if (_caseSensitive) {
75                          result = property1.toString().compareTo(
76                              property2.toString());
77                      }
78                      else {
79                          result = property1.toString().compareToIgnoreCase(
80                              property2.toString());
81                      }
82  
83                      if (result != 0) {
84                          return result;
85                      }
86                  }
87  
88                  if (property1 instanceof Comparable<?>) {
89                      int result = ((Comparable<Object>)property1).compareTo(
90                          property2);
91  
92                      if (result != 0) {
93                          return result;
94                      }
95                  }
96              }
97          }
98          catch (IllegalAccessException iae) {
99              _log.error(iae.getMessage());
100         }
101         catch (InvocationTargetException ite) {
102             _log.error(ite.getMessage());
103         }
104         catch (NoSuchMethodException nsme) {
105             _log.error(nsme.getMessage());
106         }
107 
108         return -1;
109     }
110 
111     private static Log _log = LogFactoryUtil.getLog(PropertyComparator.class);
112 
113     private String[] _propertyNames;
114     private boolean _asc;
115     private boolean _caseSensitive;
116 
117 }