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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.utils.datastructures;
\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
20 public class Union<T> extends ImmutableCollection<T> {
\r
22 private List<Collection<T>> parts;
\r
24 public Union(int partCount) {
\r
25 parts = new ArrayList<Collection<T>>(partCount);
\r
28 public void add(Collection<T> part) {
\r
33 public boolean contains(Object o) {
\r
34 for(Collection<T> part : parts)
\r
35 if(part.contains(o))
\r
41 public boolean containsAll(Collection<?> c) {
\r
49 public boolean isEmpty() {
\r
50 for(Collection<T> part : parts)
\r
57 public Iterator<T> iterator() {
\r
59 return new ImmutableIterator<T>() {
\r
61 Iterator<Collection<T>> partIterator = parts.iterator();
\r
62 Iterator<T> iterator = null;
\r
65 public boolean hasNext() {
\r
66 while(iterator == null || !iterator.hasNext()) {
\r
67 if(!partIterator.hasNext())
\r
69 iterator = partIterator.next().iterator();
\r
77 return iterator.next();
\r
79 throw new NoSuchElementException();
\r
89 for(Collection<T> part : parts)
\r
90 count += part.size();
\r