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

com.jdroid.javaweb.guava.predicate.ContainsPropertyPredicate Maven / Gradle / Ivy

The newest version!
package com.jdroid.javaweb.guava.predicate;

import com.google.common.base.Objects;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.jdroid.javaweb.guava.function.PropertyFunction;

import java.io.Serializable;
import java.util.Collection;

/**
 * Predicates that verifies if the input value is contained by the property represent by the propertyName or viceversa.
 * 
 * @param  The object type
 * 
 */
public class ContainsPropertyPredicate implements Predicate, Serializable {
	
	private Object value;
	private PropertyFunction propertyFunction;
	private boolean valueIsCollection;
	
	/**
	 * @param propertyName The propertyName The property name of the main object to get the property value
	 * @param value The value or collection of values to evaluate the contains
	 */
	public ContainsPropertyPredicate(String propertyName, Object value) {
		this.propertyFunction = new PropertyFunction<>(propertyName);
		this.value = value;
		this.valueIsCollection = Collection.class.isInstance(this.value);
	}
	
	/**
	 * @see com.google.common.base.Predicate#apply(java.lang.Object)
	 */
	@Override
	public boolean apply(T input) {
		Object object = this.propertyFunction.apply(input);
		if (valueIsCollection) {
			return contains(Collection.class.cast(this.value), object);
		} else if (Collection.class.isInstance(object)) {
			return contains(Collection.class.cast(object), this.value);
		}
		return Objects.equal(this.value, object);
	}
	
	private boolean contains(Collection collection, Object object) {
		return Iterables.contains(collection, object);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy