001/*
002 * Copyright 2018 Leibniz-Institut für Analytische Wissenschaften – ISAS – e.V..
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package de.isas.mztab2.cvmapping;
017
018import java.util.Collection;
019import java.util.LinkedHashSet;
020import java.util.Set;
021
022/**
023 *
024 * Utility methods for sets with common names.
025 *
026 * @author nilshoffmann
027 */
028public class SetOperations {
029
030    /**
031     * Creates a new typed {@link LinkedHashSet} (preserving order of insertion)
032     * from the given collection.
033     *
034     * @param <T> the value type
035     * @param c the collection
036     * @return a new typed hash set
037     */
038    public static <T> Set<T> newSet(Collection<? extends T> c) {
039        return new LinkedHashSet<>(c);
040    }
041
042    /**
043     * Returns the union (a+b) of <code>a</code> and <code>b</code>.
044     *
045     * @param <T> the value type
046     * @param a set a
047     * @param b set b
048     * @return the union of a and b
049     */
050    public static <T> Set<T> union(Set<T> a, Set<T> b) {
051        Set<T> union = new LinkedHashSet<>(a);
052        union.addAll(b);
053        return union;
054    }
055
056    /**
057     * Returns the intersection (a \cap b, all common elements) of
058     * <code>a</code> and <code>b</code>.
059     *
060     * @param <T> the value type
061     * @param a set a
062     * @param b set b
063     * @return the intersection of a and b
064     */
065    public static <T> Set<T> intersection(Set<T> a, Set<T> b) {
066        Set<T> inters = new LinkedHashSet<>(a);
067        inters.retainAll(b);
068        return inters;
069    }
070
071    /**
072     * Returns the complement (a without elements also in b) of <code>a</code>
073     * and <code>b</code>.
074     *
075     * @param <T> the value type
076     * @param a set a
077     * @param b set b
078     * @return the complement of a and b
079     */
080    public static <T> Set<T> complement(Set<T> a, Set<T> b) {
081        Set<T> a1 = new LinkedHashSet<>(a);
082        a1.removeAll(b);
083        return a1;
084    }
085
086    /**
087     * Returns the symmetric set difference
088     * (<code>union(complement(a,b),complement(b,a))</code>) on <code>a</code>
089     * and <code>b</code>.
090     *
091     * @param <T> the value type
092     * @param a set a
093     * @param b set b
094     * @return the symmetric set difference
095     */
096    public static <T> Set<T> symmetricDifference(Set<T> a, Set<T> b) {
097        return union(complement(a, b), complement(b, a));
098    }
099}