[java-support] branch main updated: JSPT-109 - Add scripted variants of additional functional interfaces
Scott Cantor
cantor.2 at osu.edu
Tue Feb 23 15:47:04 UTC 2021
This is an automated email from the git hooks/post-receive script.
scantor pushed a commit to branch main
in repository java-support.
View the commit online:
http://git.shibboleth.net/view/?p=java-support.git;a=commit;h=c12e26bfe1239b4a92babf984a2c1c448220d55e
The following commit(s) were added to refs/heads/main by this push:
new c12e26b JSPT-109 - Add scripted variants of additional functional interfaces
c12e26b is described below
commit c12e26bfe1239b4a92babf984a2c1c448220d55e
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Tue Feb 23 10:46:35 2021 -0500
JSPT-109 - Add scripted variants of additional functional interfaces
https://issues.shibboleth.net/jira/browse/JSPT-109
---
...riptedFunction.java => ScriptedBiConsumer.java} | 123 ++++++++++-----------
...riptedFunction.java => ScriptedBiFunction.java} | 123 ++++++++++++---------
...iptedFunction.java => ScriptedBiPredicate.java} | 123 +++++++++++----------
...ScriptedFunction.java => ScriptedConsumer.java} | 71 ++++--------
.../java/support/logic/ScriptedFunction.java | 4 +-
.../java/support/logic/ScriptedPredicate.java | 33 +++++-
6 files changed, 251 insertions(+), 226 deletions(-)
diff --git a/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedFunction.java b/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedBiConsumer.java
similarity index 57%
copy from src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedFunction.java
copy to src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedBiConsumer.java
index 498c5bb..3628471 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedFunction.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedBiConsumer.java
@@ -20,7 +20,7 @@ package net.shibboleth.utilities.java.support.logic;
import java.io.IOException;
import java.io.InputStream;
-import java.util.function.Function;
+import java.util.function.BiConsumer;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@@ -29,6 +29,7 @@ import javax.script.ScriptException;
import net.shibboleth.utilities.java.support.annotation.ParameterName;
import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+import net.shibboleth.utilities.java.support.collection.Pair;
import net.shibboleth.utilities.java.support.resource.Resource;
import net.shibboleth.utilities.java.support.scripting.AbstractScriptEvaluator;
import net.shibboleth.utilities.java.support.scripting.EvaluableScript;
@@ -37,19 +38,22 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * A {@link Function} which calls out to a supplied script.
+ * A {@link BiConsumer} which calls out to a supplied script.
*
- * @param <T> input type
- * @param <U> output type
- * @since 7.4.0
+ * @param <T> first input type
+ * @param <U> second input type
+ * @since 8.2.0
*/
-public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements Function<T,U> {
+public class ScriptedBiConsumer<T,U> extends AbstractScriptEvaluator implements BiConsumer<T,U> {
/** Class logger. */
- @Nonnull private final Logger log = LoggerFactory.getLogger(ScriptedFunction.class);
+ @Nonnull private final Logger log = LoggerFactory.getLogger(ScriptedBiConsumer.class);
- /** Input Type.*/
- @Nullable private Class<T> inputTypeClass;
+ /** Input type 1. */
+ @Nullable private Class<T> inputTypeClass1;
+
+ /** Input type 2. */
+ @Nullable private Class<U> inputTypeClass2;
/**
* Constructor.
@@ -57,10 +61,10 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
* @param theScript the script we will evaluate.
* @param extraInfo debugging information.
*/
- protected ScriptedFunction(@Nonnull @NotEmpty @ParameterName(name="theScript") final EvaluableScript theScript,
+ protected ScriptedBiConsumer(@Nonnull @NotEmpty @ParameterName(name="theScript") final EvaluableScript theScript,
@Nullable @NotEmpty @ParameterName(name="extraInfo") final String extraInfo) {
super(theScript);
- setLogPrefix("Scripted Function from " + extraInfo + ":");
+ setLogPrefix("Scripted BiConsumer from " + extraInfo + ":");
}
/**
@@ -68,18 +72,9 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
*
* @param theScript the script we will evaluate.
*/
- protected ScriptedFunction(@Nonnull @NotEmpty @ParameterName(name="theScript") final EvaluableScript theScript) {
+ protected ScriptedBiConsumer(@Nonnull @NotEmpty @ParameterName(name="theScript") final EvaluableScript theScript) {
super(theScript);
- setLogPrefix("Anonymous Function:");
- }
-
- /**
- * Set the output type to be enforced.
- *
- * @param type output type
- */
- @Override public void setOutputType(@Nullable final Class<?> type) {
- super.setOutputType(type);
+ setLogPrefix("Anonymous BiConsumer:");
}
/**
@@ -87,52 +82,52 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
*
* @return input type
*/
- @Nullable public Class<T> getInputType() {
- return inputTypeClass;
+ @Nullable public Pair<Class<T>,Class<U>> getInputTypes() {
+ return new Pair<>(inputTypeClass1, inputTypeClass2);
}
/**
* Set the input type to be enforced.
*
- * @param type input type
+ * @param type1 first input type
+ * @param type2 second input type
*/
- public void setInputType(@Nullable final Class<T> type) {
- inputTypeClass = type;
- }
-
- /**
- * Set value to return if an error occurs.
- *
- * @param value value to return
- */
- @Override public void setReturnOnError(@Nullable final Object value) {
- super.setReturnOnError(value);
+ public void setInputTypes(@Nullable final Class<T> type1, @Nullable final Class<U> type2) {
+ inputTypeClass1 = type1;
+ inputTypeClass2 = type2;
}
/** {@inheritDoc} */
- @SuppressWarnings("unchecked")
- public U apply(@Nullable final T input) {
-
- if (null != getInputType() && null != input && !getInputType().isInstance(input)) {
- log.error("{} Input of type {} was not of type {}", getLogPrefix(), input.getClass(),
- getInputType());
- return (U) getReturnOnError();
+ public void accept(@Nullable final T first, @Nullable final U second) {
+
+ final Pair<Class<T>,Class<U>> types = getInputTypes();
+ if (null != types) {
+ if (null != first && !types.getFirst().isInstance(first)) {
+ log.error("{} Input of type {} was not of type {}", getLogPrefix(), first.getClass(), types.getFirst());
+ return;
+ }
+ if (null != second && !types.getSecond().isInstance(second)) {
+ log.error("{} Input of type {} was not of type {}", getLogPrefix(), second.getClass(),
+ types.getSecond());
+ return;
+ }
}
- return (U) evaluate(input);
+ evaluate(first, second);
}
/** {@inheritDoc} */
@Override
protected void prepareContext(@Nonnull final ScriptContext scriptContext, @Nullable final Object... input) {
- scriptContext.setAttribute("input", input[0], ScriptContext.ENGINE_SCOPE);
+ scriptContext.setAttribute("input1", input[0], ScriptContext.ENGINE_SCOPE);
+ scriptContext.setAttribute("input2", input[1], ScriptContext.ENGINE_SCOPE);
}
/**
- * Factory to create {@link ScriptedFunction} from a {@link Resource}.
+ * Factory to create {@link ScriptedBiConsumer} from a {@link Resource}.
*
- * @param <T> input type
- * @param <U> output type
+ * @param <T> first input type
+ * @param <U> second input type
* @param resource the resource to look at
* @param engineName the language
*
@@ -142,22 +137,22 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
* @throws IOException if the file doesn't exist.
*/
@SuppressWarnings("removal")
- public static <T,U> ScriptedFunction<T,U> resourceScript(@Nonnull @NotEmpty final String engineName,
+ public static <T,U> ScriptedBiConsumer<T,U> resourceScript(@Nonnull @NotEmpty final String engineName,
@Nonnull final Resource resource) throws ScriptException, IOException {
try (final InputStream is = resource.getInputStream()) {
final EvaluableScript script = new EvaluableScript();
script.setEngineName(engineName);
script.setScript(is);
script.initializeWithScriptException();
- return new ScriptedFunction<>(script, resource.getDescription());
+ return new ScriptedBiConsumer<>(script, resource.getDescription());
}
}
/**
- * Factory to create {@link ScriptedFunction} from a {@link Resource}.
+ * Factory to create {@link ScriptedBiConsumer} from a {@link Resource}.
*
- * @param <T> input type
- * @param <U> output type
+ * @param <T> first input type
+ * @param <U> second input type
* @param resource the resource to look at
*
* @return the function
@@ -165,16 +160,16 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
* @throws ScriptException if the compile fails
* @throws IOException if the file doesn't exist.
*/
- public static <T,U> ScriptedFunction<T,U> resourceScript(final Resource resource)
+ public static <T,U> ScriptedBiConsumer<T,U> resourceScript(final Resource resource)
throws ScriptException, IOException {
return resourceScript(DEFAULT_ENGINE, resource);
}
/**
- * Factory to create {@link ScriptedFunction} from inline data.
+ * Factory to create {@link ScriptedBiConsumer} from inline data.
*
- * @param <T> input type
- * @param <U> output type
+ * @param <T> first input type
+ * @param <U> second input type
* @param scriptSource the script, as a string
* @param engineName the language
*
@@ -183,20 +178,20 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
* @throws ScriptException if the compile fails
*/
@SuppressWarnings("removal")
- public static <T,U> ScriptedFunction<T,U> inlineScript(@Nonnull @NotEmpty final String engineName,
+ public static <T,U> ScriptedBiConsumer<T,U> inlineScript(@Nonnull @NotEmpty final String engineName,
@Nonnull @NotEmpty final String scriptSource) throws ScriptException {
final EvaluableScript script = new EvaluableScript();
script.setEngineName(engineName);
script.setScript(scriptSource);
script.initializeWithScriptException();
- return new ScriptedFunction<>(script, "Inline");
+ return new ScriptedBiConsumer<>(script, "Inline");
}
/**
- * Factory to create {@link ScriptedFunction} from inline data.
+ * Factory to create {@link ScriptedBiConsumer} from inline data.
*
- * @param <T> input type
- * @param <U> output type
+ * @param <T> first input type
+ * @param <U> second input type
* @param scriptSource the script, as a string
*
* @return the function
@@ -204,11 +199,11 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
* @throws ScriptException if the compile fails
*/
@SuppressWarnings("removal")
- public static <T,U> ScriptedFunction<T,U> inlineScript(@Nonnull @NotEmpty final String scriptSource)
+ public static <T,U> ScriptedBiConsumer<T,U> inlineScript(@Nonnull @NotEmpty final String scriptSource)
throws ScriptException {
final EvaluableScript script = new EvaluableScript();
script.setScript(scriptSource);
script.initializeWithScriptException();
- return new ScriptedFunction<>(script, "Inline");
+ return new ScriptedBiConsumer<>(script, "Inline");
}
}
\ No newline at end of file
diff --git a/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedFunction.java b/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedBiFunction.java
similarity index 57%
copy from src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedFunction.java
copy to src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedBiFunction.java
index 498c5bb..4e5b0f1 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedFunction.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedBiFunction.java
@@ -20,7 +20,7 @@ package net.shibboleth.utilities.java.support.logic;
import java.io.IOException;
import java.io.InputStream;
-import java.util.function.Function;
+import java.util.function.BiFunction;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@@ -29,6 +29,7 @@ import javax.script.ScriptException;
import net.shibboleth.utilities.java.support.annotation.ParameterName;
import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+import net.shibboleth.utilities.java.support.collection.Pair;
import net.shibboleth.utilities.java.support.resource.Resource;
import net.shibboleth.utilities.java.support.scripting.AbstractScriptEvaluator;
import net.shibboleth.utilities.java.support.scripting.EvaluableScript;
@@ -37,19 +38,23 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * A {@link Function} which calls out to a supplied script.
+ * A {@link BiFunction} which calls out to a supplied script.
*
- * @param <T> input type
- * @param <U> output type
- * @since 7.4.0
+ * @param <T> first input type
+ * @param <U> second input type
+ * @param <V> return type
+ * @since 8.2.0
*/
-public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements Function<T,U> {
+public class ScriptedBiFunction<T,U,V> extends AbstractScriptEvaluator implements BiFunction<T,U,V> {
/** Class logger. */
- @Nonnull private final Logger log = LoggerFactory.getLogger(ScriptedFunction.class);
+ @Nonnull private final Logger log = LoggerFactory.getLogger(ScriptedBiFunction.class);
- /** Input Type.*/
- @Nullable private Class<T> inputTypeClass;
+ /** Input type 1. */
+ @Nullable private Class<T> inputTypeClass1;
+
+ /** Input type 2. */
+ @Nullable private Class<U> inputTypeClass2;
/**
* Constructor.
@@ -57,10 +62,10 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
* @param theScript the script we will evaluate.
* @param extraInfo debugging information.
*/
- protected ScriptedFunction(@Nonnull @NotEmpty @ParameterName(name="theScript") final EvaluableScript theScript,
+ protected ScriptedBiFunction(@Nonnull @NotEmpty @ParameterName(name="theScript") final EvaluableScript theScript,
@Nullable @NotEmpty @ParameterName(name="extraInfo") final String extraInfo) {
super(theScript);
- setLogPrefix("Scripted Function from " + extraInfo + ":");
+ setLogPrefix("Scripted BiFunction from " + extraInfo + ":");
}
/**
@@ -68,36 +73,38 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
*
* @param theScript the script we will evaluate.
*/
- protected ScriptedFunction(@Nonnull @NotEmpty @ParameterName(name="theScript") final EvaluableScript theScript) {
+ protected ScriptedBiFunction(@Nonnull @NotEmpty @ParameterName(name="theScript") final EvaluableScript theScript) {
super(theScript);
- setLogPrefix("Anonymous Function:");
+ setLogPrefix("Anonymous BiFunction:");
}
/**
- * Set the output type to be enforced.
+ * Get the input type to be enforced.
*
- * @param type output type
+ * @return input type
*/
- @Override public void setOutputType(@Nullable final Class<?> type) {
- super.setOutputType(type);
+ @Nullable public Pair<Class<T>,Class<U>> getInputTypes() {
+ return new Pair<>(inputTypeClass1, inputTypeClass2);
}
/**
- * Get the input type to be enforced.
+ * Set the input type to be enforced.
*
- * @return input type
+ * @param type1 first input type
+ * @param type2 second input type
*/
- @Nullable public Class<T> getInputType() {
- return inputTypeClass;
+ public void setInputTypes(@Nullable final Class<T> type1, @Nullable final Class<U> type2) {
+ inputTypeClass1 = type1;
+ inputTypeClass2 = type2;
}
/**
- * Set the input type to be enforced.
+ * Set the output type to be enforced.
*
- * @param type input type
+ * @param type output type
*/
- public void setInputType(@Nullable final Class<T> type) {
- inputTypeClass = type;
+ @Override public void setOutputType(@Nullable final Class<?> type) {
+ super.setOutputType(type);
}
/**
@@ -111,28 +118,37 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
- public U apply(@Nullable final T input) {
-
- if (null != getInputType() && null != input && !getInputType().isInstance(input)) {
- log.error("{} Input of type {} was not of type {}", getLogPrefix(), input.getClass(),
- getInputType());
- return (U) getReturnOnError();
+ @Nullable public V apply(@Nullable final T first, @Nullable final U second) {
+
+ final Pair<Class<T>,Class<U>> types = getInputTypes();
+ if (null != types) {
+ if (null != first && !types.getFirst().isInstance(first)) {
+ log.error("{} Input of type {} was not of type {}", getLogPrefix(), first.getClass(), types.getFirst());
+ return (V) getReturnOnError();
+ }
+ if (null != second && !types.getSecond().isInstance(second)) {
+ log.error("{} Input of type {} was not of type {}", getLogPrefix(), second.getClass(),
+ types.getSecond());
+ return (V) getReturnOnError();
+ }
}
- return (U) evaluate(input);
+ return (V) evaluate(first, second);
}
/** {@inheritDoc} */
@Override
protected void prepareContext(@Nonnull final ScriptContext scriptContext, @Nullable final Object... input) {
- scriptContext.setAttribute("input", input[0], ScriptContext.ENGINE_SCOPE);
+ scriptContext.setAttribute("input1", input[0], ScriptContext.ENGINE_SCOPE);
+ scriptContext.setAttribute("input2", input[1], ScriptContext.ENGINE_SCOPE);
}
/**
- * Factory to create {@link ScriptedFunction} from a {@link Resource}.
+ * Factory to create {@link ScriptedBiFunction} from a {@link Resource}.
*
- * @param <T> input type
- * @param <U> output type
+ * @param <T> first input type
+ * @param <U> second input type
+ * @param <V> return type
* @param resource the resource to look at
* @param engineName the language
*
@@ -142,22 +158,23 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
* @throws IOException if the file doesn't exist.
*/
@SuppressWarnings("removal")
- public static <T,U> ScriptedFunction<T,U> resourceScript(@Nonnull @NotEmpty final String engineName,
+ public static <T,U,V> ScriptedBiFunction<T,U,V> resourceScript(@Nonnull @NotEmpty final String engineName,
@Nonnull final Resource resource) throws ScriptException, IOException {
try (final InputStream is = resource.getInputStream()) {
final EvaluableScript script = new EvaluableScript();
script.setEngineName(engineName);
script.setScript(is);
script.initializeWithScriptException();
- return new ScriptedFunction<>(script, resource.getDescription());
+ return new ScriptedBiFunction<>(script, resource.getDescription());
}
}
/**
- * Factory to create {@link ScriptedFunction} from a {@link Resource}.
+ * Factory to create {@link ScriptedBiFunction} from a {@link Resource}.
*
- * @param <T> input type
- * @param <U> output type
+ * @param <T> first input type
+ * @param <U> second input type
+ * @param <V> return type
* @param resource the resource to look at
*
* @return the function
@@ -165,16 +182,17 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
* @throws ScriptException if the compile fails
* @throws IOException if the file doesn't exist.
*/
- public static <T,U> ScriptedFunction<T,U> resourceScript(final Resource resource)
+ public static <T,U,V> ScriptedBiFunction<T,U,V> resourceScript(final Resource resource)
throws ScriptException, IOException {
return resourceScript(DEFAULT_ENGINE, resource);
}
/**
- * Factory to create {@link ScriptedFunction} from inline data.
+ * Factory to create {@link ScriptedBiFunction} from inline data.
*
- * @param <T> input type
- * @param <U> output type
+ * @param <T> first input type
+ * @param <U> second input type
+ * @param <V> return type
* @param scriptSource the script, as a string
* @param engineName the language
*
@@ -183,20 +201,21 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
* @throws ScriptException if the compile fails
*/
@SuppressWarnings("removal")
- public static <T,U> ScriptedFunction<T,U> inlineScript(@Nonnull @NotEmpty final String engineName,
+ public static <T,U,V> ScriptedBiFunction<T,U,V> inlineScript(@Nonnull @NotEmpty final String engineName,
@Nonnull @NotEmpty final String scriptSource) throws ScriptException {
final EvaluableScript script = new EvaluableScript();
script.setEngineName(engineName);
script.setScript(scriptSource);
script.initializeWithScriptException();
- return new ScriptedFunction<>(script, "Inline");
+ return new ScriptedBiFunction<>(script, "Inline");
}
/**
- * Factory to create {@link ScriptedFunction} from inline data.
+ * Factory to create {@link ScriptedBiFunction} from inline data.
*
- * @param <T> input type
- * @param <U> output type
+ * @param <T> first input type
+ * @param <U> second input type
+ * @param <V> return type
* @param scriptSource the script, as a string
*
* @return the function
@@ -204,11 +223,11 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
* @throws ScriptException if the compile fails
*/
@SuppressWarnings("removal")
- public static <T,U> ScriptedFunction<T,U> inlineScript(@Nonnull @NotEmpty final String scriptSource)
+ public static <T,U,V> ScriptedBiFunction<T,U,V> inlineScript(@Nonnull @NotEmpty final String scriptSource)
throws ScriptException {
final EvaluableScript script = new EvaluableScript();
script.setScript(scriptSource);
script.initializeWithScriptException();
- return new ScriptedFunction<>(script, "Inline");
+ return new ScriptedBiFunction<>(script, "Inline");
}
}
\ No newline at end of file
diff --git a/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedFunction.java b/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedBiPredicate.java
similarity index 54%
copy from src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedFunction.java
copy to src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedBiPredicate.java
index 498c5bb..e4ff913 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedFunction.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedBiPredicate.java
@@ -20,7 +20,7 @@ package net.shibboleth.utilities.java.support.logic;
import java.io.IOException;
import java.io.InputStream;
-import java.util.function.Function;
+import java.util.function.BiPredicate;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@@ -29,6 +29,7 @@ import javax.script.ScriptException;
import net.shibboleth.utilities.java.support.annotation.ParameterName;
import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+import net.shibboleth.utilities.java.support.collection.Pair;
import net.shibboleth.utilities.java.support.resource.Resource;
import net.shibboleth.utilities.java.support.scripting.AbstractScriptEvaluator;
import net.shibboleth.utilities.java.support.scripting.EvaluableScript;
@@ -37,19 +38,22 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * A {@link Function} which calls out to a supplied script.
+ * A {@link BiPredicate} which calls out to a supplied script.
*
- * @param <T> input type
- * @param <U> output type
- * @since 7.4.0
+ * @param <T> first input type
+ * @param <U> second input type
+ * @since 8.2.0
*/
-public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements Function<T,U> {
+public class ScriptedBiPredicate<T,U> extends AbstractScriptEvaluator implements BiPredicate<T,U> {
/** Class logger. */
- @Nonnull private final Logger log = LoggerFactory.getLogger(ScriptedFunction.class);
+ @Nonnull private final Logger log = LoggerFactory.getLogger(ScriptedBiPredicate.class);
- /** Input Type.*/
- @Nullable private Class<T> inputTypeClass;
+ /** Input type 1. */
+ @Nullable private Class<T> inputTypeClass1;
+
+ /** Input type 2. */
+ @Nullable private Class<U> inputTypeClass2;
/**
* Constructor.
@@ -57,10 +61,10 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
* @param theScript the script we will evaluate.
* @param extraInfo debugging information.
*/
- protected ScriptedFunction(@Nonnull @NotEmpty @ParameterName(name="theScript") final EvaluableScript theScript,
+ protected ScriptedBiPredicate(@Nonnull @NotEmpty @ParameterName(name="theScript") final EvaluableScript theScript,
@Nullable @NotEmpty @ParameterName(name="extraInfo") final String extraInfo) {
super(theScript);
- setLogPrefix("Scripted Function from " + extraInfo + ":");
+ setLogPrefix("Scripted BiPredicate from " + extraInfo + ":");
}
/**
@@ -68,18 +72,9 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
*
* @param theScript the script we will evaluate.
*/
- protected ScriptedFunction(@Nonnull @NotEmpty @ParameterName(name="theScript") final EvaluableScript theScript) {
+ protected ScriptedBiPredicate(@Nonnull @NotEmpty @ParameterName(name="theScript") final EvaluableScript theScript) {
super(theScript);
- setLogPrefix("Anonymous Function:");
- }
-
- /**
- * Set the output type to be enforced.
- *
- * @param type output type
- */
- @Override public void setOutputType(@Nullable final Class<?> type) {
- super.setOutputType(type);
+ setLogPrefix("Anonymous BiPredicate:");
}
/**
@@ -87,52 +82,62 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
*
* @return input type
*/
- @Nullable public Class<T> getInputType() {
- return inputTypeClass;
+ @Nullable public Pair<Class<T>,Class<U>> getInputTypes() {
+ return new Pair<>(inputTypeClass1, inputTypeClass2);
}
/**
* Set the input type to be enforced.
*
- * @param type input type
+ * @param type1 first input type
+ * @param type2 second input type
*/
- public void setInputType(@Nullable final Class<T> type) {
- inputTypeClass = type;
+ public void setInputTypes(@Nullable final Class<T> type1, @Nullable final Class<U> type2) {
+ inputTypeClass1 = type1;
+ inputTypeClass2 = type2;
}
/**
* Set value to return if an error occurs.
- *
- * @param value value to return
+ *
+ * @param flag value to return
*/
- @Override public void setReturnOnError(@Nullable final Object value) {
- super.setReturnOnError(value);
+ public void setReturnOnError(final boolean flag) {
+ setReturnOnError(Boolean.valueOf(flag));
}
/** {@inheritDoc} */
- @SuppressWarnings("unchecked")
- public U apply(@Nullable final T input) {
-
- if (null != getInputType() && null != input && !getInputType().isInstance(input)) {
- log.error("{} Input of type {} was not of type {}", getLogPrefix(), input.getClass(),
- getInputType());
- return (U) getReturnOnError();
+ public boolean test(@Nullable final T first, @Nullable final U second) {
+
+ final Pair<Class<T>,Class<U>> types = getInputTypes();
+ if (null != types) {
+ if (null != first && !types.getFirst().isInstance(first)) {
+ log.error("{} Input of type {} was not of type {}", getLogPrefix(), first.getClass(), types.getFirst());
+ return (boolean) getReturnOnError();
+ }
+ if (null != second && !types.getSecond().isInstance(second)) {
+ log.error("{} Input of type {} was not of type {}", getLogPrefix(), second.getClass(),
+ types.getSecond());
+ return (boolean) getReturnOnError();
+ }
}
- return (U) evaluate(input);
+ final Object result = evaluate(first, second);
+ return (boolean) (result != null ? result : getReturnOnError());
}
/** {@inheritDoc} */
@Override
protected void prepareContext(@Nonnull final ScriptContext scriptContext, @Nullable final Object... input) {
- scriptContext.setAttribute("input", input[0], ScriptContext.ENGINE_SCOPE);
+ scriptContext.setAttribute("input1", input[0], ScriptContext.ENGINE_SCOPE);
+ scriptContext.setAttribute("input2", input[1], ScriptContext.ENGINE_SCOPE);
}
/**
- * Factory to create {@link ScriptedFunction} from a {@link Resource}.
+ * Factory to create {@link ScriptedBiPredicate} from a {@link Resource}.
*
- * @param <T> input type
- * @param <U> output type
+ * @param <T> first input type
+ * @param <U> second input type
* @param resource the resource to look at
* @param engineName the language
*
@@ -142,22 +147,22 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
* @throws IOException if the file doesn't exist.
*/
@SuppressWarnings("removal")
- public static <T,U> ScriptedFunction<T,U> resourceScript(@Nonnull @NotEmpty final String engineName,
+ public static <T,U> ScriptedBiPredicate<T,U> resourceScript(@Nonnull @NotEmpty final String engineName,
@Nonnull final Resource resource) throws ScriptException, IOException {
try (final InputStream is = resource.getInputStream()) {
final EvaluableScript script = new EvaluableScript();
script.setEngineName(engineName);
script.setScript(is);
script.initializeWithScriptException();
- return new ScriptedFunction<>(script, resource.getDescription());
+ return new ScriptedBiPredicate<>(script, resource.getDescription());
}
}
/**
- * Factory to create {@link ScriptedFunction} from a {@link Resource}.
+ * Factory to create {@link ScriptedBiPredicate} from a {@link Resource}.
*
- * @param <T> input type
- * @param <U> output type
+ * @param <T> first input type
+ * @param <U> second input type
* @param resource the resource to look at
*
* @return the function
@@ -165,16 +170,16 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
* @throws ScriptException if the compile fails
* @throws IOException if the file doesn't exist.
*/
- public static <T,U> ScriptedFunction<T,U> resourceScript(final Resource resource)
+ public static <T,U> ScriptedBiPredicate<T,U> resourceScript(final Resource resource)
throws ScriptException, IOException {
return resourceScript(DEFAULT_ENGINE, resource);
}
/**
- * Factory to create {@link ScriptedFunction} from inline data.
+ * Factory to create {@link ScriptedBiPredicate} from inline data.
*
- * @param <T> input type
- * @param <U> output type
+ * @param <T> first input type
+ * @param <U> second input type
* @param scriptSource the script, as a string
* @param engineName the language
*
@@ -183,20 +188,20 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
* @throws ScriptException if the compile fails
*/
@SuppressWarnings("removal")
- public static <T,U> ScriptedFunction<T,U> inlineScript(@Nonnull @NotEmpty final String engineName,
+ public static <T,U> ScriptedBiPredicate<T,U> inlineScript(@Nonnull @NotEmpty final String engineName,
@Nonnull @NotEmpty final String scriptSource) throws ScriptException {
final EvaluableScript script = new EvaluableScript();
script.setEngineName(engineName);
script.setScript(scriptSource);
script.initializeWithScriptException();
- return new ScriptedFunction<>(script, "Inline");
+ return new ScriptedBiPredicate<>(script, "Inline");
}
/**
- * Factory to create {@link ScriptedFunction} from inline data.
+ * Factory to create {@link ScriptedBiPredicate} from inline data.
*
- * @param <T> input type
- * @param <U> output type
+ * @param <T> first input type
+ * @param <U> second input type
* @param scriptSource the script, as a string
*
* @return the function
@@ -204,11 +209,11 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
* @throws ScriptException if the compile fails
*/
@SuppressWarnings("removal")
- public static <T,U> ScriptedFunction<T,U> inlineScript(@Nonnull @NotEmpty final String scriptSource)
+ public static <T,U> ScriptedBiPredicate<T,U> inlineScript(@Nonnull @NotEmpty final String scriptSource)
throws ScriptException {
final EvaluableScript script = new EvaluableScript();
script.setScript(scriptSource);
script.initializeWithScriptException();
- return new ScriptedFunction<>(script, "Inline");
+ return new ScriptedBiPredicate<>(script, "Inline");
}
}
\ No newline at end of file
diff --git a/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedFunction.java b/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedConsumer.java
similarity index 71%
copy from src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedFunction.java
copy to src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedConsumer.java
index 498c5bb..7c8a267 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedFunction.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedConsumer.java
@@ -20,7 +20,7 @@ package net.shibboleth.utilities.java.support.logic;
import java.io.IOException;
import java.io.InputStream;
-import java.util.function.Function;
+import java.util.function.Consumer;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@@ -37,16 +37,15 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * A {@link Function} which calls out to a supplied script.
+ * A {@link Consumer} which calls out to a supplied script.
*
* @param <T> input type
- * @param <U> output type
- * @since 7.4.0
+ * @since 8.2.0
*/
-public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements Function<T,U> {
+public class ScriptedConsumer<T> extends AbstractScriptEvaluator implements Consumer<T> {
/** Class logger. */
- @Nonnull private final Logger log = LoggerFactory.getLogger(ScriptedFunction.class);
+ @Nonnull private final Logger log = LoggerFactory.getLogger(ScriptedConsumer.class);
/** Input Type.*/
@Nullable private Class<T> inputTypeClass;
@@ -57,10 +56,10 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
* @param theScript the script we will evaluate.
* @param extraInfo debugging information.
*/
- protected ScriptedFunction(@Nonnull @NotEmpty @ParameterName(name="theScript") final EvaluableScript theScript,
+ protected ScriptedConsumer(@Nonnull @NotEmpty @ParameterName(name="theScript") final EvaluableScript theScript,
@Nullable @NotEmpty @ParameterName(name="extraInfo") final String extraInfo) {
super(theScript);
- setLogPrefix("Scripted Function from " + extraInfo + ":");
+ setLogPrefix("Scripted Consumer from " + extraInfo + ":");
}
/**
@@ -68,18 +67,9 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
*
* @param theScript the script we will evaluate.
*/
- protected ScriptedFunction(@Nonnull @NotEmpty @ParameterName(name="theScript") final EvaluableScript theScript) {
+ protected ScriptedConsumer(@Nonnull @NotEmpty @ParameterName(name="theScript") final EvaluableScript theScript) {
super(theScript);
- setLogPrefix("Anonymous Function:");
- }
-
- /**
- * Set the output type to be enforced.
- *
- * @param type output type
- */
- @Override public void setOutputType(@Nullable final Class<?> type) {
- super.setOutputType(type);
+ setLogPrefix("Anonymous Consumer:");
}
/**
@@ -100,26 +90,15 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
inputTypeClass = type;
}
- /**
- * Set value to return if an error occurs.
- *
- * @param value value to return
- */
- @Override public void setReturnOnError(@Nullable final Object value) {
- super.setReturnOnError(value);
- }
-
/** {@inheritDoc} */
- @SuppressWarnings("unchecked")
- public U apply(@Nullable final T input) {
+ public void accept(@Nullable final T input) {
if (null != getInputType() && null != input && !getInputType().isInstance(input)) {
log.error("{} Input of type {} was not of type {}", getLogPrefix(), input.getClass(),
getInputType());
- return (U) getReturnOnError();
+ } else {
+ evaluate(input);
}
-
- return (U) evaluate(input);
}
/** {@inheritDoc} */
@@ -129,10 +108,9 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
}
/**
- * Factory to create {@link ScriptedFunction} from a {@link Resource}.
+ * Factory to create {@link ScriptedConsumer} from a {@link Resource}.
*
* @param <T> input type
- * @param <U> output type
* @param resource the resource to look at
* @param engineName the language
*
@@ -142,22 +120,21 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
* @throws IOException if the file doesn't exist.
*/
@SuppressWarnings("removal")
- public static <T,U> ScriptedFunction<T,U> resourceScript(@Nonnull @NotEmpty final String engineName,
+ public static <T> ScriptedConsumer<T> resourceScript(@Nonnull @NotEmpty final String engineName,
@Nonnull final Resource resource) throws ScriptException, IOException {
try (final InputStream is = resource.getInputStream()) {
final EvaluableScript script = new EvaluableScript();
script.setEngineName(engineName);
script.setScript(is);
script.initializeWithScriptException();
- return new ScriptedFunction<>(script, resource.getDescription());
+ return new ScriptedConsumer<>(script, resource.getDescription());
}
}
/**
- * Factory to create {@link ScriptedFunction} from a {@link Resource}.
+ * Factory to create {@link ScriptedConsumer} from a {@link Resource}.
*
* @param <T> input type
- * @param <U> output type
* @param resource the resource to look at
*
* @return the function
@@ -165,16 +142,15 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
* @throws ScriptException if the compile fails
* @throws IOException if the file doesn't exist.
*/
- public static <T,U> ScriptedFunction<T,U> resourceScript(final Resource resource)
+ public static <T> ScriptedConsumer<T> resourceScript(final Resource resource)
throws ScriptException, IOException {
return resourceScript(DEFAULT_ENGINE, resource);
}
/**
- * Factory to create {@link ScriptedFunction} from inline data.
+ * Factory to create {@link ScriptedConsumer} from inline data.
*
* @param <T> input type
- * @param <U> output type
* @param scriptSource the script, as a string
* @param engineName the language
*
@@ -183,20 +159,19 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
* @throws ScriptException if the compile fails
*/
@SuppressWarnings("removal")
- public static <T,U> ScriptedFunction<T,U> inlineScript(@Nonnull @NotEmpty final String engineName,
+ public static <T> ScriptedConsumer<T> inlineScript(@Nonnull @NotEmpty final String engineName,
@Nonnull @NotEmpty final String scriptSource) throws ScriptException {
final EvaluableScript script = new EvaluableScript();
script.setEngineName(engineName);
script.setScript(scriptSource);
script.initializeWithScriptException();
- return new ScriptedFunction<>(script, "Inline");
+ return new ScriptedConsumer<>(script, "Inline");
}
/**
- * Factory to create {@link ScriptedFunction} from inline data.
+ * Factory to create {@link ScriptedConsumer} from inline data.
*
* @param <T> input type
- * @param <U> output type
* @param scriptSource the script, as a string
*
* @return the function
@@ -204,11 +179,11 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
* @throws ScriptException if the compile fails
*/
@SuppressWarnings("removal")
- public static <T,U> ScriptedFunction<T,U> inlineScript(@Nonnull @NotEmpty final String scriptSource)
+ public static <T> ScriptedConsumer<T> inlineScript(@Nonnull @NotEmpty final String scriptSource)
throws ScriptException {
final EvaluableScript script = new EvaluableScript();
script.setScript(scriptSource);
script.initializeWithScriptException();
- return new ScriptedFunction<>(script, "Inline");
+ return new ScriptedConsumer<>(script, "Inline");
}
}
\ No newline at end of file
diff --git a/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedFunction.java b/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedFunction.java
index 498c5bb..072f286 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedFunction.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedFunction.java
@@ -48,7 +48,7 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
/** Class logger. */
@Nonnull private final Logger log = LoggerFactory.getLogger(ScriptedFunction.class);
- /** Input Type.*/
+ /** Input type. */
@Nullable private Class<T> inputTypeClass;
/**
@@ -87,7 +87,7 @@ public class ScriptedFunction<T, U> extends AbstractScriptEvaluator implements F
*
* @return input type
*/
- @Nullable public Class<T> getInputType() {
+ @Nullable public Class<T> getInputType() {
return inputTypeClass;
}
diff --git a/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedPredicate.java b/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedPredicate.java
index c32a44b..a503d01 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedPredicate.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/logic/ScriptedPredicate.java
@@ -46,7 +46,10 @@ public class ScriptedPredicate<T> extends AbstractScriptEvaluator implements Pre
/** Class logger. */
@Nonnull private final Logger log = LoggerFactory.getLogger(ScriptedPredicate.class);
-
+
+ /** Input type. */
+ @Nullable private Class<T> inputTypeClass;
+
/**
* Constructor.
*
@@ -73,6 +76,28 @@ public class ScriptedPredicate<T> extends AbstractScriptEvaluator implements Pre
setReturnOnError(false);
}
+ /**
+ * Get the input type to be enforced.
+ *
+ * @return input type
+ *
+ * @since 8.2.0
+ */
+ @Nullable public Class<T> getInputType() {
+ return inputTypeClass;
+ }
+
+ /**
+ * Set the input type to be enforced.
+ *
+ * @param type input type
+ *
+ * @since 8.2.0
+ */
+ public void setInputType(@Nullable final Class<T> type) {
+ inputTypeClass = type;
+ }
+
/**
* Set value to return if an error occurs.
*
@@ -85,6 +110,12 @@ public class ScriptedPredicate<T> extends AbstractScriptEvaluator implements Pre
/** {@inheritDoc} */
public boolean test(@Nullable final T input) {
+ if (null != getInputType() && null != input && !getInputType().isInstance(input)) {
+ log.error("{} Input of type {} was not of type {}", getLogPrefix(), input.getClass(),
+ getInputType());
+ return (boolean) getReturnOnError();
+ }
+
final Object result = evaluate(input);
return (boolean) (result != null ? result : getReturnOnError());
}
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list