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
This commit is contained in:
andrzej@threerings.net
2008-09-17 01:15:48 +00:00
parent 6245c3dd44
commit f1a2723292
@@ -140,6 +140,26 @@ public abstract class Predicate<T>
protected Predicate<T> _pred;
}
/**
* Returns a type-safe reference to the shared instance of a predicate that always returns
* <code>true</code>.
*/
@SuppressWarnings("unchecked")
public static <T> Predicate<T> trueInstance ()
{
return TRUE_INSTANCE;
}
/**
* Returns a type-safe reference to the shared instance of a predicate that always returns
* <code>false</code>.
*/
@SuppressWarnings("unchecked")
public static <T> Predicate<T> falseInstance ()
{
return FALSE_INSTANCE;
}
//--------------------------------------------------------------------
// Here's the sole abstract method in the Predicate class:
@@ -290,4 +310,18 @@ public abstract class Predicate<T>
}
};
}
/** 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;
}
};
}