com.discovery.darchrow.util.comparator.PropertyComparator.java Source code

Java tutorial

Introduction

Here is the source code for com.discovery.darchrow.util.comparator.PropertyComparator.java

Source

/*
 * Copyright (C) 2008 feilong
 *
 * Licensed 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.
 */
package com.discovery.darchrow.util.comparator;

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

import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.discovery.darchrow.bean.PropertyUtil;

/**
 * ,? <code>T</code>?? {@link #propertyName},,???? {@link Comparator}.
 * 
 * <h3> {@link #propertyName}:</h3>
 * 
 * <blockquote>
 * <p>
 * {@link #propertyName}??, {@link Comparable}?,  {@link Integer}, {@link String}
 * </p>
 * </blockquote>
 * 
 * <h3>?:</h3>
 * 
 * <blockquote>
 * <p>
 * <span style="color:red">?</span>?,????,? {@link org.apache.commons.collections.comparators.ReverseComparator}
 * </p>
 * </blockquote>
 *
 * @author feilong
 * @version 1.2.0 2015521 ?11:02:42
 * @param <T>
 *            the generic type
 * @see "org.springframework.beans.support.PropertyComparator"
 * @see org.apache.commons.collections.comparators.BooleanComparator
 * @see org.apache.commons.collections.comparators.ReverseComparator
 * @see org.apache.commons.collections.comparators.ComparableComparator
 * @since 1.2.0
 */
public class PropertyComparator<T> implements Comparator<T>, Serializable {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = -3159374167882773300L;

    /** The Constant LOGGER. */
    private static final Logger LOGGER = LoggerFactory.getLogger(PropertyComparator.class);

    /** T??,value  {@link Comparable}?. */
    private final String propertyName;

    /**
     * The Constructor.
     *
     * @param propertyName
     *            T??,value  {@link Comparable}?.
     */
    public PropertyComparator(String propertyName) {
        super();
        this.propertyName = propertyName;
        LOGGER.info("propertyName:[{}]", propertyName);
    }

    /**
     * Compare.
     *
     * @param t1
     *            the t1
     * @param t2
     *            the t2
     * @return the int
     * @see org.apache.commons.lang3.ObjectUtils#compare(Comparable, Comparable)
     * @see org.apache.commons.lang3.ObjectUtils#compare(Comparable, Comparable, boolean)
     */
    @Override
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public int compare(T t1, T t2) {
        if (t1 == t2) {
            return 0;
        } else if (null == t1) {
            return 1;
        } else if (null == t2) {
            return -1;
        }

        Comparable propertyValue1 = PropertyUtil.getProperty(t1, propertyName);
        Comparable propertyValue2 = PropertyUtil.getProperty(t2, propertyName);

        int compareTo = ObjectUtils.compare(propertyValue1, propertyValue2);

        if (0 == compareTo) {
            //??TreeSet / TreeMap ?sort??
            compareTo = ObjectUtils.compare(t1.hashCode(), t2.hashCode());
        }

        //NullPointException
        //propertyValue1.compareTo(propertyValue2);

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("propertyName:[{}],propertyValue1:[{}],propertyValue2:[{}],compareTo:[{}]", propertyName,
                    propertyValue1, propertyValue2, compareTo);
        }
        return compareTo;
    }
}