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

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

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

import java.util.ArrayList;
import java.util.List;

public class Argument {
    private final ParameterType parameterType;
    private final Group group;

    static List> build(TreeRegexp treeRegexp, List> parameterTypes, String text) {
        Group group = treeRegexp.match(text);
        if (group == null) return null;

        List argGroups = group.getChildren();

        if (argGroups.size() != parameterTypes.size()) {
            throw new CucumberExpressionException(String.format("Expression /%s/ has %s capture groups (%s), but there were %s parameter types (%s)",
                    treeRegexp.pattern().pattern(),
                    argGroups.size(),
                    getGroupValues(argGroups),
                    parameterTypes.size(),
                    getParameterTypeNames(parameterTypes)
            ));
        }
        List> args = new ArrayList<>(argGroups.size());
        for (int i = 0; i < parameterTypes.size(); i++) {
            Group argGroup = argGroups.get(i);
            ParameterType parameterType = parameterTypes.get(i);
            args.add(new Argument<>(argGroup, parameterType));
        }

        return args;
    }

    private static List getParameterTypeNames(List> parameterTypes) {
        List list = new ArrayList<>();
        for (ParameterType type : parameterTypes) {
            String name = type.getName();
            list.add(name);
        }
        return list;
    }

    private static List getGroupValues(List argGroups) {
        List list = new ArrayList<>();
        for (Group argGroup : argGroups) {
            String value = argGroup.getValue();
            list.add(value);
        }
        return list;
    }

    public Argument(Group group, ParameterType parameterType) {
        this.group = group;
        this.parameterType = parameterType;
    }

    public Group getGroup() {
        return group;
    }

    public T getValue() {
        return parameterType.transform(group == null ? null : group.getValues());
    }
}