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

net.anotheria.anodoc.query2.QueryContainsProperty Maven / Gradle / Ivy

package net.anotheria.anodoc.query2;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.anotheria.anodoc.data.Property;
import net.anotheria.util.StringUtils;

/**
 * Query to List/Array Property. Passes only if Property contains all queried values.
 * 
 * IMPORTANT:Not properly tested yet. Use on own risk!!!!
 * 
 * @author denis
 *
 */
public class QueryContainsProperty extends QueryProperty{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -8649073486730051958L;
	
	public QueryContainsProperty(String aName, T... aValues){
		this(aName, Arrays.asList(aValues));
	}

	public QueryContainsProperty(String aName, Collection aValues){
		super(aName, aValues);
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean doesMatch(Object o) {
		if(o== null)
			return getOriginalValue() == null;
		List properties = ((List)o);
		Set toCompare = new HashSet();
		for(Property p: properties)
			toCompare.add(p.getValue());
		
		return toCompare.containsAll(getListValue());
	}

	@Override
	public String getComparator() {
		return " @> ";
	}

	@Override
	public Object getValue() {
		Collection values = getListValue(); 
		return StringUtils.surroundWith(StringUtils.concatenateTokens(values, ',', '\'', '\''), '(', ')');
	}
	
	@SuppressWarnings("unchecked")
	private Collection getListValue(){
		return (Collection) getOriginalValue();
	}

	@Override
	public boolean unprepaireable() {
		return true;
	}
}