Jump to content

[Snippet] Functional operators for Filter & Functional classes (negate, chain, and, or, ...)


Botre

Recommended Posts

Chain, negate, combine, ...

 

Filter Example:

	public static void main(String[] args) {
		Filter<String> IS_EMPTY =  s -> s.isEmpty();
		Filter<String> CONTAINS_O = s -> s.contains("o");
		
		String a = "";
		String b = "osbot";
		
		// Check if empty
		System.out.println(IS_EMPTY.match(a)); //true
		System.out.println(IS_EMPTY.match(b)); //false
		
		//Check if NOT empty
		System.out.println(FilterOperation.negate(IS_EMPTY).match(a)); //false
		System.out.println(FilterOperation.negate(IS_EMPTY).match(b)); //true
		
		// Check if empty OR contains o
		System.out.println(FilterOperation.or(IS_EMPTY, CONTAINS_O).match(a)); //true
		System.out.println(FilterOperation.or(IS_EMPTY, CONTAINS_O).match(b)); //true
		
		// Check if empty AND contains o
		System.out.println(FilterOperation.and(IS_EMPTY, CONTAINS_O).match(a)); //false
		System.out.println(FilterOperation.and(IS_EMPTY, CONTAINS_O).match(b)); //false
	}

Filter Operations:

package org.botre.functional;

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

public final class FilterOperation {
	
	private FilterOperation() {
		
	}
	
	/**
	 * 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<? super T> filter) {
		return e -> !filter.match(e);
	}
	
	/**
	 * Returns a filter that represents a short-circuiting logical AND of the supplied filters.
	 * 
	 * @return a filter that represents a short-circuiting logical AND of the supplied filters
	 */
	@SafeVarargs
	public static final <T> Filter<T> and(Filter<? super T>... filters) {
		if(filters.length < 1) throw new IllegalArgumentException();
		return new Filter<T>() {
			@Override
			public boolean match(T e) {
				for (Filter<? super T> filter : filters) if(!filter.match(e)) return false;
				return true;
			}
		};
	}
	
	/**
	 * Returns a composed filter that represents a short-circuiting logical OR of the supplied filters.
	 * 
	 * @return a composed filter that represents a short-circuiting logical OR of the supplied filters
	 */
	@SafeVarargs
	public static final <T> Filter<T> or(Filter<? super T>... filters) {
		if(filters.length < 1) throw new IllegalArgumentException();
		return new Filter<T>() {
			@Override
			public boolean match(T e) {
				for (Filter<? super T> filter : filters) if(filter.match(e)) return true;
				return false;
			}
		};
	}
	
}

Functional Operations:

package org.botre.functional;

import java.util.function.BooleanSupplier;
import java.util.function.Predicate;

public final class FunctionalOperation {

	private FunctionalOperation() {
		
	}
	
	/**
	 * Returns a composed boolean supplier that represents a short-circuiting logical AND of the supplied boolean suppliers.
	 * 
	 * @return a composed boolean supplier that represents a short-circuiting logical AND of the supplied boolean suppliers
	 */
	public static final BooleanSupplier and(BooleanSupplier... suppliers) {
		if(suppliers.length < 1) throw new IllegalArgumentException();
		return () -> {
			for (BooleanSupplier s : suppliers) if(!s.getAsBoolean()) return false;
			return true;
		};
	}
	
	/**
	 * Returns a composed boolean supplier that represents a short-circuiting logical OR of the supplied boolean suppliers.
	 * 
	 * @return a composed boolean supplier that represents a short-circuiting logical OR of the supplied boolean suppliers
	 */
	public static final BooleanSupplier or(BooleanSupplier... suppliers) {
		if(suppliers.length < 1) throw new IllegalArgumentException();
		return () -> {
			for (BooleanSupplier s : suppliers) if(s.getAsBoolean()) return true;
			return false;
		};
	}
	
	/**
	 * Returns a composed predicate that represents a short-circuiting logical AND of the supplied predicates.
	 * 
	 * @return a composed predicate that represents a short-circuiting logical AND of the supplied predicates
	 */
	@SafeVarargs
	public static final <T> Predicate<T> and(Predicate<T>... predicates) {
		if(predicates.length < 1) throw new IllegalArgumentException();
		Predicate<T> result = predicates[0];
		for (int i = 1; i < predicates.length; i++) {
			result  = result.and(predicates[i]);
		}
		return result;
	}
	
	/**
	 * Returns a composed predicate that represents a short-circuiting logical OR of the supplied predicates.
	 * 
	 * @return a composed predicate that represents a short-circuiting logical OR of the supplied predicates
	 */
	@SafeVarargs
	public static final <T> Predicate<T> or(Predicate<T>... predicates) {
		if(predicates.length < 1) throw new IllegalArgumentException();
		Predicate<T> result = predicates[0];
		for (int i = 1; i < predicates.length; i++) {
			result  = result.or(predicates[i]);
		}
		return result;
	}
	
}
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...