]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/Union.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / Union.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils.datastructures;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 import java.util.Iterator;\r
17 import java.util.List;\r
18 import java.util.NoSuchElementException;\r
19 \r
20 public class Union<T> extends ImmutableCollection<T> {\r
21     \r
22     private List<Collection<T>> parts;\r
23     \r
24     public Union(int partCount) {\r
25         parts = new ArrayList<Collection<T>>(partCount);\r
26     }\r
27     \r
28     public void add(Collection<T> part) {\r
29         parts.add(part);\r
30     }\r
31 \r
32     @Override\r
33     public boolean contains(Object o) {\r
34         for(Collection<T> part : parts)\r
35             if(part.contains(o))\r
36                 return true;\r
37         return false;\r
38     }\r
39 \r
40     @Override\r
41     public boolean containsAll(Collection<?> c) {\r
42         for(Object o : c)\r
43             if(!contains(o))\r
44                 return false;\r
45         return true;\r
46     }\r
47 \r
48     @Override\r
49     public boolean isEmpty() {\r
50         for(Collection<T> part : parts)\r
51             if(part.isEmpty())\r
52                 return true;\r
53         return false;\r
54     }\r
55 \r
56     @Override\r
57     public Iterator<T> iterator() {\r
58         \r
59         return new ImmutableIterator<T>() {\r
60 \r
61             Iterator<Collection<T>> partIterator = parts.iterator();\r
62             Iterator<T> iterator = null;\r
63             \r
64             @Override\r
65             public boolean hasNext() {\r
66                 while(iterator == null || !iterator.hasNext()) {\r
67                     if(!partIterator.hasNext())\r
68                         return false;\r
69                     iterator = partIterator.next().iterator();\r
70                 }\r
71                 return true;\r
72             }\r
73 \r
74             @Override\r
75             public T next() {\r
76                 if(hasNext()) \r
77                     return iterator.next();\r
78                 else\r
79                     throw new NoSuchElementException();\r
80             }\r
81             \r
82         };\r
83         \r
84     }\r
85 \r
86     @Override\r
87     public int size() {\r
88         int count = 0;\r
89         for(Collection<T> part : parts)\r
90             count += part.size();\r
91         return count;\r
92     }\r
93 \r
94 }\r