All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.myfaces.tobago.util.BeanComparator Maven / Gradle / Ivy

Go to download

Tobago JSF 1.1-1.2 Compatibility Bridge (makes Tobago independent from differences between JSF 1.1 and JSF 1.2).

There is a newer version: 1.6.0-beta-2
Show newest version
package org.apache.myfaces.tobago.util;

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Comparator;
import java.io.Serializable;

/**
 * Created: Mon Apr 15 15:56:44 2002
 */

public class BeanComparator extends ComparatorBase implements Serializable {

  private static final long serialVersionUID = -7450094725566090886L;

  private static final Logger LOG = LoggerFactory.getLogger(BeanComparator.class);

  private String property;

  public BeanComparator(String property) {
    this.property = property;
  }

  public BeanComparator(String property, boolean reverse) {
    super(reverse);
    this.property = property;
  }

  public BeanComparator(String property, Comparator comparator) {
    super(comparator);
    this.property = property;
  }

  public BeanComparator(String property, Comparator comparator, boolean reverse) {
    super(reverse, comparator);
    this.property = property;
  }

  /**
   * @param param1 
   * @return 
   */
  public boolean equals(Object param1) {
    if (this == param1) {
      return true;
    }
    if (param1 instanceof BeanComparator) {
      return ((BeanComparator) param1).getProperty().equals(property)
          && super.equals(param1);
    }
    return false;
  }

  public int hashCode() {
    int result;
    result = (property != null ? property.hashCode() : 0);
    result = 29 * result + super.hashCode();
    return result;
  }

  // implementation of java.util.Comparator interface

  /**
   * @param param1 
   * @param param2 
   * @return 
   */
  public int compare(Object param1, Object param2) {
    Object obj1;
    Object obj2;
    try {
      obj1 = PropertyUtils.getProperty(param1, property);
      obj2 = PropertyUtils.getProperty(param2, property);

    } catch (Exception e) {
      LOG.error(e.getMessage(), e);
      return 0;
    }

    return internalCompare(obj1, obj2);
  }

  public String getProperty() {
    return this.property;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy