All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.molindo.utils.collections.IterableChain Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
/**
 * Copyright 2010 Molindo GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package at.molindo.utils.collections;

import static at.molindo.utils.collections.IteratorUtils.iterators;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class IterableChain implements Iterable {

	private final Iterable> _iterables;

	public static  Builder builder() {
		return new Builder();
	}

	public static  Builder builder(Class cls) {
		return new Builder();
	}

	public static  Builder builder(Iterable iter) {
		return new Builder().add(iter);
	}

	public static  Builder builder(T o) {
		return new Builder().add(o);
	}

	public static  Builder builder(T... o) {
		return new Builder().add(o);
	}

	public static  IterableChain chainIterables(Iterable> iterables) {
		return new IterableChain(iterables);
	}

	public IterableChain(Iterable> iterables) {
		// make a copy
		_iterables = IteratorUtils.list(iterables);
	}

	@Override
	public Iterator iterator() {
		return new IteratorChain(iterators(_iterables));
	}

	public static class Builder implements Iterable {

		private final List> _iterables = new ArrayList>();

		private Builder() {
		}

		public Builder add(Iterable iter) {
			_iterables.add(iter);
			return this;
		}

		public Builder add(T o) {
			return add(Collections.singleton(o));
		}

		public Builder add(T... o) {
			return add(Arrays.asList(o));
		}

		public IterableChain build() {
			return new IterableChain(_iterables);
		}

		@Override
		public Iterator iterator() {
			return build().iterator();
		}

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy