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