com.github.javafaker.Options Maven / Gradle / Ivy
package com.github.javafaker;
import java.util.List;
public class Options {
private final Faker faker;
protected Options(Faker faker) {
this.faker = faker;
}
/**
* Returns a random element from an varargs.
*
* @param options The varargs to take a random element from.
* @param The type of the elements in the varargs.
* @return A randomly selected element from the varargs.
*/
public E option(E... options) {
return options[faker.random().nextInt(options.length)];
}
/**
* Returns a random element from Enum.
*
* @param enumeration The Enum to take a random element from.
* @return A randomly selected element from the enum.
*/
public > E option(Class enumeration) {
E[] enumConstants = enumeration.getEnumConstants();
return enumConstants[faker.random().nextInt(enumConstants.length)];
}
/**
* Returns a random element from an array.
*
* @param array The array to take a random element from.
* @param The type of the elements in the array.
* @return A randomly selected element from the array.
*/
public E nextElement(E[] array) {
return array[faker.random().nextInt(array.length)];
}
/**
* Returns a random element from a list.
*
* @param list The list to take a random element from.
* @param The type of the elements in the list.
* @return A randomly selected element from the list.
*/
public E nextElement(List list) {
return list.get(faker.random().nextInt(list.size()));
}
}