]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/SetUnion2.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / SetUnion2.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.security.InvalidParameterException;
15 import java.util.Collection;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.Set;
19
20 public class SetUnion2<T> extends ImmutableCollection<T> {
21
22     List<Set<T>> sets;
23
24     @Override
25     public boolean contains(Object o) {
26         for(Set<T> set : sets)
27             if(set.contains(o))
28                 return true;
29         return false;
30     }
31
32     @Override
33     public boolean containsAll(Collection<?> c) {
34         for(Object o : c)
35             if(!contains(o))
36                 return false;
37         return true;
38     }
39
40     @Override
41     public boolean isEmpty() {
42         for(Set<T> set : sets)
43             if(!set.isEmpty())
44                 return false;
45         return true;
46     }
47
48     @Override
49     public Iterator<T> iterator() {
50         return new ImmutableIterator<T>() {
51
52             int setId = 0;
53             Iterator<T> it = sets.get(0).iterator();
54             T element = null;
55             
56             @Override
57             public boolean hasNext() {
58                 while(true) {
59                     if(element != null)
60                         return true;
61                     if(it.hasNext()) {
62                         element = it.next();
63                         for(int i=0;i<setId;++i)
64                             if(sets.get(i).contains(element)) {
65                                 element = null;
66                                 break;
67                             }
68                     }
69                     else {
70                         ++setId;
71                         if(setId < sets.size())
72                             it = sets.get(setId).iterator();
73                         else
74                             return false;
75                     }
76                 }
77             }
78
79             @Override
80             public T next() {
81                 while(true) {
82                     if(element != null)
83                         return element;
84                     if(it.hasNext()) {
85                         element = it.next();
86                         for(int i=0;i<setId;++i)
87                             if(sets.get(i).contains(element)) {
88                                 element = null;
89                                 break;
90                             }
91                     }
92                     else {
93                         ++setId;
94                         if(setId < sets.size())
95                             it = sets.get(setId).iterator();
96                         else
97                             throw new InvalidParameterException();
98                     }
99                 }
100             }
101             
102         };
103     }
104
105     @Override
106     public int size() {
107         int count = sets.get(0).size();
108         for(int i=1;i<sets.size();++i) {
109             floop:
110             for(T o : sets.get(i)) {
111                 for(int j=0;j<i;++j)
112                     if(sets.get(j).contains(o))
113                         break floop;
114                 ++count;
115             }
116         }
117         return count;
118     }       
119     
120 }