Jump to content

[Snippet ]Predicate <-> Filter adapter


Botre

Recommended Posts

Updated from: http://osbot.org/forum/topic/91630-snippet-predicate-filter-adapter/

 

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!

 

Example: 

package utils;

import java.util.function.Predicate;

import org.osbot.rs07.api.filter.Filter;
import org.osbot.rs07.api.model.NPC;

public class Example {

	public static void main(String[] args) {
		Filter<NPC> filter = npc -> npc.exists();
		Predicate<NPC> predicate = FilterPredicate.fromFilter(filter);
		filter = FilterPredicate.fromPredicate(predicate);
	}
	
}

Snippet:

package utils;

import java.util.function.Predicate;

import org.osbot.rs07.api.filter.Filter;

/*
 * Adapts an OSBot Filter to work as a Java Util Predicate and vice versa.
 * 
 */
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);
	}
	
	public static final <T> FilterPredicate<T> fromFilter(Filter<T> filter) {
		return new FilterPredicate<T>() {
			@Override
			protected boolean _test(T value) {
				return filter.match(value);
			}
		};
	}

	public static final <T> FilterPredicate<T> fromPredicate(Predicate<T> predicate) {
		return new FilterPredicate<T>() {
			@Override
			protected boolean _test(T value) {
				return predicate.test(value);
			}
		};
	}
	
}
Edited by Botre
  • Like 1
Link to comment
Share on other sites

  • 2 years later...

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...