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

spock.genesis.generators.composites.TupleGenerator.groovy Maven / Gradle / Ivy

package spock.genesis.generators.composites

import groovy.transform.CompileStatic
import spock.genesis.extension.ExtensionMethods
import spock.genesis.generators.Generator
import spock.genesis.generators.Permutable
import spock.genesis.generators.UnmodifiableIterator

@CompileStatic
class TupleGenerator extends Generator> implements Closeable, Permutable {

    protected final List> generators

    TupleGenerator(List> iterables) {
        List> collector = []
        iterables.collect(collector) { ExtensionMethods.toGenerator(it) }
        this.generators = collector.asImmutable()
    }

    TupleGenerator(Iterable... iterables) {
        this(iterables.toList())
    }

    UnmodifiableIterator> iterator() {
        new UnmodifiableIterator>() {
            List iterators = generators*.iterator()

            @Override
            boolean hasNext() {
                iterators.every { it.hasNext() }
            }

            @Override
            List next() {
                iterators*.next()
            }
        }
    }

    PermutationGenerator permute() {
        new PermutationGenerator(generators)
    }

    PermutationGenerator permute(int maxDepth) {
        new PermutationGenerator(generators, maxDepth)
    }

    @Override
    boolean isFinite() {
        generators.every { it.finite }
    }

    @Override
    void close() {
        generators.each { it.close() }
    }

    @Override
    TupleGenerator seed(Long seed) {
        generators.each { it.seed(seed) }
        super.seed(seed)
        this
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy