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

org.nutz.lang.random.EnumRandom Maven / Gradle / Ivy

Go to download

Nutz, which is a collections of lightweight frameworks, each of them can be used independently

There is a newer version: 1.r.72
Show newest version
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();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy