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

org.aksw.jena_sparql_api.views.ValueSet Maven / Gradle / Ivy

The newest version!
package org.aksw.jena_sparql_api.views;

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

/**
 * 
 * 
 * @author Claus Stadler, Aug 1, 2018
 *
 * @param 
 */
@Deprecated //Use from a better location (aksw-commons?)
public class ValueSet
{
	private Set values;
	private boolean isPositive;

	public ValueSet(Set values) {
		this.values = values;
		this.isPositive = true;
	}

	public ValueSet(boolean isPositive, Set values) {
		this.values = values;
		this.isPositive = isPositive;
	}

	/*
	public ValueSet(boolean isPositive, T ...values) {
		this.isPositive = isPositive;
		this.values = new HashSet(Arrays.asList(values));
	}*/
	
	
	public static  ValueSet create(boolean isPositive, T ... values) {
		//this.isPositive = isPositive;
		Set v = new HashSet(Arrays.asList(values));
		ValueSet result = new ValueSet(isPositive, v);
		
		return result;
	}
	
	/**
	 * case: positive - positive
	 *     Simply take the intersection
	 *     
	 * case: positive - negative
	 *     {1, 2, 3} intersect {not {2}}: -> {1, 3} (positive.removeAll(negative)) 
	 *     
	 * case: negative - positive
	 *     Same as above     
	 *     
	 * case: negative - negative
	 *     Simply take the union
	 * 
	 * 
	 * @param other
	 * @return
	 */
	public ValueSet intersect(ValueSet that) {
		Set set = new HashSet();
		boolean isPos = true;

		if(isPositive) {
			if(that.isPositive) {
				set.addAll(this.values);
				set.retainAll(that.values);

			} else {

				set.addAll(this.values);
				set.removeAll(that.values);
			}
		} else {
			if(that.isPositive) {

				set.addAll(that.values);
				set.removeAll(this.values);
			} else {
				
				set.addAll(this.values);
				set.addAll(that.values);
				isPos = false;				
			}
		}
		
		ValueSet result = new ValueSet(isPos, set);
		return result;
	}
	
	
	/**
	 * case: positive - positive
	 *     Simply take the union
	 *     
	 * case: positive - negative
	 *     {1, 2, 3} intersect {not {1, 4}}: -> {4} (negative.removeAll(positive)) 
	 *     
	 * case: negative - positive
	 *     Same as above     
	 *     
	 * case: negative - negative
	 *     Simply take the intersection
	 * 
	 * @param that
	 * @return
	 */
	public ValueSet union(ValueSet that) {
		Set set = new HashSet();
		boolean isPos = true;

		if(isPositive) {
			if(that.isPositive) {
				set.addAll(this.values);
				set.addAll(that.values);

			} else {

				set.addAll(that.values);
				set.removeAll(this.values);
				isPos = false;
			}
		} else {
			if(that.isPositive) {

				set.addAll(this.values);
				set.removeAll(that.values);
				isPos = false;
			} else {
				
				set.addAll(this.values);
				set.retainAll(that.values);				
			}
		}
		
		ValueSet result = new ValueSet(isPos, set);
		return result;
	}
	
	public ValueSet negate() {
		ValueSet result = new ValueSet(!isPositive, values);
		return result;
	}
	
	
	public boolean isEmpty() {
		return isPositive && values.isEmpty();
	}

	
	public boolean contains(Object item) {
		boolean isContained = values.contains(item); 
		
		boolean result = isPositive
				? isContained
				: !isContained;

		return result;
	}
	
	public boolean isPositive() {
		return isPositive;
	}
	
	public Set getValue() {
		return values;
	}
	
	
	@Override
	public String toString() {
		String polarity = (isPositive) ? "+" : "-";
		
		return polarity + values;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (isPositive ? 1231 : 1237);
		result = prime * result + ((values == null) ? 0 : values.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ValueSet other = (ValueSet) obj;
		if (isPositive != other.isPositive)
			return false;
		if (values == null) {
			if (other.values != null)
				return false;
		} else if (!values.equals(other.values))
			return false;
		return true;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy