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

org.aksw.commons.collections.CollectionFromIterable Maven / Gradle / Ivy

There is a newer version: 0.9.9
Show newest version
package org.aksw.commons.collections;

import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Iterator;

import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;

/**
 * Collection view of an underlying iterable.
 * If the provided iterable is a collection, .size() will delegate to it
 * 
 * @author raven
 *
 * @param 
 */
public class CollectionFromIterable
	extends AbstractCollection
{
	protected Iterable iterable;
	
	public CollectionFromIterable(Iterable iterable) {
		super();
		this.iterable = iterable;
	}

	@Override
	public Iterator iterator() {
		Iterator result = iterable.iterator();
		return result;
	}

	@Override
	public int size() {
		int result = Iterators.size(iterator());
		return result;
	}
	
	@Override
	public String toString() {
		String result = Iterables.toString(iterable);
		return result;
	}
	
	public static  Collection wrap(Iterable iterable) {
		Collection result = iterable instanceof Collection
				? (Collection)iterable
				: new CollectionFromIterable<>(iterable); 

		return result;
	}

	public static  CollectionFromIterable create(Iterable iterable) {
		return new CollectionFromIterable<>(iterable);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy