001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.lang.reflect.InvocationTargetException;
021    
022    import java.util.Comparator;
023    
024    import org.apache.commons.beanutils.PropertyUtils;
025    
026    /**
027     * @author Patrick Brady
028     * @author Raymond Augé
029     */
030    public class PropertyComparator implements Comparator<Object> {
031    
032            public PropertyComparator(String propertyName) {
033                    this(new String[] {propertyName}, true, false);
034            }
035    
036            public PropertyComparator(String[] propertyNames) {
037                    this(propertyNames, true, false);
038            }
039    
040            public PropertyComparator(
041                    String propertyName, boolean ascending, boolean caseSensitive) {
042    
043                    this(new String[] {propertyName}, ascending, caseSensitive);
044            }
045    
046            public PropertyComparator(
047                    String[] propertyNames, boolean ascending, boolean caseSensitive) {
048    
049                    _propertyNames = propertyNames;
050                    _ascending = ascending;
051                    _caseSensitive = caseSensitive;
052            }
053    
054            public int compare(Object obj1, Object obj2) {
055                    try {
056                            for (String propertyName : _propertyNames) {
057                                    Object property1 = PropertyUtils.getProperty(
058                                            obj1, propertyName);
059                                    Object property2 = PropertyUtils.getProperty(
060                                            obj2, propertyName);
061    
062                                    if (!_ascending) {
063                                            Object temp = property1;
064    
065                                            property1 = property2;
066                                            property2 = temp;
067                                    }
068    
069                                    if (property1 instanceof String) {
070                                            int result = 0;
071    
072                                            if (_caseSensitive) {
073                                                    result = property1.toString().compareTo(
074                                                            property2.toString());
075                                            }
076                                            else {
077                                                    result = property1.toString().compareToIgnoreCase(
078                                                            property2.toString());
079                                            }
080    
081                                            if (result != 0) {
082                                                    return result;
083                                            }
084                                    }
085    
086                                    if (property1 instanceof Comparable<?>) {
087                                            int result = ((Comparable<Object>)property1).compareTo(
088                                                    property2);
089    
090                                            if (result != 0) {
091                                                    return result;
092                                            }
093                                    }
094                            }
095                    }
096                    catch (IllegalAccessException iae) {
097                            _log.error(iae.getMessage());
098                    }
099                    catch (InvocationTargetException ite) {
100                            _log.error(ite.getMessage());
101                    }
102                    catch (NoSuchMethodException nsme) {
103                            _log.error(nsme.getMessage());
104                    }
105    
106                    return -1;
107            }
108    
109            private static Log _log = LogFactoryUtil.getLog(PropertyComparator.class);
110    
111            private String[] _propertyNames;
112            private boolean _ascending;
113            private boolean _caseSensitive;
114    
115    }