[java-shib-shared] 08/12: Add true/false BiPredicates.

Scott Cantor cantor.2 at osu.edu
Wed Jan 31 14:15:55 UTC 2024


This is an automated email from the git hooks/post-receive script.

scantor pushed a commit to branch dev/thymeleaf
in repository java-shib-shared.

View the commit online:
http://git.shibboleth.net/view/?p=java-shib-shared.git;a=commit;h=328b0f9c124ec712985089488b1dc4f4b877d596

commit 328b0f9c124ec712985089488b1dc4f4b877d596
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Thu Dec 28 09:46:54 2023 -0500

    Add true/false BiPredicates.
---
 .../shared/logic/BiPredicateSupport.java           | 65 +++++++++++++++++++++-
 1 file changed, 64 insertions(+), 1 deletion(-)

diff --git a/shib-support/src/main/java/net/shibboleth/shared/logic/BiPredicateSupport.java b/shib-support/src/main/java/net/shibboleth/shared/logic/BiPredicateSupport.java
index 9b29269a..37c18a5f 100644
--- a/shib-support/src/main/java/net/shibboleth/shared/logic/BiPredicateSupport.java
+++ b/shib-support/src/main/java/net/shibboleth/shared/logic/BiPredicateSupport.java
@@ -31,7 +31,7 @@ public final class BiPredicateSupport {
     /** Constructor. */
     private BiPredicateSupport() {
     }
-    
+
     /**
      * Creates a {@link BiPredicate} that applies a {@link BiFunction} to inputs and returns its result,
      * or a default value if null.
@@ -54,6 +54,34 @@ public final class BiPredicateSupport {
             }
         };
     }
+
+    /**
+     * Returns a {@link BiPredicate} that always evaluates to true.
+     * 
+     * @param <T> first argument type
+     * @param <U> second argument type
+     * 
+     * @return the conforming BiPredicate
+     * 
+     * @since 9.1.0
+     */
+    @Nonnull public static <T extends Object,U extends Object> BiPredicate<T,U> alwaysTrue() {
+        return ObjectBiPredicate.ALWAYS_TRUE.withNarrowedType();
+    }
+
+    /**
+     * Returns a {@link BiPredicate} that always evaluates to false.
+     * 
+     * @param <T> first argument type
+     * @param <U> second argument type
+     * 
+     * @return the conforming BiPredicate
+     * 
+     * @since 9.1.0
+     */
+    @Nonnull public static <T extends Object,U extends Object> BiPredicate<T,U> alwaysFalse() {
+        return ObjectBiPredicate.ALWAYS_FALSE.withNarrowedType();
+    }
     
     /**
      * Returns a {@link BiPredicate} that evaluates to {@code true} if the given {@link BiPredicate} evaluates to {@code
@@ -236,4 +264,39 @@ public final class BiPredicateSupport {
         return result;
     }
     
+    /**
+     * Private enum to optimize the true/false objects.
+     * 
+     * @since 9.1.0
+     */
+    enum ObjectBiPredicate implements BiPredicate<Object,Object> {
+        
+        /** @see BiPredicateSupport#alwaysTrue() */
+        ALWAYS_TRUE {
+            public boolean test(@Nullable final Object a, @Nullable final Object b) {
+                return true;
+            }
+        },
+        
+        /** @see BiPredicateSupport#alwaysFalse() */
+        ALWAYS_FALSE {
+            public boolean test(@Nullable final Object a, @Nullable final Object b) {
+                return false;
+            }
+        };
+
+        /**
+         * Internalizes the cast for generic use.
+         * 
+         * @param <T> first argument type
+         * @param <U> second argument type
+         * 
+         * @return a casted object
+         */
+        @SuppressWarnings("unchecked") // safe contravariant cast
+        @Nonnull <T extends Object,U extends Object> BiPredicate<T,U> withNarrowedType() {
+            return (BiPredicate<T,U>) this;
+        }
+    }
+
 }
\ 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