/* * Copyright (C) 2013 SeqWare * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package net.sourceforge.seqware.pipeline.tools; import java.util.Set; import java.util.TreeSet; /** * Taken from http://www.java2s.com/Code/Java/Collections-Data-Structure/ * Setoperationsunionintersectiondifferencesymmetricdifferenceissubsetissuperset.htm * * @author dyuen */ public class SetOperations { public static <T> Set<T> union(Set<T> setA, Set<T> setB) { Set<T> tmp = new TreeSet<>(setA); tmp.addAll(setB); return tmp; } public static <T> Set<T> intersection(Set<T> setA, Set<T> setB) { Set<T> tmp = new TreeSet<>(); for (T x : setA) if (setB.contains(x)) tmp.add(x); return tmp; } public static <T> Set<T> difference(Set<T> setA, Set<T> setB) { Set<T> tmp = new TreeSet<>(setA); tmp.removeAll(setB); return tmp; } public static <T> Set<T> symDifference(Set<T> setA, Set<T> setB) { Set<T> tmpA; Set<T> tmpB; tmpA = union(setA, setB); tmpB = intersection(setA, setB); return difference(tmpA, tmpB); } public static <T> boolean isSubset(Set<T> setA, Set<T> setB) { return setB.containsAll(setA); } public static <T> boolean isSuperset(Set<T> setA, Set<T> setB) { return setA.containsAll(setB); } }