From f1a2723292db4eccbc8f23a842a532f8b5b74902 Mon Sep 17 00:00:00 2001 From: "andrzej@threerings.net" Date: Wed, 17 Sep 2008 01:15:48 +0000 Subject: [PATCH] Added shared true and false predicate instances, with type-safe accessors (a la Collections.emptyList). git-svn-id: https://samskivert.googlecode.com/svn/trunk@2435 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/java/com/samskivert/util/Predicate.java | 34 +++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/java/com/samskivert/util/Predicate.java b/src/java/com/samskivert/util/Predicate.java index 18e9aa34..a9089ebb 100644 --- a/src/java/com/samskivert/util/Predicate.java +++ b/src/java/com/samskivert/util/Predicate.java @@ -140,6 +140,26 @@ public abstract class Predicate protected Predicate _pred; } + /** + * Returns a type-safe reference to the shared instance of a predicate that always returns + * true. + */ + @SuppressWarnings("unchecked") + public static Predicate trueInstance () + { + return TRUE_INSTANCE; + } + + /** + * Returns a type-safe reference to the shared instance of a predicate that always returns + * false. + */ + @SuppressWarnings("unchecked") + public static Predicate falseInstance () + { + return FALSE_INSTANCE; + } + //-------------------------------------------------------------------- // Here's the sole abstract method in the Predicate class: @@ -290,4 +310,18 @@ public abstract class Predicate } }; } + + /** A shared predicate instance that always matches its input. */ + protected static final Predicate TRUE_INSTANCE = new Predicate() { + public boolean isMatch (Object object) { + return true; + } + }; + + /** A shared predicate instance that never matches its input. */ + protected static final Predicate FALSE_INSTANCE = new Predicate() { + public boolean isMatch (Object object) { + return false; + } + }; }