Java Enum from Ordinal fromOrdinal(Class enumClass, Integer value)

Here you can find the source of fromOrdinal(Class enumClass, Integer value)

Description

Get the enum value from its ordinal value

License

Apache License

Parameter

Parameter Description
enumClass a parameter
value a parameter
T a parameter

Declaration

@SuppressWarnings("unchecked")
public static <T extends Enum<T>> T fromOrdinal(Class<T> enumClass, Integer value) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*w w  w  . j  av  a 2  s  .  c  o  m*/
     * Get the enum value from its ordinal value
     *
     * @param enumClass
     * @param value
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T extends Enum<T>> T fromOrdinal(Class<T> enumClass, Integer value) {
        Enum<T>[] 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];
    }
}

Related

  1. fromOrdinal(Class enumClass, int ordinal)
  2. fromOrdinal(E[] values, int ordinal)