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