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 javax.faces.context.FacesContext;
26  import javax.faces.el.ValueBinding;
27  import java.util.Comparator;
28  import java.util.Map;
29  
30  /**
31   * @deprecated Since 2.0.0
32   */
33  @Deprecated
34  public class ValueBindingComparator extends ComparatorBase {
35  
36    private static final Logger LOG = LoggerFactory.getLogger(ValueBindingComparator.class);
37  
38    private FacesContext facesContext;
39  
40    private String var;
41  
42    private ValueBinding valueBinding;
43  
44    public ValueBindingComparator(FacesContext facesContext, String var, ValueBinding valueBinding) {
45      this.facesContext = facesContext;
46      this.var = var;
47      this.valueBinding = valueBinding;
48    }
49  
50    public ValueBindingComparator(FacesContext facesContext, String var, ValueBinding valueBinding, boolean reverse) {
51      super(reverse);
52      this.facesContext = facesContext;
53      this.var = var;
54      this.valueBinding = valueBinding;
55    }
56  
57    public ValueBindingComparator(FacesContext facesContext, String var,
58        ValueBinding valueBinding, Comparator comparator) {
59      super(comparator);
60      this.facesContext = facesContext;
61      this.var = var;
62      this.valueBinding = valueBinding;
63    }
64  
65    public ValueBindingComparator(FacesContext facesContext, String var,
66        ValueBinding valueBinding, boolean reverse, Comparator comparator) {
67      super(reverse, comparator);
68      this.facesContext = facesContext;
69      this.var = var;
70      this.valueBinding = valueBinding;
71    }
72  
73    public boolean equals(Object o) {
74      if (this == o) {
75        return true;
76      }
77      if (o == null || getClass() != o.getClass()) {
78        return false;
79      }
80  
81      final ValueBindingComparator that = (ValueBindingComparator) o;
82  
83      if (!super.equals(o)) {
84        return false;
85      }
86      if (facesContext != null ? !facesContext.equals(that.facesContext) : that.facesContext != null) {
87        return false;
88      }
89      if (valueBinding != null ? !valueBinding.equals(that.valueBinding) : that.valueBinding != null) {
90        return false;
91      }
92      if (var != null ? !var.equals(that.var) : that.var != null) {
93        return false;
94      }
95  
96      return true;
97    }
98  
99    public int hashCode() {
100     int result;
101     result = (facesContext != null ? facesContext.hashCode() : 0);
102     result = 29 * result + (var != null ? var.hashCode() : 0);
103     result = 29 * result + (valueBinding != null ? valueBinding.hashCode() : 0);
104     result = 29 * result + super.hashCode();
105     return result;
106   }
107 
108   // implementation of java.util.Comparator interface
109 
110   /**
111    * @param param1 <description>
112    * @param param2 <description>
113    * @return <description>
114    */
115   public int compare(Object param1, Object param2) {
116     Object obj1;
117     Object obj2;
118     try {
119       final Map requestMap = facesContext.getExternalContext().getRequestMap();
120       requestMap.put(var, param1);
121       obj1 = valueBinding.getValue(facesContext);
122       requestMap.put(var, param2);
123       obj2 = valueBinding.getValue(facesContext);
124 
125     } catch (Exception e) {
126       LOG.error(e.getMessage(), e);
127       return 0;
128     }
129     return super.internalCompare(obj1, obj2);
130   }
131 
132 }