-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The new `FunctionProvider` Service Provider Interface simplifies the extension of the function set provided by Stencil. Implementing and registering these new implementations in `META-INF/services/io.github.erdos.stencil.functions.FunctionProvider` enable other libraries to register their functions.
- Loading branch information
Showing
13 changed files
with
206 additions
and
14 deletions.
There are no files selected for viewing
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
40 changes: 40 additions & 0 deletions
40
java-src/io/github/erdos/stencil/functions/ClojureCallFnProvider.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,40 @@ | ||
package io.github.erdos.stencil.functions; | ||
|
||
import io.github.erdos.stencil.impl.ClojureHelper; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
/** | ||
* This provider requires a Clojure NS which contains implementations of the call-fn multimethod. | ||
* The constructor defines the | ||
*/ | ||
public abstract class ClojureCallFnProvider implements FunctionProvider { | ||
|
||
/** | ||
* The constructor loads the Clojure namespace which contains the call-fn multimethod implementations. | ||
* This should be called from the no-args constructor of the concrete class. | ||
* @param ns the clojuse namespace which contains the function implementations. | ||
*/ | ||
protected ClojureCallFnProvider(final String ns) { | ||
ClojureHelper.requireNs(ns); | ||
} | ||
|
||
@Override | ||
/** | ||
* This provider does not provide any functions directly. | ||
* | ||
* @return an empty collection | ||
*/ | ||
public Collection<Function> functions() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
@Override | ||
/* | ||
* call-fn implementations have priority over Java Function implementations during evaluation. | ||
*/ | ||
public int priority() { | ||
return Integer.MIN_VALUE; | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
java-src/io/github/erdos/stencil/functions/DefaultCallFnProvider.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,11 @@ | ||
package io.github.erdos.stencil.functions; | ||
|
||
public class DefaultCallFnProvider extends ClojureCallFnProvider { | ||
/** | ||
* The built-in call-fn functions are defined in the stencil.functions namespace. | ||
* This constructor loads it. | ||
*/ | ||
public DefaultCallFnProvider() { | ||
super("stencil.functions"); | ||
} | ||
} |
15 changes: 9 additions & 6 deletions
15
java-src/io/github/erdos/stencil/functions/FunctionEvaluator.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
20 changes: 20 additions & 0 deletions
20
java-src/io/github/erdos/stencil/functions/FunctionLoader.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,20 @@ | ||
package io.github.erdos.stencil.functions; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.ServiceLoader; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.StreamSupport; | ||
|
||
final class FunctionLoader { | ||
private FunctionLoader() {} | ||
|
||
private static final ServiceLoader<FunctionProvider> REGISTRARS = ServiceLoader.load(FunctionProvider.class); | ||
|
||
static List<Function> getFunctions() { | ||
return StreamSupport.stream(REGISTRARS.spliterator(), false) | ||
.sorted(Comparator.comparingInt(FunctionProvider::priority)) | ||
.flatMap(p -> p.functions().stream()) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
java-src/io/github/erdos/stencil/functions/FunctionProvider.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,32 @@ | ||
package io.github.erdos.stencil.functions; | ||
|
||
import java.util.Collection; | ||
|
||
public interface FunctionProvider { | ||
|
||
|
||
int DEFAULT_PRIORITY = 10; | ||
|
||
/** | ||
* Return the functions instances for the current render. | ||
* The provider may choose to return new instances of a function for each call, if the function is not pure: | ||
* e.g.: a counter, which returns an increasing sequence of numbers for each call. | ||
* | ||
* @return the functions provided by this provider | ||
*/ | ||
Collection<Function> functions(); | ||
|
||
/** | ||
* Priority of the provider. | ||
* <p> | ||
* Providers are called in ascending order of priority. | ||
* <p> | ||
* Default priority is 10. | ||
* NB: Multimethod functions defined in Clojure namespaces have priority over Java defined functions. | ||
* | ||
* @return priority of the provider | ||
*/ | ||
default int priority() { | ||
return DEFAULT_PRIORITY; | ||
} | ||
} |
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
6 changes: 6 additions & 0 deletions
6
resources/META-INF/services/io.github.erdos.stencil.functions.FunctionProvider
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,6 @@ | ||
io.github.erdos.stencil.functions.DefaultCallFnProvider | ||
io.github.erdos.stencil.functions.BasicFunctions$Provider | ||
io.github.erdos.stencil.functions.StringFunctions$Provider | ||
io.github.erdos.stencil.functions.NumberFunctions$Provider | ||
io.github.erdos.stencil.functions.DateFunctions$Provider | ||
io.github.erdos.stencil.functions.LocaleFunctions$Provider |