]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/Union.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / Union.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.ArrayList;
15 import java.util.Collection;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.NoSuchElementException;
19
20 public class Union<T> extends ImmutableCollection<T> {
21     
22     private List<Collection<T>> parts;
23     
24     public Union(int partCount) {
25         parts = new ArrayList<Collection<T>>(partCount);
26     }
27     
28     public void add(Collection<T> part) {
29         parts.add(part);
30     }
31
32     @Override
33     public boolean contains(Object o) {
34         for(Collection<T> part : parts)
35             if(part.contains(o))
36                 return true;
37         return false;
38     }
39
40     @Override
41     public boolean containsAll(Collection<?> c) {
42         for(Object o : c)
43             if(!contains(o))
44                 return false;
45         return true;
46     }
47
48     @Override
49     public boolean isEmpty() {
50         for(Collection<T> part : parts)
51             if(part.isEmpty())
52                 return true;
53         return false;
54     }
55
56     @Override
57     public Iterator<T> iterator() {
58         
59         return new ImmutableIterator<T>() {
60
61             Iterator<Collection<T>> partIterator = parts.iterator();
62             Iterator<T> iterator = null;
63             
64             @Override
65             public boolean hasNext() {
66                 while(iterator == null || !iterator.hasNext()) {
67                     if(!partIterator.hasNext())
68                         return false;
69                     iterator = partIterator.next().iterator();
70                 }
71                 return true;
72             }
73
74             @Override
75             public T next() {
76                 if(hasNext()) 
77                     return iterator.next();
78                 else
79                     throw new NoSuchElementException();
80             }
81             
82         };
83         
84     }
85
86     @Override
87     public int size() {
88         int count = 0;
89         for(Collection<T> part : parts)
90             count += part.size();
91         return count;
92     }
93
94 }