001    package org.apache.myfaces.tobago.util;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.commons.beanutils.PropertyUtils;
021    import org.apache.commons.logging.Log;
022    import org.apache.commons.logging.LogFactory;
023    
024    import java.util.Comparator;
025    import java.io.Serializable;
026    
027    /**
028     * Created: Mon Apr 15 15:56:44 2002
029     */
030    
031    public class BeanComparator extends ComparatorBase implements Serializable {
032    
033      private static final long serialVersionUID = -7450094725566090886L;
034    
035      private static final Log LOG = LogFactory.getLog(BeanComparator.class);
036    
037      private String property;
038    
039      public BeanComparator(String property) {
040        this.property = property;
041      }
042    
043      public BeanComparator(String property, boolean reverse) {
044        super(reverse);
045        this.property = property;
046      }
047    
048      public BeanComparator(String property, Comparator comparator) {
049        super(comparator);
050        this.property = property;
051      }
052    
053      public BeanComparator(String property, Comparator comparator, boolean reverse) {
054        super(reverse, comparator);
055        this.property = property;
056      }
057    
058      /**
059       *
060       * @param param1 <description>
061       * @return <description>
062       */
063      public boolean equals(Object param1) {
064        if (this == param1) {
065          return true;
066        }
067        if (param1 instanceof BeanComparator) {
068          return ((BeanComparator) param1).getProperty().equals(property)
069              && super.equals(param1);
070        }
071        return false;
072      }
073    
074      public int hashCode() {
075        int result;
076        result = (property != null ? property.hashCode() : 0);
077        result = 29 * result + super.hashCode();
078        return result;
079      }
080    
081      // implementation of java.util.Comparator interface
082    
083      /**
084       *
085       * @param param1 <description>
086       * @param param2 <description>
087       * @return <description>
088       */
089      public int compare(Object param1, Object param2){
090        Object obj1;
091        Object obj2;
092        try {
093          obj1 = PropertyUtils.getProperty(param1, property);
094          obj2 = PropertyUtils.getProperty(param2, property);
095    
096        } catch (Exception e) {
097          LOG.error(e.getMessage(), e);
098          return 0;
099        }
100    
101        return internalCompare(obj1, obj2);
102      }
103    
104      public String getProperty() {
105        return this.property;
106      }
107    }