Skip to content

Commit bd0ed05

Browse files
committed
Release 1.1.1 : Add a fluent API for implicit functions
1 parent 4b7d476 commit bd0ed05

4 files changed

Lines changed: 49 additions & 1 deletion

File tree

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ dependencies {
1212
}
1313

1414
allprojects {
15-
version = "1.1.0"
15+
version = "1.1.1"
1616
}
1717

1818

fluava/src/main/java/dev/goldmensch/fluava/function/Function.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package dev.goldmensch.fluava.function;
22

33
import dev.goldmensch.fluava.Result;
4+
import dev.goldmensch.fluava.function.util.TriFunction;
45

6+
import java.util.Arrays;
57
import java.util.Collection;
68

79

@@ -15,8 +17,42 @@
1517
/// [Function.Implicit] is a special case, were the function only takes one positional argument, the "value".
1618
/// This special sort of function is automatically applied if any of the variables match (or can be converted via proteus to) a class
1719
/// specified in [Implicit#acceptableTypes()].
20+
///
21+
@FunctionalInterface
1822
public interface Function<R extends Value.Formatted, T> {
1923

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+
2056
/// Applies this function to the provided value and options.
2157
///
2258
/// @param context information like the locale
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package dev.goldmensch.fluava.function.util;
2+
3+
import java.util.function.Function;
4+
5+
/// A [Function] that takes three arguments and returns one result.
6+
@FunctionalInterface
7+
public interface TriFunction<F, S, T, R> {
8+
9+
/// @see Function#apply(Object)
10+
R apply(F first, S second, T third);
11+
}

fluava/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module dev.goldmensch.fluava {
22
exports dev.goldmensch.fluava;
33
exports dev.goldmensch.fluava.function;
4+
exports dev.goldmensch.fluava.function.util;
45

56
requires io.github.parseworks.parseworks;
67
requires dev.goldmensch.fluava.cldrplurals;

0 commit comments

Comments
 (0)