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

org.joo.libra.text.IsEmptyPredicate Maven / Gradle / Ivy

There is a newer version: 2.1.2
Show newest version
package org.joo.libra.text;

import java.util.Collection;

import org.joo.libra.PredicateContext;
import org.joo.libra.common.CompositionPredicate;
import org.joo.libra.common.HasValue;

/**
 * Represents a is empty predicate. It supports
 * String, Collection and Array. Also if
 * the value is null, it will always return true regardless of the type.
 * 
 * @author griever
 *
 */
public class IsEmptyPredicate implements CompositionPredicate {

	private HasValue value;

	public IsEmptyPredicate(final HasValue value) {
		this.value = value;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public boolean satisfiedBy(final PredicateContext context) {
		Object rawValue = value.getValue(context);
		if (rawValue == null)
			return true;
		if (rawValue instanceof String)
			return rawValue.toString().isEmpty();
		if (rawValue instanceof Collection)
			return ((Collection) rawValue).isEmpty();
		if (rawValue instanceof Object[])
			return ((Object[]) rawValue).length == 0;
		return false;
	}

	public String toString() {
		return "IS_EMPTY(" + value + ")";
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy