001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020 package org.apache.myfaces.tobago.util; 021 022 import org.apache.commons.logging.Log; 023 import org.apache.commons.logging.LogFactory; 024 025 import javax.faces.context.FacesContext; 026 import javax.faces.el.ValueBinding; 027 import java.util.Comparator; 028 import java.util.Map; 029 030 /* 031 * Created: Mon Apr 15 15:56:44 2002 032 */ 033 034 public class ValueBindingComparator extends ComparatorBase { 035 036 private static final Log LOG = LogFactory.getLog(ValueBindingComparator.class); 037 038 private FacesContext facesContext; 039 040 private String var; 041 042 private ValueBinding valueBinding; 043 044 public ValueBindingComparator(FacesContext facesContext, String var, ValueBinding valueBinding) { 045 this.facesContext = facesContext; 046 this.var = var; 047 this.valueBinding = valueBinding; 048 } 049 050 public ValueBindingComparator(FacesContext facesContext, String var, ValueBinding valueBinding, boolean reverse) { 051 super(reverse); 052 this.facesContext = facesContext; 053 this.var = var; 054 this.valueBinding = valueBinding; 055 } 056 057 public ValueBindingComparator(FacesContext facesContext, String var, 058 ValueBinding valueBinding, Comparator comparator) { 059 super(comparator); 060 this.facesContext = facesContext; 061 this.var = var; 062 this.valueBinding = valueBinding; 063 } 064 065 public ValueBindingComparator(FacesContext facesContext, String var, 066 ValueBinding valueBinding, boolean reverse, Comparator comparator) { 067 super(reverse, comparator); 068 this.facesContext = facesContext; 069 this.var = var; 070 this.valueBinding = valueBinding; 071 } 072 073 public boolean equals(Object o) { 074 if (this == o) { 075 return true; 076 } 077 if (o == null || getClass() != o.getClass()) { 078 return false; 079 } 080 081 final ValueBindingComparator that = (ValueBindingComparator) o; 082 083 if (!super.equals(o)) { 084 return false; 085 } 086 if (facesContext != null ? !facesContext.equals(that.facesContext) : that.facesContext != null) { 087 return false; 088 } 089 if (valueBinding != null ? !valueBinding.equals(that.valueBinding) : that.valueBinding != null) { 090 return false; 091 } 092 if (var != null ? !var.equals(that.var) : that.var != null) { 093 return false; 094 } 095 096 return true; 097 } 098 099 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 ValueBindingComparator.LOG.error(e.getMessage(), e); 127 return 0; 128 } 129 return super.internalCompare(obj1, obj2); 130 } 131 132 }