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