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

com.jparams.object.builder.provider.EnumProvider Maven / Gradle / Ivy

There is a newer version: 2.3.1
Show newest version
package com.jparams.object.builder.provider;

import java.util.Random;

import com.jparams.object.builder.Context;

public class EnumProvider implements Provider
{
    private final Random random = new Random();

    @Override
    public boolean supports(final Class clazz)
    {
        return clazz.isEnum();
    }

    @Override
    public Enum provide(final Context context)
    {
        final Class type = context.getPath().getMemberType().getType();
        final int valueCount = type.getEnumConstants().length;

        if (valueCount > 0)
        {
            final int index = random.nextInt(valueCount - 1);
            return (Enum) type.getEnumConstants()[index];
        }

        return null;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy