org.nutz.lang.random.EnumRandom Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nutz Show documentation
Show all versions of nutz Show documentation
Nutz, which is a collections of lightweight frameworks, each of them can be used independently
package org.nutz.lang.random;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import org.nutz.lang.Lang;
import org.nutz.lang.Mirror;
public abstract class EnumRandom> implements Random {
private RecurArrayRandom r;
@SuppressWarnings("unchecked")
protected EnumRandom() {
try {
Class type = (Class) Mirror.getTypeParams(this.getClass())[0];
Field[] fields = type.getFields();
List list = new ArrayList(fields.length);
for (Field f : fields) {
if (f.getType() == type) {
list.add((T) f.get(null));
}
}
T[] ens = (T[]) Array.newInstance(type, list.size());
this.r = new RecurArrayRandom(list.toArray(ens));
}
catch (Exception e) {
throw Lang.wrapThrow(e);
}
}
public T next() {
return r.next();
}
}