Java Random Enum randomEnum(Class cls, T exceptValue)

Here you can find the source of randomEnum(Class cls, T exceptValue)

Description

random Enum

License

Apache License

Declaration

public static <T extends Enum<?>> T randomEnum(Class<T> cls, T exceptValue) 

Method Source Code

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

import java.util.Random;

public class Main {
    public static <T extends Enum<?>> T randomEnum(Class<T> cls, T exceptValue) {
        int x = new Random().nextInt(cls.getEnumConstants().length);
        T t = cls.getEnumConstants()[x];
        return t.equals(exceptValue) ? randomEnum(cls, exceptValue) : t;
    }//  ww  w  .  j ava  2 s  . c  o  m

    public static <T extends Enum<?>> T randomEnum(Class<T> cls) {
        return randomEnum(cls, null);
    }
}

Related

  1. RandomEnum(Class c)
  2. randomEnum(Class c)
  3. randomEnum(Class clazz)
  4. randomEnumSet(Class enumType)