Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.cucumber.cucumberexpressions.GeneratedExpression Maven / Gradle / Ivy
package io.cucumber.cucumberexpressions;
import java.text.Collator;
import java.util.*;
import java.util.stream.Collectors;
public class GeneratedExpression {
private static final Collator ENGLISH_COLLATOR = Collator.getInstance(Locale.ENGLISH);
private static final String JAVA_KEYWORDS[] = {
"abstract", "assert", "boolean", "break", "byte", "case",
"catch", "char", "class", "const", "continue",
"default", "do", "double", "else", "extends",
"false", "final", "finally", "float", "for",
"goto", "if", "implements", "import", "instanceof",
"int", "interface", "long", "native", "new",
"null", "package", "private", "protected", "public",
"return", "short", "static", "strictfp", "super",
"switch", "synchronized", "this", "throw", "throws",
"transient", "true", "try", "void", "volatile",
"while"
};
private final String expressionTemplate;
private final List> parameterTypes;
public GeneratedExpression(String expressionTemplate, List> parameterTypes) {
this.expressionTemplate = expressionTemplate;
this.parameterTypes = parameterTypes;
}
private static boolean isJavaKeyword(String keyword) {
return (Arrays.binarySearch(JAVA_KEYWORDS, keyword, ENGLISH_COLLATOR) >= 0);
}
public String getSource() {
List parameterTypeNames = parameterTypes.stream().map(ParameterType::getName).collect(Collectors.toList());
return String.format(expressionTemplate, parameterTypeNames.toArray());
}
private String getParameterName(String typeName, Map usageByTypeName) {
Integer count = usageByTypeName.get(typeName);
count = count != null ? count + 1 : 1;
usageByTypeName.put(typeName, count);
return count == 1 && !isJavaKeyword(typeName) ? typeName : typeName + count;
}
public List getParameterNames() {
HashMap usageByTypeName = new HashMap<>();
return parameterTypes.stream().map(parameterType -> getParameterName(parameterType.getName(), usageByTypeName)).collect(Collectors.toList());
}
public List> getParameterTypes() {
return parameterTypes;
}
}