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