Jump to content

[Snippet ]Predicate <-> Filter adapter


Recommended Posts

Posted (edited)

Adapts an OSBot Filter to work as a Java Util Predicate and vice versa.

 

Useful if you don't wan't to rewrite your Predicate and / or Filter libraries but still want to enjoy the benefits provided by both worlds.

 

If someone has a cleaner method, please do let me know!

public class FilterPredicate<V> implements Filter<V>, Predicate<V> {
	
	protected boolean _test(V value) {
		return false;
	}

	@Override
	public boolean match(V value) {
		return _test(value);
	}

	@Override
	public boolean test(V value) {
		return _test(value);
	}

	// Virtual constructor for OSBot Filter.
	public FilterPredicate<V> fromFilter(Filter<V> filter) {
		return new FilterPredicate<V>() {
			@Override
			protected boolean _test(V value) {
				return filter.match(value);
			}
		};
	}

	// Virtual constructor for Java Util Predicate.
	public FilterPredicate<V> fromPredicate(Predicate<V> predicate) {
		return new FilterPredicate<V>() {
			@Override
			protected boolean _test(V value) {
				return predicate.test(value);
			}
		};
	}
	
}

Examples:

 * EXAMPLE A:

  Predicate<V> predicate = ...
  Filter<V> filter = new FilterPredicate<V>().fromPredicate(predicate);

 * EXAMPLE B:
 
  Filter<V> filter = ...
  Predicate<V> predicate = new FilterPredicate<V>().fromFilter(filter);
  
 * EXAMPLE C:
  
  PredicateFilter<V> god = new PredicateFilter<V>().fromPredicate(lambda);
  god.predicate_specific_method();
  god.filter_specific_method();
Edited by Traum
  • 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...