aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/itc/alg/util/CollectionHelper.java
blob: 6655be6da1d7df2216a6ad6318f7e53c705bc0a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*******************************************************************************
 * Copyright (c) 2010-2012, Tamas Szabo, Istvan Rath and Daniel Varro
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-v20.html.
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package tools.refinery.viatra.runtime.rete.itc.alg.util;

import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory;

import java.util.Set;

/**
 * @author Tamas Szabo
 *
 */
public class CollectionHelper {

    private CollectionHelper() {/*Utility class constructor*/}

    /**
     * Returns the intersection of two sets. It calls {@link Set#retainAll(java.util.Collection)} but returns a new set
     * containing the elements of the intersection.
     *
     * @param set1
     *            the first set (can be null, interpreted as empty)
     * @param set2
     *            the second set (can be null, interpreted as empty)
     * @return the intersection of the sets
     * @since 1.7
     */
    public static <V> Set<V> intersection(Set<V> set1, Set<V> set2) {
        if (set1 == null || set2 == null)
            return CollectionsFactory.createSet();

        Set<V> intersection = CollectionsFactory.createSet(set1);
        intersection.retainAll(set2);
        return intersection;
    }


    /**
     * Returns the difference of two sets (S1\S2). It calls {@link Set#removeAll(java.util.Collection)} but returns a
     * new set containing the elements of the difference.
     *
     * @param set1
     *            the first set (can be null, interpreted as empty)
     * @param set2
     *            the second set (can be null, interpreted as empty)
     * @return the difference of the sets
     * @since 1.7
     */
    public static <V> Set<V> difference(Set<V> set1, Set<V> set2) {
        if (set1 == null)
            return CollectionsFactory.createSet();

        Set<V> difference = CollectionsFactory.createSet(set1);
        if (set2 != null) difference.removeAll(set2);
        return difference;
    }

}