-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom function support to SpEL resolver
Allow adding custom functions to SpEL resolver by name, parameter types, and lambda implementation. Simplifies adding functions without a full interface and implementing class.
- Loading branch information
1 parent
03e8ff5
commit e438ddc
Showing
13 changed files
with
245 additions
and
163 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
engine/src/main/java/pro/verron/officestamper/api/CustomFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package pro.verron.officestamper.api; | ||
|
||
import pro.verron.officestamper.utils.TriFunction; | ||
|
||
import java.util.List; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
|
||
public record CustomFunction( | ||
String name, | ||
List<Class<?>> parameterTypes, | ||
Function<List<Object>, Object> function | ||
) { | ||
public interface NeedsFunctionImpl<T> { | ||
void withImplementation(Function<T, ?> function); | ||
} | ||
|
||
public interface NeedsBiFunctionImpl<T, U> { | ||
void withImplementation(BiFunction<T, U, ?> object); | ||
} | ||
|
||
public interface NeedsTriFunctionImpl<T, U, V> { | ||
void withImplementation(TriFunction<T, U, V, ?> function); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 2 additions & 7 deletions
9
engine/src/main/java/pro/verron/officestamper/core/Invokers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
engine/src/main/java/pro/verron/officestamper/core/functions/BiFunctionBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package pro.verron.officestamper.core.functions; | ||
|
||
import pro.verron.officestamper.api.CustomFunction; | ||
import pro.verron.officestamper.core.DocxStamperConfiguration; | ||
|
||
import java.util.List; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
|
||
public class BiFunctionBuilder<T, U> | ||
implements CustomFunction.NeedsBiFunctionImpl<T, U> { | ||
private final DocxStamperConfiguration source; | ||
private final String name; | ||
private final Class<T> class0; | ||
private final Class<U> class1; | ||
|
||
public BiFunctionBuilder(DocxStamperConfiguration source, String name, Class<T> class0, Class<U> class1) { | ||
this.source = source; | ||
this.name = name; | ||
this.class0 = class0; | ||
this.class1 = class1; | ||
} | ||
|
||
@Override public void withImplementation(BiFunction<T, U, ?> implementation) { | ||
Function<List<Object>, Object> function = args -> { | ||
var arg0 = class0.cast(args.getFirst()); | ||
var arg1 = class1.cast(args.get(1)); | ||
return implementation.apply(arg0, arg1); | ||
}; | ||
var customFunction = new CustomFunction(name, List.of(class0, class1), function); | ||
source.addCustomFunction(customFunction); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
engine/src/main/java/pro/verron/officestamper/core/functions/FunctionBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package pro.verron.officestamper.core.functions; | ||
|
||
import pro.verron.officestamper.api.CustomFunction; | ||
import pro.verron.officestamper.core.DocxStamperConfiguration; | ||
|
||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
public class FunctionBuilder<T> | ||
implements CustomFunction.NeedsFunctionImpl<T> { | ||
private final DocxStamperConfiguration source; | ||
private final String name; | ||
private final Class<T> class0; | ||
|
||
public FunctionBuilder(DocxStamperConfiguration source, String name, Class<T> class0) { | ||
this.source = source; | ||
this.name = name; | ||
this.class0 = class0; | ||
} | ||
|
||
@Override public void withImplementation(Function<T, ?> implementation) { | ||
Function<List<Object>, Object> objectFunction = args -> { | ||
var arg0 = class0.cast(args.getFirst()); | ||
return implementation.apply(arg0); | ||
}; | ||
var customFunction = new CustomFunction(name, List.of(class0), objectFunction); | ||
source.addCustomFunction(customFunction); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
engine/src/main/java/pro/verron/officestamper/core/functions/TriFunctionBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package pro.verron.officestamper.core.functions; | ||
|
||
import pro.verron.officestamper.api.CustomFunction; | ||
import pro.verron.officestamper.core.DocxStamperConfiguration; | ||
import pro.verron.officestamper.utils.TriFunction; | ||
|
||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
public class TriFunctionBuilder<T, U, V> | ||
implements CustomFunction.NeedsTriFunctionImpl<T, U, V> { | ||
private final DocxStamperConfiguration source; | ||
private final String name; | ||
private final Class<T> class0; | ||
private final Class<U> class1; | ||
private final Class<V> class2; | ||
|
||
public TriFunctionBuilder( | ||
DocxStamperConfiguration source, String name, Class<T> class0, Class<U> class1, Class<V> class2 | ||
) { | ||
this.source = source; | ||
this.name = name; | ||
this.class0 = class0; | ||
this.class1 = class1; | ||
this.class2 = class2; | ||
} | ||
|
||
@Override public void withImplementation(TriFunction<T, U, V, ?> implementation) { | ||
Function<List<Object>, Object> function = args -> { | ||
var arg0 = class0.cast(args.getFirst()); | ||
var arg1 = class1.cast(args.get(1)); | ||
var arg2 = class2.cast(args.get(2)); | ||
return implementation.apply(arg0, arg1, arg2); | ||
}; | ||
var customFunction = new CustomFunction(name, List.of(class0, class1, class2), function); | ||
source.addCustomFunction(customFunction); | ||
} | ||
} |
47 changes: 0 additions & 47 deletions
47
engine/src/main/java/pro/verron/officestamper/preset/IStamperDateFormatter.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.