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.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import java.beans.PropertyDescriptor;
26  import java.io.Serializable;
27  import java.util.Comparator;
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    /**
38     * @deprecated since 4.0.0
39     */
40    @Deprecated
41    public BeanComparator(final String property) {
42      this.property = property;
43    }
44  
45    /**
46     * @deprecated since 4.0.0
47     */
48    @Deprecated
49    public BeanComparator(final String property, final boolean reverse) {
50      super(reverse);
51      this.property = property;
52    }
53  
54    /**
55     * @deprecated since 4.0.0
56     */
57    @Deprecated
58    public BeanComparator(final String property, final Comparator comparator) {
59      super(comparator);
60      this.property = property;
61    }
62  
63    public BeanComparator(final String property, final Comparator comparator, final boolean reverse) {
64      super(reverse, comparator);
65      this.property = property;
66    }
67  
68    public boolean equals(final Object param1) {
69      if (this == param1) {
70        return true;
71      }
72      if (param1 instanceof BeanComparator) {
73        return ((BeanComparator) param1).getProperty().equals(property)
74            && super.equals(param1);
75      }
76      return false;
77    }
78  
79    public int hashCode() {
80      int result;
81      result = property != null ? property.hashCode() : 0;
82      result = 29 * result + super.hashCode();
83      return result;
84    }
85  
86    @Override
87    public int compare(final Object param1, final Object param2) {
88      final Object obj1;
89      final Object obj2;
90      try {
91        obj1 = new PropertyDescriptor(property, param1.getClass()).getReadMethod().invoke(param1);
92        obj2 = new PropertyDescriptor(property, param1.getClass()).getReadMethod().invoke(param2);
93      } catch (final Exception e) {
94        LOG.error(e.getMessage(), e);
95        return 0;
96      }
97  
98      return internalCompare(obj1, obj2);
99    }
100 
101   public String getProperty() {
102     return this.property;
103   }
104 }