Currently this library contains the following utility classes:
- ArrayUtils - Utility library for working with Arrays
- DateUtils - Utility library for working with Dates
- EnumUtils - Utility library for working with Enums
- IterableUtils - Utility library for working with Iterables
- RandomCollectionUtils - Utility library to generate random collections
- RandomArrayUtils - Utility library to generate random arrays
- RandomDateUtils - Utility library to return random dates, e.g., Instants, ZonedDateTimes, LocalDates, Dates, etc.
- Currently supports java.util.Date and all the dates, times, instants, and durations from Java 8's date and time API
- RandomEnumUtils - Utility library to retrieve random elements from enum instances
- RandomNumberUtils - Utility library to return random numbers. Unlike Apaches RandomUtils, this supports negative numbers
@Test
public void managerWithFiveSubordinatesHasFavoriteEmployee() {
Employee manager = createEmployee(JobTitle.MANAGER);
manager.subordinates = RandomCollectionUtils.randomListFrom(this::createSubordinate, 5);
Employee favoriteEmployee = IterableUtils.randomFrom(manager.subordinates);
assertTrue(manager.subordinates.contains(favoriteEmployee));
}
private Employee createEmployee(JobTitle jobTitle) {
Employee employee = new Employee();
employee.jobTitle = jobTitle;
employee.birthDateTime = RandomDateUtils.randomPastZonedDateTime();
employee.startDate = RandomDateUtils.randomLocalDate();
employee.endDate = RandomDateUtils.randomLocalDateAfter(employee.startDate);
employee.clockInTime = RandomDateUtils.randomLocalTime();
employee.clockOutTime = RandomDateUtils.randomLocalTimeAfter(employee.clockInTime);
employee.lunchTime = RandomDateUtils.randomLocalTime(employee.clockInTime, employee.clockOutTime);
employee.subordinates = Collections.emptyList();
return employee;
}
private Employee createSubordinate() {
JobTitle[] excludes = new JobTitle[] { JobTitle.MANAGER, JobTitle.SUPERVISOR };
return createEmployee(RandomEnumUtils.random(JobTitle.class, excludes));
}
private class Employee {
JobTitle jobTitle;
ZonedDateTime birthDateTime;
LocalDate startDate;
LocalDate endDate;
LocalTime clockInTime;
LocalTime clockOutTime;
LocalTime lunchTime;
List<Employee> subordinates;
}
private enum JobTitle {
MANAGER, SUPERVISOR, DEVELOPER, TESTER
}
You can download source and binaries from the releases page.
Alternatively you can pull it from the central Maven repositories:
<dependency>
<groupId>com.github.rkumsher</groupId>
<artifactId>utils</artifactId>
<version>1.3</version>
</dependency>
This code is under the Apache Licence v2.