View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.util;
21  
22  import org.apache.commons.beanutils.PropertyUtils;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import java.util.Comparator;
27  import java.io.Serializable;
28  
29  public class BeanComparator extends ComparatorBase implements Serializable {
30  
31    private static final long serialVersionUID = -7450094725566090886L;
32  
33    private static final Logger LOG = LoggerFactory.getLogger(BeanComparator.class);
34  
35    private String property;
36  
37    public BeanComparator(final String property) {
38      this.property = property;
39    }
40  
41    public BeanComparator(final String property, final boolean reverse) {
42      super(reverse);
43      this.property = property;
44    }
45  
46    public BeanComparator(final String property, final Comparator comparator) {
47      super(comparator);
48      this.property = property;
49    }
50  
51    public BeanComparator(final String property, final Comparator comparator, final boolean reverse) {
52      super(reverse, comparator);
53      this.property = property;
54    }
55  
56    /**
57     * @param param1 <description>
58     * @return <description>
59     */
60    public boolean equals(final Object param1) {
61      if (this == param1) {
62        return true;
63      }
64      if (param1 instanceof BeanComparator) {
65        return ((BeanComparator) param1).getProperty().equals(property)
66            && super.equals(param1);
67      }
68      return false;
69    }
70  
71    public int hashCode() {
72      int result;
73      result = (property != null ? property.hashCode() : 0);
74      result = 29 * result + super.hashCode();
75      return result;
76    }
77  
78    // implementation of java.util.Comparator interface
79  
80    /**
81     * @param param1 <description>
82     * @param param2 <description>
83     * @return <description>
84     */
85    public int compare(final Object param1, final Object param2) {
86      final Object obj1;
87      final Object obj2;
88      try {
89        obj1 = PropertyUtils.getProperty(param1, property);
90        obj2 = PropertyUtils.getProperty(param2, property);
91  
92      } catch (final Exception e) {
93        LOG.error(e.getMessage(), e);
94        return 0;
95      }
96  
97      return internalCompare(obj1, obj2);
98    }
99  
100   public String getProperty() {
101     return this.property;
102   }
103 }