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