]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/SetUnion.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / SetUnion.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.utils.datastructures;
13
14 import java.util.Collection;
15 import java.util.Iterator;
16 import java.util.Set;
17
18 public class SetUnion<T> extends ImmutableCollection<T> {
19
20     private Set<T> set1;
21     private Set<T> set2;    
22     
23     public SetUnion(Set<T> set1, Set<T> set2) {
24         this.set1 = set1;
25         this.set2 = set2;
26     }
27
28     @Override
29     public boolean contains(Object o) {
30         return set1.contains(o) || set2.contains(o);
31     }
32
33     @Override
34     public boolean containsAll(Collection<?> c) {
35         for(Object o : c)
36             if(!contains(o))
37                 return false;
38         return true;
39     }
40
41     @Override
42     public boolean isEmpty() {
43         return set1.isEmpty() && set2.isEmpty();
44     }
45
46     @Override
47     public Iterator<T> iterator() {
48         return new ImmutableIterator<T>() {
49
50             int setId = 0;
51             Iterator<T> it1 = set1.iterator();
52             Iterator<T> it2 = set2.iterator();
53             T element = null;
54             
55             @Override
56             public boolean hasNext() {
57                 if(setId == 0) {
58                     if(it1.hasNext())
59                         return true;
60                     else {
61                         setId = 1;
62                         return hasNext();
63                     }
64                 }
65                 else {
66                     if(element != null)
67                         return true;
68                     while(it2.hasNext()) {                            
69                         element = it2.next();
70                         if(set1.contains(element)) 
71                             element = null;
72                         else
73                             return true;
74                     }
75                     return false;
76                 }
77             }
78
79             @Override
80             public T next() {
81                 if(setId == 0) {
82                     if(it1.hasNext())
83                         return it1.next();
84                     else {
85                         setId = 1;
86                         return next();
87                     }
88                 }
89                 else {
90                     if(element != null) {
91                         T ret = element;
92                         element = null;
93                         return ret;
94                     }
95                     while(it2.hasNext()) {                            
96                         T ret = it2.next();
97                         if(!set1.contains(ret)) 
98                             return ret;
99                     }
100                     return null;
101                 }
102             }
103             
104         };
105     }
106
107     @Override
108     public int size() {
109         int count = set1.size();       
110         for(T o : set2)
111             if(!set1.contains(o))
112                 ++count;
113         return count;
114     }       
115     
116 }