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

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

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

import com.google.common.collect.Iterables;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: raven
 * Date: 4/24/11
 * Time: 12:01 AM
 * To change this template use File | Settings | File Templates.
 */
public class CartesianProduct
    extends AbstractCollection>
{
    protected List>	collections;
    protected boolean inPlace;

	public CartesianProduct(List> collections)
	{
		this(false, collections);
	}

	public CartesianProduct(boolean inPlace, List> collections)
	{
		this.inPlace = inPlace;
		this.collections = collections;
	}

    public static  CartesianProduct create(List> collections)
    {
        return new CartesianProduct(false, collections);
    }

    public static  CartesianProduct create(Iterable> iterables)
    {
        List> tmp = new ArrayList>();
        for(Iterable item : iterables) {
            tmp.add(item);
        }

        return new CartesianProduct(false, tmp);
    }


    public static  CartesianProduct create(T[]... collections)
    {
        List> tmp = new ArrayList>(collections.length);

        for (T[] item : collections)
            tmp.add(Arrays.asList(item));

        return  new CartesianProduct(false, tmp);
    }

    public static  CartesianProduct create(Iterable... collections)
    {
        return new CartesianProduct(false, Arrays.asList(collections));
    }


    //public static  CartesianProduct crea

    @Override
    public Iterator> iterator() {
        return new CartesianProductIterator(inPlace, collections);
    }

    @Override
    public int size() {
        int size = collections.isEmpty() ? 0 : 1;

        for(Iterable item : collections) {
            size *= Iterables.size(item);
        }

        return size;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy