Example usage for java.lang FunctionalInterface FunctionalInterface

List of usage examples for java.lang FunctionalInterface FunctionalInterface

Introduction

In this page you can find the example usage for java.lang FunctionalInterface FunctionalInterface.

Prototype

FunctionalInterface

Source Link

Usage

From source file:org.jsweet.input.typescriptdef.ast.DeclarationHelper.java

public static TypeDeclaration createFunctionalType(String name, int parameterCount, boolean hasResult,
        boolean disambiguation) {
    TypeDeclaration functionalType = null;
    TypeParameterDeclaration[] typeParameters = new TypeParameterDeclaration[parameterCount];
    for (int i = 0; i < parameterCount; i++) {
        typeParameters[i] = new TypeParameterDeclaration(null, "T" + (i + 1));
    }//w  ww.ja  v a 2  s  . co  m
    TypeParameterDeclaration resultType = new TypeParameterDeclaration(null, "R");
    ParameterDeclaration[] parameters = new ParameterDeclaration[typeParameters.length];
    for (int i = 0; i < typeParameters.length; i++) {
        parameters[i] = new ParameterDeclaration(null, "p" + (i + 1),
                new TypeReference(null, typeParameters[i], null), false, false);
    }
    FunctionDeclaration newFunction = new FunctionDeclaration(null,
            JSweetDefTranslatorConfig.ANONYMOUS_FUNCTION_NAME,
            hasResult ? new TypeReference(null, resultType, null) : new TypeReference(null, "void", null),
            parameters, null);

    if (hasResult) {
        typeParameters = ArrayUtils.add(typeParameters, resultType);
    }
    functionalType = new TypeDeclaration(null, "interface", name, typeParameters, null,
            new FunctionDeclaration[] { newFunction });
    functionalType.addAnnotation(new FunctionalInterface() {
        @Override
        public Class<? extends Annotation> annotationType() {
            return FunctionalInterface.class;
        }
    });
    if (disambiguation) {
        functionalType.setDocumentation(
                "/** This functional interface should be used for disambiguating lambdas in function parameters (by casting to this interface)."
                        + "<p>It was automatically generated for functions (taking lambdas) that lead to the same erased signature. */");
    } else {
        functionalType.setDocumentation(
                "/** This functional interface was automatically generated for allowing lambdas taking "
                        + parameterCount + " parameters " + (hasResult ? " and returning a result." : ".")
                        + " */");
    }
    return functionalType;
}