All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.paganini2008.devtools.beans.streaming.Restrictions Maven / Gradle / Ivy

There is a newer version: 2.0.5
Show newest version
package com.github.paganini2008.devtools.beans.streaming;

import java.util.Collection;
import java.util.function.Predicate;

import com.github.paganini2008.devtools.Comparables;
import com.github.paganini2008.devtools.MatchMode;
import com.github.paganini2008.devtools.beans.BeanUtils;
import com.github.paganini2008.devtools.beans.PropertyUtils;

/**
 * 
 * Restrictions
 * 
 * @author Fred Feng
 * 
 * @version 1.0
 */
@SuppressWarnings("unchecked")
public abstract class Restrictions {

	public static  Predicate disjunction() {
		return e -> {
			return false;
		};
	}

	public static  Predicate conjunction() {
		return e -> {
			return true;
		};
	}

	public static > Predicate ne(String attributeName, T value) {
		return e -> {
			T result = (T) PropertyUtils.getProperty(e, attributeName);
			return Comparables.ne(result, value);
		};
	}

	public static > Predicate ne(String attributeName, Class requiredType, T value) {
		return e -> {
			T result = BeanUtils.getProperty(e, attributeName, requiredType);
			return Comparables.ne(result, value);
		};
	}

	public static > Predicate eq(String attributeName, T value) {
		return e -> {
			T result = (T) PropertyUtils.getProperty(e, attributeName);
			return Comparables.eq(result, value);
		};
	}

	public static > Predicate eq(String attributeName, Class requiredType, T value) {
		return e -> {
			T result = BeanUtils.getProperty(e, attributeName, requiredType);
			return Comparables.eq(result, value);
		};
	}

	public static > Predicate gte(String attributeName, T value) {
		return e -> {
			T result = (T) PropertyUtils.getProperty(e, attributeName);
			return Comparables.gte(result, value);
		};
	}

	public static > Predicate gte(String attributeName, Class requiredType, T value) {
		return e -> {
			T result = BeanUtils.getProperty(e, attributeName, requiredType);
			return Comparables.gte(result, value);
		};
	}

	public static > Predicate gt(String attributeName, T value) {
		return e -> {
			T result = (T) PropertyUtils.getProperty(e, attributeName);
			return Comparables.gt(result, value);
		};
	}

	public static > Predicate gt(String attributeName, Class requiredType, T value) {
		return e -> {
			T result = BeanUtils.getProperty(e, attributeName, requiredType);
			return Comparables.gt(result, value);
		};
	}

	public static > Predicate lte(String attributeName, T value) {
		return e -> {
			T result = (T) PropertyUtils.getProperty(e, attributeName);
			return Comparables.lte(result, value);
		};
	}

	public static > Predicate lte(String attributeName, Class requiredType, T value) {
		return e -> {
			T result = BeanUtils.getProperty(e, attributeName, requiredType);
			return Comparables.lte(result, value);
		};
	}

	public static > Predicate lt(String attributeName, T value) {
		return e -> {
			T result = (T) PropertyUtils.getProperty(e, attributeName);
			return Comparables.lt(result, value);
		};
	}

	public static > Predicate lt(String attributeName, Class requiredType, T value) {
		return e -> {
			T result = BeanUtils.getProperty(e, attributeName, requiredType);
			return Comparables.lt(result, value);
		};
	}

	public static > Predicate between(String attributeName, T minValue, T maxValue) {
		return e -> {
			T result = (T) PropertyUtils.getProperty(e, attributeName);
			return Comparables.between(result, minValue, maxValue);
		};
	}

	public static > Predicate between(String attributeName, Class requiredType, T minValue, T maxValue) {
		return e -> {
			T result = BeanUtils.getProperty(e, attributeName, requiredType);
			return Comparables.between(result, minValue, maxValue);
		};
	}

	public static  Predicate in(String attributeName, Collection values) {
		return e -> {
			Object result = PropertyUtils.getProperty(e, attributeName);
			return values.contains(result);
		};
	}

	public static  Predicate in(String attributeName, Class requiredType, Collection values) {
		return e -> {
			Object result = BeanUtils.getProperty(e, attributeName, requiredType);
			return values.contains(result);
		};
	}

	public static  Predicate notNull(String attributeName) {
		return e -> {
			Object result = PropertyUtils.getProperty(e, attributeName);
			return result != null;
		};
	}

	public static  Predicate isNull(String attributeName) {
		return e -> {
			Object result = PropertyUtils.getProperty(e, attributeName);
			return result == null;
		};
	}

	public static  Predicate like(String attributeName, String substr, MatchMode matchMode) {
		return e -> {
			String result = (String) PropertyUtils.getProperty(e, attributeName);
			return matchMode.matches(result, substr);
		};
	}

	public static  Predicate or(Predicate... predicates) {
		Predicate result = disjunction();
		for (Predicate predicate : predicates) {
			result = result.or(predicate);
		}
		return result;
	}

	public static  Predicate and(Predicate... predicates) {
		Predicate result = conjunction();
		for (Predicate predicate : predicates) {
			result = result.and(predicate);
		}
		return result;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy