|
1 | 1 | package dev.goldmensch.fluava.function; |
2 | 2 |
|
3 | 3 | import dev.goldmensch.fluava.Result; |
| 4 | +import dev.goldmensch.fluava.function.util.TriFunction; |
4 | 5 |
|
| 6 | +import java.util.Arrays; |
5 | 7 | import java.util.Collection; |
6 | 8 |
|
7 | 9 |
|
|
15 | 17 | /// [Function.Implicit] is a special case, were the function only takes one positional argument, the "value". |
16 | 18 | /// This special sort of function is automatically applied if any of the variables match (or can be converted via proteus to) a class |
17 | 19 | /// specified in [Implicit#acceptableTypes()]. |
| 20 | +/// |
| 21 | +@FunctionalInterface |
18 | 22 | public interface Function<R extends Value.Formatted, T> { |
19 | 23 |
|
| 24 | + /// Constructs a new [Function.Implicit] for the specified types. |
| 25 | + /// |
| 26 | + /// @param function the transforming function ([Function.Implicit#apply(Context, Object, Options)]) |
| 27 | + /// @param acceptableTypes the types this function accepts ([Implicit#acceptableTypes()]) |
| 28 | + /// @return the newly created instance of [Function] |
| 29 | + /// |
| 30 | + /// @see Function.Implicit |
| 31 | + static <R extends Value.Formatted, T> Function<R, T> implicit(TriFunction<Context, T, Options, Result<R>> function, Collection<Class<? extends T>> acceptableTypes) { |
| 32 | + return new Implicit<>() { |
| 33 | + @Override |
| 34 | + public Result<R> apply(Context context, T value, Options options) throws FunctionException { |
| 35 | + return function.apply(context, value, options); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public Collection<Class<? extends T>> acceptableTypes() { |
| 40 | + return acceptableTypes; |
| 41 | + } |
| 42 | + }; |
| 43 | + } |
| 44 | + |
| 45 | + /// Constructs a new [Function.Implicit] for the specified types. |
| 46 | + /// |
| 47 | + /// @param function the transforming function ([Function.Implicit#apply(Context, Object, Options)]) |
| 48 | + /// @param acceptableTypes the types this function accepts ([Implicit#acceptableTypes()]) |
| 49 | + /// @return the newly created instance of [Function] |
| 50 | + /// |
| 51 | + /// @see Function.Implicit |
| 52 | + static <R extends Value.Formatted, T> Function<R, T> implicit(TriFunction<Context, T, Options, Result<R>> function, Class<? extends T>... acceptableTypes) { |
| 53 | + return implicit(function, Arrays.asList(acceptableTypes)); |
| 54 | + } |
| 55 | + |
20 | 56 | /// Applies this function to the provided value and options. |
21 | 57 | /// |
22 | 58 | /// @param context information like the locale |
|
0 commit comments