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

net.dongliu.commons.Enums Maven / Gradle / Ivy

There is a newer version: 12.0.2
Show newest version
package net.dongliu.commons;

import net.dongliu.commons.concurrent.WeakLoader;
import net.dongliu.commons.reflect.JavaClass;

/**
 * Enum utils methods
 *
 * @author Liu Dong
 */
public class Enums {
    @SuppressWarnings("unchecked")
    private static final WeakLoader, Object> enumValuesLoader =
            WeakLoader.create(Enums::_getValues);

    /**
     * Get all values of this enum type
     */
    public static > T[] values(Class cls) {
        return Cast.to(enumValuesLoader.get(cls));
    }

    private static > T[] _getValues(Class cls) {
        return Cast.to(JavaClass.of(cls).getStaticMethod("values").call());
    }

    /**
     * Get enum value from ordinal
     */
    public static > T ofOrdinal(Class cls, int ordinal) {
        T[] values = values(cls);
        return values[ordinal];
    }

    /**
     * Add a amount to enmu, get new value
     */
    public static > T add(T value, int amount) {
        int ordinal = value.ordinal() + amount;
        return ofOrdinal(Cast.to(value.getClass()), ordinal);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy