This is an automated email from the ASF dual-hosted git repository.
henrib pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jexl.git
The following commit(s) were added to refs/heads/master by this push:
new fd86178a JEXL: improve example on filter/map; keep inner classes close
to usage to ease comprehension when reading code
fd86178a is described below
commit fd86178aac2db17d102cf2e7513fdfde5b7c6af1
Author: Henri Biestro <[email protected]>
AuthorDate: Wed Nov 8 12:31:23 2023 +0100
JEXL: improve example on filter/map; keep inner classes close to usage to
ease comprehension when reading code
---
.../apache/commons/jexl3/examples/StreamTest.java | 64 +++++++---------------
1 file changed, 20 insertions(+), 44 deletions(-)
diff --git a/src/test/java/org/apache/commons/jexl3/examples/StreamTest.java
b/src/test/java/org/apache/commons/jexl3/examples/StreamTest.java
index b57cd720..962ca7b7 100644
--- a/src/test/java/org/apache/commons/jexl3/examples/StreamTest.java
+++ b/src/test/java/org/apache/commons/jexl3/examples/StreamTest.java
@@ -42,6 +42,26 @@ import org.junit.Test;
* A test around scripting streams.
*/
public class StreamTest {
+
+ /** Our engine instance. */
+ private final JexlEngine jexl;
+
+ public StreamTest() {
+ // Restricting features; no loops, no side effects
+ final JexlFeatures features = new JexlFeatures()
+ .loops(false)
+ .sideEffectGlobal(false)
+ .sideEffect(false);
+ // Restricted permissions to a safe set but with URI allowed
+ final JexlPermissions permissions = new
ClassPermissions(java.net.URI.class);
+ // Create the engine
+ jexl = new JexlBuilder()
+ .features(features)
+ .permissions(permissions)
+ .namespaces(Collections.singletonMap("URI", java.net.URI.class))
+ .create();
+ }
+
/**
* A MapContext that can operate on streams and collections.
*/
@@ -65,50 +85,6 @@ public class StreamTest {
public Stream<?> map(final Stream<?> stream, final JexlScript mapper) {
return stream.map( x -> mapper.execute(this, x));
}
-
- /**
- * This allows using a JEXL lambda as a filter.
- * @param collection the collection
- * @param filter the lambda to use as filter
- * @return the filtered result as a list
- */
- public List<?> filter(Collection<?> collection, final JexlScript
filter) {
- return collection.stream()
- .filter(x -> x != null && TRUE.equals(filter.execute(this, x)))
- .collect(Collectors.toList());
- }
-
- /**
- * This allows using a JEXL lambda as a mapper.
- * @param collection the collection
- * @param mapper the lambda to use as mapper
- * @return the mapped result as a list
- */
- public List<?> map(Collection<?> collection, final JexlScript mapper) {
- return collection.stream()
- .map(x -> mapper.execute(this, x))
- .filter(Objects::nonNull)
- .collect(Collectors.toList());
- }
- }
-
- /** Our engine instance. */
- private final JexlEngine jexl;
-
- public StreamTest() {
- // Restricting features; no loops, no side effects
- final JexlFeatures features = new JexlFeatures()
- .loops(false)
- .sideEffectGlobal(false)
- .sideEffect(false);
- // Restricted permissions to a safe set but with URI allowed
- final JexlPermissions permissions = new
ClassPermissions(java.net.URI.class);
- // Create the engine
- jexl = new JexlBuilder()
- .features(features)
- .permissions(permissions)
- .namespaces(Collections.singletonMap("URI", java.net.URI.class))
- .create();
}
@Test