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

io.cucumber.cucumberexpressions.CombinatorialGeneratedExpressionFactory Maven / Gradle / Ivy

Go to download

Cucumber Expressions are simple patterns for matching Step Definitions with Gherkin steps

There is a newer version: 18.0.1
Show newest version
package io.cucumber.cucumberexpressions;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

final class CombinatorialGeneratedExpressionFactory {
    // 256 generated expressions ought to be enough for anybody
    private static final int MAX_EXPRESSIONS = 256;
    private final String expressionTemplate;
    private final List>> parameterTypeCombinations;

    CombinatorialGeneratedExpressionFactory(
            String expressionTemplate,
            List>> parameterTypeCombinations) {

        this.expressionTemplate = expressionTemplate;
        this.parameterTypeCombinations = parameterTypeCombinations;
    }

    List generateExpressions() {
        List generatedExpressions = new ArrayList<>();
        ArrayDeque> permutation = new ArrayDeque<>(parameterTypeCombinations.size());
        generatePermutations(generatedExpressions, permutation);
        return generatedExpressions;
    }

    private void generatePermutations(
            List generatedExpressions,
            Deque> permutation
    ) {
        if (generatedExpressions.size() >= MAX_EXPRESSIONS) {
            return;
        }

        if (permutation.size() == parameterTypeCombinations.size()) {
            ArrayList> permutationCopy = new ArrayList<>(permutation);
            generatedExpressions.add(new GeneratedExpression(expressionTemplate, permutationCopy));
            return;
        }

        List> parameterTypes = parameterTypeCombinations.get(permutation.size());
        for (ParameterType parameterType : parameterTypes) {
            // Avoid recursion if no elements can be added.
            if (generatedExpressions.size() >= MAX_EXPRESSIONS) {
                return;
            }
            permutation.addLast(parameterType);
            generatePermutations(generatedExpressions, permutation);
            permutation.removeLast();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy