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