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 * @param param1 <description> 060 * @return <description> 061 */ 062 public boolean equals(Object param1) { 063 if (this == param1) { 064 return true; 065 } 066 if (param1 instanceof BeanComparator) { 067 return ((BeanComparator) param1).getProperty().equals(property) 068 && super.equals(param1); 069 } 070 return false; 071 } 072 073 public int hashCode() { 074 int result; 075 result = (property != null ? property.hashCode() : 0); 076 result = 29 * result + super.hashCode(); 077 return result; 078 } 079 080 // implementation of java.util.Comparator interface 081 082 /** 083 * @param param1 <description> 084 * @param param2 <description> 085 * @return <description> 086 */ 087 public int compare(Object param1, Object param2) { 088 Object obj1; 089 Object obj2; 090 try { 091 obj1 = PropertyUtils.getProperty(param1, property); 092 obj2 = PropertyUtils.getProperty(param2, property); 093 094 } catch (Exception e) { 095 LOG.error(e.getMessage(), e); 096 return 0; 097 } 098 099 return internalCompare(obj1, obj2); 100 } 101 102 public String getProperty() { 103 return this.property; 104 } 105 }