Java Reflection Enum isLegalEnum(Class clazz, Object enuValue)

Here you can find the source of isLegalEnum(Class clazz, Object enuValue)

Description

is Legal Enum

License

Open Source License

Declaration

@SuppressWarnings("rawtypes")
    public static boolean isLegalEnum(Class clazz, Object enuValue) 

Method Source Code

//package com.java2s;

import java.util.Arrays;
import java.util.Collection;

public class Main {
    @SuppressWarnings("rawtypes")
    public static boolean isLegalEnum(Class clazz, Object enuValue) {
        boolean result = false;
        if (null == clazz || null == enuValue) {
            return result;
        }/*from   w  ww.  j a v  a 2 s  . c om*/
        if (clazz.isEnum()) {
            Enum[] enums = (Enum[]) clazz.getEnumConstants();
            if (!isEmpty(enums)) {
                if (enuValue instanceof Enum) {
                    Enum e = (Enum) enuValue;
                    if (Arrays.asList(enums).contains(e)) {
                        result = true;
                    }
                    ;
                } else if (enuValue instanceof String) {
                    String str = (String) enuValue;
                    for (Enum en : enums) {
                        if (en.name().equals(str.trim())) {
                            result = true;
                            break;
                        }
                    }
                }
            }
        }
        return result;
    }

    /**
     * return whether the given array is empty
     */
    public static boolean isEmpty(Object[] array) {
        return (array == null || array.length == 0);
    }

    public static boolean isEmpty(Collection<?> collection) {
        if (collection == null || collection.size() == 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * check if the given objects are equal
     */
    public static boolean equals(Object o1, Object o2) {
        if (o1 == o2) {
            return true;
        }
        if (o1 == null || o2 == null) {
            return false;
        }
        if (o1.equals(o2)) {
            return true;
        }
        if (o1.getClass().isArray() && o2.getClass().isArray()) {
            if (o1 instanceof Object[] && o2 instanceof Object[]) {
                return Arrays.equals((Object[]) o1, (Object[]) o2);
            }
            if (o1 instanceof boolean[] && o2 instanceof boolean[]) {
                return Arrays.equals((boolean[]) o1, (boolean[]) o2);
            }
            if (o1 instanceof byte[] && o2 instanceof byte[]) {
                return Arrays.equals((byte[]) o1, (byte[]) o2);
            }
            if (o1 instanceof char[] && o2 instanceof char[]) {
                return Arrays.equals((char[]) o1, (char[]) o2);
            }
            if (o1 instanceof double[] && o2 instanceof double[]) {
                return Arrays.equals((double[]) o1, (double[]) o2);
            }
            if (o1 instanceof float[] && o2 instanceof float[]) {
                return Arrays.equals((float[]) o1, (float[]) o2);
            }
            if (o1 instanceof int[] && o2 instanceof int[]) {
                return Arrays.equals((int[]) o1, (int[]) o2);
            }
            if (o1 instanceof long[] && o2 instanceof long[]) {
                return Arrays.equals((long[]) o1, (long[]) o2);
            }
            if (o1 instanceof short[] && o2 instanceof short[]) {
                return Arrays.equals((short[]) o1, (short[]) o2);
            }
        }
        return false;
    }
}

Related

  1. getEnumConstants(Class c)
  2. getEnumNames(Class> e)
  3. getEnumValues(Class type)
  4. getOptionsFromEnum(Class> enumClass)
  5. toEnum(Class e, String s)
  6. toStrings(Class clenum)