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

net.amygdalum.util.builders.Sets Maven / Gradle / Ivy

The newest version!
package net.amygdalum.util.builders;

import static java.util.Arrays.asList;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

public final class Sets {

	private Set set;

	private Sets(boolean linked) {
		if (linked) {
			this.set = new LinkedHashSet();
		} else {
			this.set = new HashSet();
		}
	}

	private Sets(Collection set, boolean linked) {
		if (linked) {
			this.set = new LinkedHashSet(set);
		} else {
			this.set = new HashSet(set);
		}
	}

	@SafeVarargs
	public static  Sets linked(T... elements) {
		if (elements.length == 0) {
			return new Sets(true);
		}
		return new Sets(asList(elements), true);
	}

	public static  Sets linked(Collection set) {
		return new Sets(set, true);
	}

	@SafeVarargs
	public static  Sets hashed(T... elements) {
		if (elements.length == 0) {
			return new Sets(false);
		}
		return new Sets(asList(elements), false);
	}

	public static  Sets hashed(Collection set) {
		return new Sets(set, false);
	}

	@SafeVarargs
	public static  Set of(T... elements) {
		return new HashSet(asList(elements));
	}

	@SafeVarargs
	public static  Set of(Predicate cond, T... elements) {
		HashSet list = new HashSet();
		for (T element : elements) {
			if (cond.evaluate(element)) {
				list.add(element);
			}
		}
		return list;
	}
	
	@SafeVarargs
	public static  Set ofLinked(T... elements) {
		return new LinkedHashSet(asList(elements));
	}

	@SafeVarargs
	public static  Set ofLinked(Predicate cond, T... elements) {
		LinkedHashSet list = new LinkedHashSet();
		for (T element : elements) {
			if (cond.evaluate(element)) {
				list.add(element);
			}
		}
		return list;
	}
	
	public static Set ofPrimitives(int... array) {
		HashSet set = new HashSet(array.length);
		for (int i : array) {
			set.add(i);
		}
		return set;
	}

	public static Set ofLinkedPrimitives(int... array) {
		LinkedHashSet set = new LinkedHashSet(array.length);
		for (int i : array) {
			set.add(i);
		}
		return set;
	}

	public static  Set intersectionOf(Set set, Set other) {
		return new Sets(set, false).intersect(other).build();
	}

	public static  Set unionOf(Set set, Set other) {
		return new Sets(set, false).union(other).build();
	}

	public static  Set complementOf(Set set, Set minus) {
		return new Sets(set, false).minus(minus).build();
	}

	public Sets union(Set add) {
		return addAll(add);
	}

	public Sets add(T add) {
		set.add(add);
		return this;
	}

	public Sets addConditional(boolean b, T add) {
		if (b) {
			set.add(add);
		}
		return this;
	}

	public Sets addAll(Set add) {
		set.addAll(add);
		return this;
	}

	@SuppressWarnings("unchecked")
	public Sets addAll(T... add) {
		set.addAll(asList(add));
		return this;
	}

	public Sets minus(Set remove) {
		return removeAll(remove);
	}

	public Sets remove(T remove) {
		set.remove(remove);
		return this;
	}

	public Sets removeConditional(boolean b, T remove) {
		if (b) {
			set.remove(remove);
		}
		return this;
	}

	public Sets removeAll(Set remove) {
		set.removeAll(remove);
		return this;
	}

	@SuppressWarnings("unchecked")
	public Sets removeAll(T... remove) {
		set.removeAll(asList(remove));
		return this;
	}

	public Sets intersect(Set retain) {
		return retainAll(retain);
	}

	public Sets retain(T retain) {
		Set retainAll = new HashSet();
		retainAll.add(retain);
		set.retainAll(retainAll);
		return this;
	}

	public Sets retainConditional(boolean b, T retain) {
		if (b) {
			Set retainAll = new HashSet();
			retainAll.add(retain);
			set.retainAll(retainAll);
		}
		return this;
	}

	public Sets retainAll(Set retain) {
		set.retainAll(retain);
		return this;
	}

	@SuppressWarnings("unchecked")
	public Sets retainAll(T... retain) {
		set.retainAll(asList(retain));
		return this;
	}

	public Set build() {
		return set;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy