Last active
May 4, 2023 11:37
-
-
Save TGITS/a1ff73d8cf389706f0995279d037fc81 to your computer and use it in GitHub Desktop.
Examples with the Options provider from Datafaker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//DEPS net.datafaker:datafaker:1.9.0 | |
/** | |
* Pour exécuter avec JBang : jbang DataFakerOptionsProvider.jsh | |
* Pour exécuter sous JShell : | |
* jshell --class-path .;.\lib\datafaker-1.9.0.jar | |
* /open DataFakerOptionsProvider.jsh | |
*/ | |
import net.datafaker.Faker; | |
import net.datafaker.providers.base.Options; | |
import java.util.List; | |
import java.util.Set; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
enum DaysOfWeek { | |
MONDAY, | |
TUESDAY, | |
WEDNESDAY, | |
FRIDAY, | |
SATURDAY, | |
SUNDAY | |
}; | |
// Instanciation d'un objet Faker pour pouvoir générer des valeurs aléatoires | |
Faker faker = new Faker(); | |
Options options = faker.options(); | |
System.out.println("Choosing randomly between several options specified as a String"); | |
IntStream.range(0, 10).forEach(i -> System.out.println("My Favorite day is (iteration #" + i + ") : " | |
+ options.option("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))); | |
System.out.println(); | |
System.out.println("Choosing randomly between several options specified by an Enumeration"); | |
IntStream.range(0, 10).forEach(i -> System.out.println("My Favorite day is (iteration #" + i + ") : " + options.option(DaysOfWeek.class))); | |
System.out.println(); | |
System.out.println("Choosing randomly an option from a List or an Array"); | |
List<String> daysOfWeekAsList = List.of("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"); | |
IntStream.range(0, 10).forEach(i -> System.out.println("My Favorite day is (iteration #" + i + ") : " + options.nextElement(daysOfWeekAsList))); | |
System.out.println(); | |
DaysOfWeek[] daysOfWeekAsArray = DaysOfWeek.values(); | |
IntStream.range(0, 10).forEach(i -> System.out.println("My Favorite day is (iteration #" + i + ") : " + options.nextElement(daysOfWeekAsArray))); | |
for(int i = 0 ; i < 4 ; i++) { | |
System.out.println(); | |
Set<DaysOfWeek> fourDaysOfWeek = options.subset(4,daysOfWeekAsArray); | |
System.out.println("A set of four random days among seven : " + fourDaysOfWeek.stream().map(DaysOfWeek::toString).collect(Collectors.joining(", "))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment