org.joo.libra.text.IsEmptyPredicate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of joo-libra Show documentation
Show all versions of joo-libra Show documentation
Java Predicate with SQL-like syntax support
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 + ")";
}
}