poem.boundary.internal.domain.RandomPoemPicker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of poem-hexagon Show documentation
Show all versions of poem-hexagon Show documentation
A simple example for a hexagonal architecture.
The newest version!
package poem.boundary.internal.domain;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Random;
/**
* Picks a random poem from a list of poems.
*
* @author b_muth
*
*/
public class RandomPoemPicker {
private Random random;
public RandomPoemPicker() {
this.random = new Random();
}
/**
* Picks a random poem from the specified list of poems.
*
* @param poems the poems to pick from
* @return a poem from the list, or an empty optional if the input list was empty
*/
public Optional pickPoem(List poems) {
Objects.requireNonNull(poems);
if (poems.size() == 0) {
return Optional.empty();
}
int randomIndex = random.nextInt(poems.size());
Poem randomPoem = poems.get(randomIndex);
return Optional.of(randomPoem);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy