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 026 /** 027 * Created: Mon Apr 15 15:56:44 2002 028 */ 029 030 public class BeanComparator extends ComparatorBase { 031 032 private static final Log LOG = LogFactory.getLog(BeanComparator.class); 033 034 private String property; 035 036 037 public BeanComparator(String property) { 038 this.property = property; 039 } 040 041 public BeanComparator(String property, boolean reverse) { 042 super(reverse); 043 this.property = property; 044 } 045 046 public BeanComparator(String property, Comparator comparator) { 047 super(comparator); 048 this.property = property; 049 } 050 051 public BeanComparator(String property, Comparator comparator, boolean reverse) { 052 super(reverse, comparator); 053 this.property = property; 054 } 055 056 /** 057 * 058 * @param param1 <description> 059 * @return <description> 060 */ 061 public boolean equals(Object param1) { 062 if (this == param1) { 063 return true; 064 } 065 if (param1 instanceof BeanComparator) { 066 return ((BeanComparator) param1).getProperty().equals(property) 067 && super.equals(param1); 068 } 069 return false; 070 } 071 072 public int hashCode() { 073 int result; 074 result = (property != null ? property.hashCode() : 0); 075 result = 29 * result + super.hashCode(); 076 return result; 077 } 078 079 // implementation of java.util.Comparator interface 080 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 }