com.discovery.darchrow.lang.EnumUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.discovery.darchrow.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.discovery.darchrow.lang;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.discovery.darchrow.bean.BeanUtilException;
import com.discovery.darchrow.bean.PropertyUtil;
import com.discovery.darchrow.net.HttpMethodType;
import com.discovery.darchrow.slf4j.Slf4jUtil;
import com.discovery.darchrow.tools.jsonlib.JsonUtil;
import com.discovery.darchrow.util.Validator;

/**
 * {@link java.lang.Enum} .
 * 
 * @author feilong
 * @version 1.0.6 201458 ?3:30:51
 * @version 1.0.8 2014-7-22 13:43 add {@link EnumUtil#getEnumByPropertyValueIgnoreCase(Class, String, Object)}
 * @see org.apache.commons.lang.enums.EnumUtils
 * @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!");
    }

    /**
     * fieldName value(?) (equalsIgnoreCase)<br>
     * 
     * <pre>
     * 
     * ?{@link HttpMethodType} ,?:
     * 
     * {@code
     *    EnumUtil.getEnumByField(HttpMethodType.class, "method", "get")
     * }
     * </pre>
     *
     * @param <E>
     *            the element type
     * @param <T>
     *            the generic type
     * @param enumClass
     *            the enum class  {@link HttpMethodType}
     * @param propertyName
     *            ??, {@link HttpMethodType}method,javabean 
     * @param value
     *             post
     * @return  enum constant
     */
    public static <E extends Enum<?>, T> E getEnumByPropertyValueIgnoreCase(Class<E> enumClass, String propertyName,
            T value) {
        boolean ignoreCase = true;
        return getEnumByPropertyValue(enumClass, propertyName, value, ignoreCase);
    }

    /**
     * fieldName value (equals)<br>
     * 
     * <pre>
     * 
     * ?{@link HttpMethodType} ,?:
     * 
     * {@code
     *    EnumUtil.getEnumByField(HttpMethodType.class, "method", "get")
     * }
     * </pre>
     *
     * @param <E>
     *            the element type
     * @param <T>
     *            the generic type
     * @param enumClass
     *            the enum class  {@link HttpMethodType}
     * @param propertyName
     *            ??, {@link HttpMethodType}method,javabean 
     * @param value
     *             post
     * @return  enum constant
     * @since 1.0.8
     */
    public static <E extends Enum<?>, T> E getEnumByPropertyValue(Class<E> enumClass, String propertyName,
            T value) {
        boolean ignoreCase = false;
        return getEnumByPropertyValue(enumClass, propertyName, value, ignoreCase);
    }

    /**
     * fieldName value <br>
     * 
     * <pre>
     * 
     * ?{@link HttpMethodType} ,?:
     * 
     * {@code
     *    EnumUtil.getEnumByField(HttpMethodType.class, "method", "get")
     * }
     * </pre>
     *
     * @param <E>
     *            the element type
     * @param <T>
     *            the generic type
     * @param enumClass
     *            the enum class  {@link HttpMethodType}
     * @param propertyName
     *            ??, {@link HttpMethodType}method,javabean 
     * @param value
     *             post
     * @param ignoreCase
     *            ??
     * @return  enum constant
     * @see com.baozun.nebulaplus.bean.BeanUtil#getProperty(Object, String)
     * @since 1.0.8
     */
    private static <E extends Enum<?>, T> E getEnumByPropertyValue(Class<E> enumClass, String propertyName, T value,
            boolean ignoreCase) {

        if (Validator.isNullOrEmpty(enumClass)) {
            throw new IllegalArgumentException("enumClass is null or empty!");
        }

        if (Validator.isNullOrEmpty(propertyName)) {
            throw new IllegalArgumentException("the fieldName is null or empty!");
        }

        // An enum is a kind of class
        // and an annotation is a kind of interface
        //  Class ? null.
        E[] enumConstants = enumClass.getEnumConstants();

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("enumClass:[{}],enumConstants:{}", enumClass.getCanonicalName(),
                    JsonUtil.format(enumConstants));
        }
        for (E e : enumConstants) {

            Object propertyValue = PropertyUtil.getProperty(e, propertyName);

            if (null == propertyValue && null == value) {
                return e;
            }
            if (null != propertyValue && null != value) {
                if (ignoreCase && propertyValue.toString().equalsIgnoreCase(value.toString())) {
                    return e;
                } else if (propertyValue.equals(value)) {
                    return e;
                }
            }
        }
        String messagePattern = "can not found the enum constants,enumClass:[{}],propertyName:[{}],value:[{}],ignoreCase:[{}]";
        throw new BeanUtilException(
                Slf4jUtil.formatMessage(messagePattern, enumClass, propertyName, value, ignoreCase));
    }
}