1 | package pro.verron.officestamper.core.functions; | |
2 | ||
3 | import org.jetbrains.annotations.Contract; | |
4 | import pro.verron.officestamper.api.CustomFunction; | |
5 | import pro.verron.officestamper.core.DocxStamperConfiguration; | |
6 | ||
7 | import java.util.List; | |
8 | import java.util.function.BiFunction; | |
9 | import java.util.function.Function; | |
10 | ||
11 | /// A builder class for creating and registering bi-functional implementations with a given configuration. | |
12 | /// This class is responsible for bridging a BiFunction implementation into a custom function that | |
13 | /// can be utilized within the provided configuration context. | |
14 | /// | |
15 | /// @param <T> the type of the first input parameter for the BiFunction | |
16 | /// @param <U> the type of the second input parameter for the BiFunction | |
17 | public class BiFunctionBuilder<T, U> | |
18 | implements CustomFunction.NeedsBiFunctionImpl<T, U> { | |
19 | private final DocxStamperConfiguration source; | |
20 | private final String name; | |
21 | private final Class<T> class0; | |
22 | private final Class<U> class1; | |
23 | ||
24 | /// Constructs a new `BiFunctionBuilder` instance, which enables the creation and registration | |
25 | /// of a bi-functional implementation with the specified source configuration. | |
26 | /// | |
27 | /// @param source the configuration instance where the custom function will be registered | |
28 | /// @param name the name given to the bi-functional custom function to identify it | |
29 | /// @param class0 the `Class` type that represents the type of the first input parameter | |
30 | /// @param class1 the `Class` type that represents the type of the second input parameter | |
31 | @Contract(pure = true) | |
32 | public BiFunctionBuilder(DocxStamperConfiguration source, String name, Class<T> class0, Class<U> class1) { | |
33 | this.source = source; | |
34 | this.name = name; | |
35 | this.class0 = class0; | |
36 | this.class1 = class1; | |
37 | } | |
38 | ||
39 | /// Registers a BiFunction implementation as a custom function within the current context. | |
40 | /// The provided implementation is converted into a generic custom function that accepts | |
41 | /// a list of arguments and produces a result. | |
42 | /// This method enables the addition of a bi-functional logic to the associated configuration, which can be | |
43 | /// invoked later with the defined parameter and behavior. | |
44 | /// | |
45 | /// @param implementation the BiFunction implementation to register, taking two input arguments | |
46 | /// of types `T` and `U`, and producing a result | |
47 | @Override | |
48 | public void withImplementation(BiFunction<T, U, ?> implementation) { | |
49 | Function<List<Object>, Object> function = args -> { | |
50 | var arg0 = class0.cast(args.getFirst()); | |
51 | var arg1 = class1.cast(args.get(1)); | |
52 |
1
1. lambda$withImplementation$0 : replaced return value with null for pro/verron/officestamper/core/functions/BiFunctionBuilder::lambda$withImplementation$0 → KILLED |
return implementation.apply(arg0, arg1); |
53 | }; | |
54 | var customFunction = new CustomFunction(name, List.of(class0, class1), function); | |
55 |
1
1. withImplementation : removed call to pro/verron/officestamper/core/DocxStamperConfiguration::addCustomFunction → KILLED |
source.addCustomFunction(customFunction); |
56 | } | |
57 | } | |
Mutations | ||
52 |
1.1 |
|
55 |
1.1 |