diff options
| author | Ben Culkin <scorpress@gmail.com> | 2020-11-15 13:35:25 -0500 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2020-11-15 13:35:25 -0500 |
| commit | cd4487c1e3b50fdd8aa4a3cc81edf665c86507ca (patch) | |
| tree | 0a3356bdd24d57f019aded22b29ee05544a46025 | |
| parent | b60ab8e8524b21a48b2440c346b1dd0c6ccc2d85 (diff) | |
Add new functional interface for choosing from arrays
Adds two additional function types which make picking a single element
from an array of elements easier.
Currently, a generic one is provided that works for all reference types,
and a variant specialized for primitive ints is also provided.
| -rw-r--r-- | src/main/java/bjc/functypes/ArrayChooser.java | 20 | ||||
| -rw-r--r-- | src/main/java/bjc/functypes/IntArrayChooser.java | 19 |
2 files changed, 39 insertions, 0 deletions
diff --git a/src/main/java/bjc/functypes/ArrayChooser.java b/src/main/java/bjc/functypes/ArrayChooser.java new file mode 100644 index 0000000..3baa00c --- /dev/null +++ b/src/main/java/bjc/functypes/ArrayChooser.java @@ -0,0 +1,20 @@ +package bjc.functypes; + +/** + * Function which picks a single element from an array of elements. + * + * @author Ben Culkin + * + * @param <ElementType> The type of element stored in the array. + */ +@FunctionalInterface +public interface ArrayChooser<ElementType> { + /** + * Select a single element from an array of elements. + * + * @param elements The elements to pick from. + * + * @return The selected element. + */ + public ElementType choose(@SuppressWarnings("unchecked") ElementType... elements); +} diff --git a/src/main/java/bjc/functypes/IntArrayChooser.java b/src/main/java/bjc/functypes/IntArrayChooser.java new file mode 100644 index 0000000..416487e --- /dev/null +++ b/src/main/java/bjc/functypes/IntArrayChooser.java @@ -0,0 +1,19 @@ +package bjc.functypes; + +/** + * Int-specialized variant of ArrayChooser. + * + * @author Ben Culkin + * + */ +@FunctionalInterface +public interface IntArrayChooser { + /** + * Choose a single int from an array of ints. + * + * @param ints The array of ints to choose. + * + * @return The chosen int. + */ + public int choose(int... ints); +} |
