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

ru.yandex.qatools.camelot.util.EnumUtil Maven / Gradle / Ivy

There is a newer version: 2.5.4
Show newest version
package ru.yandex.qatools.camelot.util;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Transformer;

import java.util.*;

/**
 * @author Ilya Sadykov
 * @version $Date$ $Revision$
 */
public abstract class EnumUtil {

    private static final Map, Set> cache = new HashMap<>();

    EnumUtil() {
    }

    /**
     * Checks that an String element is contained by the enumclass
     *
     * @param enumClass
     * @param value
     * @return true if enumClass contains value
     */
    public static boolean enumContains(Class enumClass, String value) {
        if (!cache.containsKey(enumClass)) {
            Set options = new HashSet();
            for (Enum opt : enumClass.getEnumConstants()) {
                options.add(String.valueOf(opt));
            }
            cache.put(enumClass, options);
        }
        return cache.get(enumClass).contains(value);
    }

    /**
     * Get the enum value from its string value
     *
     * @param enumClass
     * @param value
     * @param 
     * @return
     */
    public static > T fromString(Class enumClass, String value) {
        if (!enumContains(enumClass, value)) {
            throw new IllegalArgumentException("Wrong value provided to the enum: " + enumClass + " : " + value + "!");
        }
        return Enum.valueOf(enumClass, value);
    }


    /**
     * Get the enum value from its ordinal value
     *
     * @param enumClass
     * @param value
     * @param 
     * @return
     */
    @SuppressWarnings("unchecked")
    public static > T fromOrdinal(Class enumClass, Integer value) {
        Enum[] values = (enumClass).getEnumConstants();
        if (value > values.length - 1 || value < 0) {
            throw new IllegalArgumentException("Wrong value provided for the enum " + enumClass + " : " + value + "!");
        }
        return (T) values[value];
    }


    /**
     * Converts list of strings into list of enum
     *
     * @param enumClass
     * @param values
     * @return
     */
    @SuppressWarnings("unchecked")
    public static > Collection> fromStringCollection(final Class enumClass,
                                                                               Collection values) {
        return CollectionUtils.collect(values, new Transformer() {
            @Override
            public Object transform(Object o) {
                return fromString(enumClass, o.toString());
            }
        });
    }

    /**
     * Converts collection of enums to collection of strings
     *
     * @param values
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Collection toStringCollection(Collection values) {
        return CollectionUtils.collect(values, new Transformer() {
            @Override
            public Object transform(Object o) {
                return ((Enum) o).name();
            }
        });
    }


    /**
     * Returns random value from the enum
     *
     * @param enumClass
     * @param 
     * @return
     */
    public static > T random(Class enumClass) {
        int rand = RandomUtil.randomInt(enumClass.getEnumConstants().length);
        return fromOrdinal(enumClass, rand);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy