All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.webapp.utils.enums.EnumUtils Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package com.webapp.utils.enums;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;

import com.webapp.utils.clz.ClzUtils;

public final class EnumUtils {

	private EnumUtils(){}

	@SafeVarargs
    public static > List getList(Class clz, String prop, E...excludes){
		Field field = ClzUtils.getField(clz, prop);

		List result = new ArrayList();
		if(field == null) return result;

		EnumSet allOf = EnumSet.allOf(clz);
		if(excludes != null) allOf.removeAll(Arrays.asList(excludes));

		Iterator iterator = allOf.iterator();
		while(iterator.hasNext()){
			E next = iterator.next();
			result.add(ClzUtils.getFieldVal(field, next).toString());
		}
		return result;
	}

    @SafeVarargs
    @SuppressWarnings("unchecked")
	public static , T> List getList(Class clz, String prop, Class returnType, E...excludes){
		Field field = ClzUtils.getField(clz, prop);

		List result = new ArrayList();
		if(field == null) return result;

		EnumSet allOf = EnumSet.allOf(clz);
		if(excludes != null) allOf.removeAll(Arrays.asList(excludes));

		Iterator iterator = allOf.iterator();
		while(iterator.hasNext()){
			E next = iterator.next();
			result.add((T)ClzUtils.getFieldVal(field, next));
		}
		return result;
	}

	/*
     * @Description Get enum attribute value
     * @param clz enum class
     * @param prop enum attribute
	 * @return string value
	 */
	public static > String valueOf(E enumEle, String prop){
		Field field = ClzUtils.getField(enumEle.getClass(), prop);
		if(field == null) return null;

		return ClzUtils.getFieldVal(field, enumEle).toString();
	}

	/*
     * @param clz enum class
     * @param prop enum attribute
     * @param prop enum value
	 * @return boolean
	 */
	public static > boolean isNotExist(Class clz, String prop, Object value){
		return !isExist(clz, prop, value);
	}

	/*
     * @param clz enum class
     * @param prop enum attribute
     * @param prop enum value
	 * @return boolean
	 */
	public static > boolean isExist(Class clz, String prop, Object value){
		return getEnum(clz, prop, value) != null ? true : false;
	}

	/*
	 * @Description Get enum name
     * @param clz enum class
     * @param prop enum name
	 * @return boolean
	 */
	public static > E getEnum(Class clz, String name){

		Iterator iterator = EnumSet.allOf(clz).iterator();
		while(iterator.hasNext()){
			E next = iterator.next();
			if(next.name().equals(name)){
				return next;
			}
		}
		return null;
	}

	/*
	 * @Description Get enum by attribute and value
     * @param clz enum class
     * @param prop enum attribute
     * @param prop enum value
	 * @return boolean
	 */
	public static > E getEnum(Class clz, String prop, Object value){
		Field field = ClzUtils.getField(clz, prop);
		if(field == null) return null;

		Iterator iterator = EnumSet.allOf(clz).iterator();
		while(iterator.hasNext()){
			E next = iterator.next();
			if(ClzUtils.getFieldVal(field, next).equals(value)){
				return next;
			}
		}
		return null;
	}

	@SafeVarargs
    public static > List getEnums(E[] all, E... excludes) {
    	List result = new ArrayList(Arrays.asList(all));
		result.removeAll(Arrays.asList(excludes));

		return result;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy