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

org.kuali.core.db.torque.SetUtils Maven / Gradle / Ivy

package org.kuali.core.db.torque;

import java.util.Set;
import java.util.TreeSet;

/**
 * Utility methods for Set operations
 */
public class SetUtils {

	/**
	 * Returns the combined elements from both
	 */
	public static  Set union(Set a, Set b) {
		Set tmp = new TreeSet(a);
		tmp.addAll(b);
		return tmp;
	}

	/**
	 * Returns only those elements that are present in both A and B
	 */
	public static  Set intersection(Set a, Set b) {
		Set tmp = new TreeSet();
		for (T x : a) {
			if (b.contains(x)) {
				tmp.add(x);
			}
		}
		return tmp;
	}

	/**
	 * Returns elements from A that are not in B
	 */
	public static  Set difference(Set a, Set b) {
		Set tmp = new TreeSet(a);
		tmp.removeAll(b);
		return tmp;
	}

	/**
	 * 
	 */
	public static  Set symDifference(Set a, Set b) {
		Set tmpA = union(a, b);
		Set tmpB = intersection(a, b);
		return difference(tmpA, tmpB);
	}

	/**
	 * Return true if every element in A is also present in B
	 */
	public static  boolean isSubset(Set a, Set b) {
		return b.containsAll(a);
	}

	/**
	 * Return true if every element in B is also present in A
	 */
	public static  boolean isSuperset(Set a, Set b) {
		return a.containsAll(b);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy