[java-support] branch main updated: Add a "Y" adapter that turns a Predicate into one of 2 functions.

Scott Cantor cantor.2 at osu.edu
Mon Jan 4 19:18:15 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=9066294a96d23e0758fcf9ce6c23f246004be32a

The following commit(s) were added to refs/heads/main by this push:
       new  9066294   Add a "Y" adapter that turns a Predicate into one of 2 functions.
9066294 is described below

commit 9066294a96d23e0758fcf9ce6c23f246004be32a
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Mon Jan 4 14:18:10 2021 -0500

    Add a "Y" adapter that turns a Predicate into one of 2 functions.
---
 .../support/logic/PredicateDrivenFunction.java     | 73 ++++++++++++++++++++++
 1 file changed, 73 insertions(+)

diff --git a/src/main/java/net/shibboleth/utilities/java/support/logic/PredicateDrivenFunction.java b/src/main/java/net/shibboleth/utilities/java/support/logic/PredicateDrivenFunction.java
new file mode 100644
index 0000000..0ef0c71
--- /dev/null
+++ b/src/main/java/net/shibboleth/utilities/java/support/logic/PredicateDrivenFunction.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.shibboleth.utilities.java.support.logic;
+
+import java.util.function.Function;
+import java.util.function.Predicate;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import javax.annotation.concurrent.ThreadSafe;
+
+import net.shibboleth.utilities.java.support.annotation.ParameterName;
+
+/**
+ * A {@link Function} that runs a {@link Predicate} and then runs one of two other possible
+ * functions based on the result.
+ * 
+ * <p>If either function is null, the result will be null if that function were needed.</p>
+ * 
+ * @param <T> type of input accepted by this function
+ * @param <U> type of output produced by this function
+ * 
+ * @since 8.2.0
+ */
+ at ThreadSafe
+public class PredicateDrivenFunction<T,U> implements Function<T,U> {
+
+    /** A predicate to apply. */
+    @Nonnull private final Predicate<? super T> predicate;
+
+    /** Function to apply if predicate is true. */
+    @Nullable private final Function<? super T,U> trueFunction;
+
+    /** Function to apply if predicate is false. */
+    @Nullable private final Function<? super T,U> falseFunction;
+
+    /**
+     * Constructor.
+     * 
+     * @param condition     predicate to apply
+     * @param whenTrue      function to apply if predicate evaluates to true
+     * @param whenFalse     function to apply if predicate evaluates to false
+     */
+    public PredicateDrivenFunction(@Nonnull @ParameterName(name="condition") final Predicate<? super T> condition,
+            @Nullable @ParameterName(name="whenTrue") final Function<? super T,U> whenTrue,
+            @Nullable @ParameterName(name="whenFalse") final Function<? super T,U> whenFalse) {
+        
+        predicate = Constraint.isNotNull(condition, "Input predicate cannot be null");
+        trueFunction = whenTrue != null ? whenTrue : FunctionSupport.constant(null);
+        falseFunction = whenFalse != null ? whenFalse : FunctionSupport.constant(null);
+    }
+
+    /** {@inheritDoc} */
+    public U apply(@Nullable final T input) {
+        return predicate.test(input) ? trueFunction.apply(input) : falseFunction.apply(input);
+    }
+
+}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list