com.feilong.core.lang.EnumUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.feilong.core.lang.EnumUtil.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.lang;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feilong.core.bean.BeanUtilException;
import com.feilong.core.bean.PropertyUtil;
import com.feilong.tools.jsonlib.JsonUtil;
import com.feilong.tools.slf4j.Slf4jUtil;

/**
 * {@link java.lang.Enum} .
 * 
 * @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
 * @see org.apache.commons.lang3.EnumUtils
 * @since 1.0.6
 */
public final class EnumUtil {

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

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

    /**
     * <code>propertyName</code> <code>specifiedValue</code>(?) (equalsIgnoreCase).
     * 
     * <p>
     * ? <code>HttpMethodType</code> ,?:
     * </p>
     * 
     * <pre class="code">
     * EnumUtil.getEnumByField(HttpMethodType.class, "method", "get")
     * </pre>
     * 
     * <h3>:</h3>
     * 
     * <blockquote>
     * <p>
     * ??,?<code>propertyName</code> <code>specifiedValue</code>,null
     * </p>
     * </blockquote>
     *
     * @param <E>
     *            the element type
     * @param <T>
     *            the generic type
     * @param enumClass
     *              <code>HttpMethodType</code>
     * @param propertyName
     *            ??, <code>HttpMethodType</code> method,javabean 
     * @param specifiedValue
     *             post
     * @return the enum by property value ignore case
     * @throws NullPointerException
     *              <code>enumClass</code> null, <code>propertyName</code> null
     * @throws IllegalArgumentException
     *              <code>propertyName</code> blank
     * @throws BeanUtilException
     *              <code>propertyName</code> , <code>HttpMethodType</code>  <b>"method"</b> , <b>"method2222"</b> 
     * @see #getEnumByPropertyValue(Class, String, Object, boolean)
     */
    public static <E extends Enum<?>, T> E getEnumByPropertyValueIgnoreCase(Class<E> enumClass, String propertyName,
            T specifiedValue) {
        return getEnumByPropertyValue(enumClass, propertyName, specifiedValue, true);
    }

    /**
     * <code>propertyName</code> <code>specifiedValue</code> (equals,?).
     * 
     * <p>
     * ? <code>HttpMethodType</code> ,?:
     * </p>
     * 
     * <pre class="code">
     * EnumUtil.getEnumByField(HttpMethodType.class, "method", "get")
     * </pre>
     * 
     * <h3>:</h3>
     * 
     * <blockquote>
     * <p>
     * ??,?<code>propertyName</code> <code>specifiedValue</code>,null
     * </p>
     * </blockquote>
     *
     * @param <E>
     *            the element type
     * @param <T>
     *            the generic type
     * @param enumClass
     *              <code>HttpMethodType</code>
     * @param propertyName
     *            ??, <code>HttpMethodType</code> method,javabean 
     * @param specifiedValue
     *             post
     * @return the enum by property value
     * @throws NullPointerException
     *              <code>enumClass</code> null, <code>propertyName</code> null
     * @throws IllegalArgumentException
     *              <code>propertyName</code> blank
     * @throws BeanUtilException
     *              <code>propertyName</code> , <code>HttpMethodType</code>  <b>"method"</b> , <b>"method2222"</b> 
     * 
     * 
     * @since 1.0.8
     */
    public static <E extends Enum<?>, T> E getEnumByPropertyValue(Class<E> enumClass, String propertyName,
            T specifiedValue) {
        return getEnumByPropertyValue(enumClass, propertyName, specifiedValue, false);
    }

    /**
     * <code>propertyName</code> <code>specifiedValue</code> .
     * 
     * <p>
     *  <code>HttpMethodType</code> ?,?:
     * </p>
     * 
     * <pre class="code">
     * EnumUtil.getEnumByField(HttpMethodType.class, "method", "get")
     * </pre>
     * 
     * <h3>:</h3>
     * 
     * <blockquote>
     * <p>
     * ??,?<code>propertyName</code> <code>specifiedValue</code>,null
     * </p>
     * </blockquote>
     *
     * @param <E>
     *            the element type
     * @param <T>
     *            the generic type
     * @param enumClass
     *              <code>HttpMethodType</code>
     * @param propertyName
     *            ??, <code>HttpMethodType</code> method,javabean 
     * @param specifiedValue
     *             post
     * @param ignoreCase
     *            ??
     * @return the enum by property value
     * @throws NullPointerException
     *              <code>enumClass</code> null, <code>propertyName</code> null
     * @throws IllegalArgumentException
     *              <code>propertyName</code> blank
     * @throws BeanUtilException
     *              <code>propertyName</code> , <code>HttpMethodType</code>  <b>"method"</b> , <b>"method2222"</b> 
     * @see com.feilong.core.bean.PropertyUtil#getProperty(Object, String)
     * @since 1.0.8
     */
    private static <E extends Enum<?>, T> E getEnumByPropertyValue(Class<E> enumClass, String propertyName,
            T specifiedValue, boolean ignoreCase) {
        Validate.notNull(enumClass, "enumClass can't be null!");
        Validate.notBlank(propertyName, "propertyName can't be null/empty!");

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

        // An enum is a kind of class
        // An annotation is a kind of interface

        // Class ?, null.
        E[] enumConstants = enumClass.getEnumConstants();

        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("enumClass:[{}],enumConstants:{}", enumClass.getCanonicalName(),
                    JsonUtil.format(enumConstants, 0, 0));
        }

        //*************************************************************************
        for (E e : enumConstants) {
            Object propertyValue = PropertyUtil.getProperty(e, propertyName);
            if (isEquals(propertyValue, specifiedValue, ignoreCase)) {
                return e;
            }
        }

        //*************************************************************************
        if (LOGGER.isInfoEnabled()) {
            String messagePattern = "[{}],propertyName:[{}],value:[{}],ignoreCase:[{}],constants not found";
            LOGGER.info(Slf4jUtil.format(messagePattern, enumClass, propertyName, specifiedValue, ignoreCase));
        }
        return null;
    }

    /**
     * Checks if is equals.
     *
     * @param <T>
     *            the generic type
     * @param propertyValue
     *            the property value
     * @param specifiedValue
     *            
     * @param ignoreCase
     *            the ignore case
     * @return true, if checked
     * @since 1.4.0
     */
    private static <T> boolean isEquals(Object propertyValue, T specifiedValue, boolean ignoreCase) {
        if (propertyValue == null || specifiedValue == null) {//  
            return propertyValue == specifiedValue;
        } else if (propertyValue == specifiedValue) {
            return true;
        }
        //  propertyValue  specifiedValue,  ?null 
        String propertyValueString = propertyValue.toString();
        String specifiedValueString = specifiedValue.toString();
        return ignoreCase ? StringUtils.equalsIgnoreCase(propertyValueString, specifiedValueString)
                : StringUtils.equals(propertyValueString, specifiedValueString);
    }
}