Skip to content

Commit

Permalink
no lazyness
Browse files Browse the repository at this point in the history
  • Loading branch information
erdos committed Jul 29, 2024
1 parent fd7a3a7 commit 6f589c8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
3 changes: 1 addition & 2 deletions java-src/io/github/erdos/stencil/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

Expand Down Expand Up @@ -60,7 +59,7 @@ public static EvaluatedDocument render(PreparedTemplate template, Map<String, Pr
public static EvaluatedDocument render(PreparedTemplate template, Map<String, PreparedFragment> fragments, TemplateData data, Collection<Function> customFunctions) {
FunctionEvaluator function = new FunctionEvaluator();
if (customFunctions != null) {
function.registerFunctions(() -> new ArrayList<>(customFunctions));
function.registerFunctions(customFunctions.toArray(new Function[0]));
}
return template.render(fragments, function, data);
}
Expand Down
28 changes: 17 additions & 11 deletions java-src/io/github/erdos/stencil/functions/FunctionEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,27 @@
import java.util.ServiceLoader;

public final class FunctionEvaluator {
private final static Map<String, Function> preloaded;

private final Map<String, Function> functions = new HashMap<>();
static {
preloaded = new HashMap<>();
for (FunctionsProvider provider : ServiceLoader.load(FunctionsProvider.class)) {
for (Function fn : provider.functions()) {
registerFunction(preloaded, fn);
}
}
}

private static final ServiceLoader<FunctionsProvider> providers = ServiceLoader.load(FunctionsProvider.class);
private final Map<String, Function> functions;

{
for (FunctionsProvider provider : providers) {
registerFunctions(provider);
}
this.functions = new HashMap<>(preloaded);
}

private void registerFunction(Function function) {
private static void registerFunction(Map<String, Function> map, Function function) {
if (function == null)
throw new IllegalArgumentException("Registered function must not be null.");
Function present = functions.put(function.getName().toLowerCase(), function);
Function present = map.put(function.getName().toLowerCase(), function);
if (present != null)
throw new IllegalArgumentException("Function with name has already been registered.");
}
Expand All @@ -29,13 +35,13 @@ private void registerFunction(Function function) {
* Registers a function to this evaluator engine.
* Registered functions can be invoked from inside template files.
*
* @param provider contains list of functions to register
* @param functions list of functions to register
*/

@SuppressWarnings("WeakerAccess")
public void registerFunctions(FunctionsProvider provider) {
for (Function function : provider.functions()) {
registerFunction(function);
public void registerFunctions(Function... functions) {
for (Function function : functions) {
registerFunction(this.functions, function);
}
}

Expand Down

0 comments on commit 6f589c8

Please sign in to comment.