Jump to content

[Snippet ]Predicate <-> Filter adapter


Traum

Recommended Posts

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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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