com.feilong.core.util.comparator.BeanComparatorUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.feilong.core.util.comparator.BeanComparatorUtil.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.feilong.core.util.comparator;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections4.ComparatorUtils;
import org.apache.commons.collections4.comparators.FixedOrderComparator;
import org.apache.commons.lang3.Validate;

/**
 *  bean ?.
 *
 * @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
 * @since 1.8.0
 */
public final class BeanComparatorUtil {

    /** Don't let anyone instantiate this class. */
    private BeanComparatorUtil() {
        //AssertionError?. ?????. ???.
        //see Effective Java 2nd
        throw new AssertionError("No " + getClass().getName() + " instances for you!");
    }

    //*************************************************************************************************

    /**
     * Chained comparator.
     *
     * @param <T>
     *            the generic type
     * @param propertyNames
     *            T??,Possibly indexed and/or nested name of the property to be modified,??
     *            <a href="../../bean/BeanUtil.html#propertyName">propertyName</a>,value  {@link Comparable}?.
     * @return the comparator
     * @throws NullPointerException
     *              <code>propertyNames</code> null, null
     * @throws IllegalArgumentException
     *              <code>propertyNames</code> empty, blank
     * @see org.apache.commons.collections4.ComparatorUtils#chainedComparator(java.util.Collection)
     */
    public static <T> Comparator<T> chainedComparator(String... propertyNames) {
        Validate.notEmpty(propertyNames, "propertyNames can't be null/empty!");

        List<Comparator<T>> comparators = new ArrayList<>();
        for (String propertyName : propertyNames) {
            Validate.notBlank(propertyName, "propertyName can't be blank!");

            //??? propertyComparator(propertyName)  PropertyComparator
            //, PropertyComparator ?  hashcode(map), ? 
            comparators.add(new BeanComparator<T>(propertyName));
        }
        return ComparatorUtils.chainedComparator(comparators);
    }

    /**
     * Property comparator.
     *
     * @param <T>
     *            the generic type
     * @param propertyName
     *            T??,Possibly indexed and/or nested name of the property to be modified,??
     *            <a href="../../bean/BeanUtil.html#propertyName">propertyName</a>,value  {@link Comparable}?.
     * @return the comparator
     * @throws NullPointerException
     *              <code>propertyName</code> null
     * @throws IllegalArgumentException
     *              <code>propertyName</code> blank
     * @see PropertyComparator#PropertyComparator(String)
     */
    public static <T> Comparator<T> propertyComparator(String propertyName) {
        Validate.notBlank(propertyName, "propertyName can't be blank!");
        return new PropertyComparator<>(propertyName);
    }

    /**
     * Property comparator.
     *
     * @param <V>
     *            the value type
     * @param <T>
     *            the generic type
     * @param propertyName
     *            T??,Possibly indexed and/or nested name of the property to be modified,??
     *            <a href="../../bean/BeanUtil.html#propertyName">propertyName</a>,value  {@link Comparable}?.
     * @param propertyValues
     *            the property values
     * @return the comparator
     * @throws NullPointerException
     *              <code>propertyName</code> null,<code>propertyValues</code> null
     * @throws IllegalArgumentException
     *              <code>propertyName</code> blank
     * @see PropertyComparator#PropertyComparator(String, Comparator)
     * @see FixedOrderComparator#FixedOrderComparator(Object...)
     */
    @SafeVarargs
    public static <V, T> Comparator<T> propertyComparator(String propertyName, V... propertyValues) {
        Validate.notBlank(propertyName, "propertyName can't be blank!");
        Validate.notNull(propertyValues, "propertyValues can't be null!");
        return new PropertyComparator<>(propertyName, new FixedOrderComparator<>(propertyValues));
    }

    /**
     * Property comparator.
     *
     * @param <V>
     *            the value type
     * @param <T>
     *            the generic type
     * @param propertyName
     *            T??,Possibly indexed and/or nested name of the property to be modified,??
     *            <a href="../../bean/BeanUtil.html#propertyName">propertyName</a>,value  {@link Comparable}?.
     * @param propertyValues
     *            the property values
     * @return the comparator
     * @throws NullPointerException
     *              <code>propertyName</code> null,<code>propertyValues</code> null
     * @throws IllegalArgumentException
     *              <code>propertyName</code> blank
     * @see PropertyComparator#PropertyComparator(String, Comparator)
     * @see FixedOrderComparator#FixedOrderComparator(List)
     */
    public static <V, T> Comparator<T> propertyComparator(String propertyName, List<V> propertyValues) {
        Validate.notBlank(propertyName, "propertyName can't be blank!");
        Validate.notNull(propertyValues, "propertyValues can't be null!");
        return new PropertyComparator<>(propertyName, new FixedOrderComparator<>(propertyValues));
    }
}