Skip to main content
summaryrefslogtreecommitdiffstats
blob: baf9f8d7a1efc287de0d54ca9d7f7e4bd40aae69 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*******************************************************************************
 * Copyright (c) 2001, 2007 Oracle Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Oracle Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.common.sets;

import java.util.Iterator;
import java.util.Set;

/**
 * Generic (unoptimized) utilities for doing set operations.
 * 
 * <p><b>Provisional API - subject to change</b></p>
 * 
 * @author cbateman
 *
 */
public class AxiomaticSetUtil 
{
    /**
     * Creates the union of firstSet and secondSet in newSet.
     * @param newSet
     * @param firstSet
     * @param secondSet
     */
    public static void union(AxiomaticSet newSet, AxiomaticSet firstSet, AxiomaticSet secondSet)
    {
        newSet.addAll(firstSet);
        newSet.addAll(secondSet);
    }
    
    /**
     * Creates an intersection of firstSet and secondSet in newSet
     * @param newSet
     * @param firstSet
     * @param secondSet
     */
    public static void intersect(AxiomaticSet newSet, AxiomaticSet firstSet, AxiomaticSet secondSet)
    {
        // minor optimization: always iterate through the smaller of the
        // two sets.  This way we iterate through the smallest number
        // of elements
        Iterator it = null;
        Set  testSet = null;
        // if other set smaller, get its iterator
        if (secondSet.size() < firstSet.size())
        {
            it = secondSet.iterator();
            // test set is other set
            testSet = firstSet;
        }
        // first set is smaller or same
        else
        {
            it = firstSet.iterator();
            testSet = secondSet;
        }
        
        while (it.hasNext())
        {
            Object member = it.next();
            if (testSet.contains(member))
            {
                newSet.add(member);   
            }
        }
    }
    
    /**
     * @param firstSet
     * @param secondSet
     * @return true firstSet and secondSet have no common elements (their intersection is empty)
     */
    public static boolean isDisjoint(AxiomaticSet firstSet, AxiomaticSet secondSet)
    {
        return firstSet.intersect(secondSet).isEmpty();
    }

    /**
     * @param firstOperand
     * @param secondOperand
     * @return the set formed by removing the intersection of firstOperand and secondOperand
     * from firstOperand, leaving only those elements in firstOperand that are not in secondOperand
     */
    public static AxiomaticSet subtract(
            AxiomaticSet firstOperand, AxiomaticSet secondOperand) 
    {
        ConcreteAxiomaticSet  relativeComplement = new ConcreteAxiomaticSet();

        // iterate through firstOperand and add each element to the result
        // set that is not in secondOperand
        for (final Iterator it = firstOperand.iterator(); it.hasNext();)
        {
            Object member = it.next();
            if (!secondOperand.contains(member))
            {
                relativeComplement.add(member);   
            }
        }

        return relativeComplement;
    }
}

Back to the top