com.github.javafaker.Options Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javafaker Show documentation
Show all versions of javafaker Show documentation
This library is a port of Ruby's stympy/faker gem (as well as Perl's Data::Faker library) that generates fake data.
It's useful when you're developing a new project and need some pretty data for showcase.
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()));
}
}