KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > util > InvertableSet


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.util;
26
27
28 import java.util.*;
29
30 public class InvertableSet {
31     private boolean inverse;
32     private Set set;
33
34     public InvertableSet(Set set, boolean inverse) {
35         this.set = set;
36         this.inverse = inverse;
37     }
38
39     public InvertableSet(Set set) {
40         this.set = set;
41         this.inverse = false;
42     }
43
44     public InvertableSet() {
45         this.set = new HashSet();
46         this.inverse = false;
47     }
48
49     public Set getSet() { return set; }
50     public boolean isInverse() { return inverse; }
51
52
53     public void addAll(InvertableSet other) {
54         InvertableSet s = union(other);
55         this.set = s.set;
56         this.inverse = s.inverse;
57     }
58
59     public boolean contains(Object JavaDoc obj) {
60         if (inverse) return !set.contains(obj);
61         else return set.contains(obj);
62     }
63
64     public boolean containsAny(Collection c) {
65         for (Iterator i = c.iterator(); i.hasNext(); ) {
66             if (contains(i.next())) return true;
67         }
68         return false;
69     }
70
71     public InvertableSet intersect(InvertableSet s2) {
72         InvertableSet s1 = this;
73
74         //System.err.println("intersect: "+!s1.inverse+", "+!s2.inverse);
75
//s1 & s2
76
if (!s1.inverse && !s2.inverse) {
77             s1.set.retainAll(s2.set);
78             return s1;
79         }
80         //!s1 & !s2 -> !(s1 | s2)
81
if (s1.inverse && s2.inverse) {
82             s1.set.addAll(s2.set);
83             return s1;
84         }
85         if (s1.inverse) {
86             s1 = s2;
87             s2 = this;
88         }
89         //s1 & !s2
90
s1.set.removeAll(s2.set);
91         return s1;
92     }
93
94     public InvertableSet invert() {
95         inverse = !inverse;
96         return this;
97     }
98
99     public InvertableSet union(InvertableSet s2) {
100         // s1 | s2 -> !(!s1 & !s2)
101
InvertableSet s0 = this.invert().intersect(s2.invert());
102         return s0.invert();
103     }
104 }
105
Popular Tags