From 6f589c8384bf5dcd52320dbbb7df436aa9c06e54 Mon Sep 17 00:00:00 2001 From: janos erdos Date: Mon, 29 Jul 2024 09:51:55 +0200 Subject: [PATCH] no lazyness --- java-src/io/github/erdos/stencil/API.java | 3 +- .../stencil/functions/FunctionEvaluator.java | 28 +++++++++++-------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/java-src/io/github/erdos/stencil/API.java b/java-src/io/github/erdos/stencil/API.java index 427d087d..276bbe73 100644 --- a/java-src/io/github/erdos/stencil/API.java +++ b/java-src/io/github/erdos/stencil/API.java @@ -6,7 +6,6 @@ import java.io.File; import java.io.IOException; -import java.util.ArrayList; import java.util.Collection; import java.util.Map; @@ -60,7 +59,7 @@ public static EvaluatedDocument render(PreparedTemplate template, Map fragments, TemplateData data, Collection 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); } diff --git a/java-src/io/github/erdos/stencil/functions/FunctionEvaluator.java b/java-src/io/github/erdos/stencil/functions/FunctionEvaluator.java index a6432af3..4e8d3648 100644 --- a/java-src/io/github/erdos/stencil/functions/FunctionEvaluator.java +++ b/java-src/io/github/erdos/stencil/functions/FunctionEvaluator.java @@ -6,21 +6,27 @@ import java.util.ServiceLoader; public final class FunctionEvaluator { + private final static Map preloaded; - private final Map 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 providers = ServiceLoader.load(FunctionsProvider.class); + private final Map functions; { - for (FunctionsProvider provider : providers) { - registerFunctions(provider); - } + this.functions = new HashMap<>(preloaded); } - private void registerFunction(Function function) { + private static void registerFunction(Map 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."); } @@ -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); } }