Jump to content

[Snippet] How to negate/invert a Filter


Recommended Posts

Posted (edited)

A very basic snippet, I keep seeing people rewriting negations manually way too often.

Allows to you create inversions/negations of filters.

 

Example

		final Filter<String> IS_EMPTY = s -> s.isEmpty(); 
		final Filter<String> IS_NOT_EMPTY = negate(IS_EMPTY);

Snippet

	/**
	 * Returns a filter that represents the logical negation of the supplied filter.
	 * 
	 * @return a filter that represents the logical negation of the supplied filter
	 */
	public static final <T> Filter<T> negate(Filter<T> filter) {
		return e -> !filter.match(e);
	}
Edited by Botre
  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...