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

com.sap.cds.reflect.impl.CdsFunctionBuilder Maven / Gradle / Ivy

/*********************************************************************
 * (C) 2020 SAP SE or an SAP affiliate company. All rights reserved. *
 *********************************************************************/
package com.sap.cds.reflect.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import com.sap.cds.reflect.CdsAnnotation;
import com.sap.cds.reflect.CdsFunction;
import com.sap.cds.reflect.CdsParameter;
import com.sap.cds.reflect.CdsType;

public class CdsFunctionBuilder extends CdsDefinitionBuilder {

	private final List parameters = new ArrayList<>(); // ordered

	private CdsTypeBuilder returnType;

	private CdsFunctionImpl impl = null;

	public CdsFunctionBuilder(List> annotations, String qualifiedName) {
		super(annotations, qualifiedName);
	}

	@Override
	public CdsFunction build() {
		if (impl == null) {
			List immutableParameters = new ArrayList<>();
			parameters.forEach(p -> immutableParameters.add(p.build()));
			impl = new CdsFunctionImpl(annotations, qualifiedName, immutableParameters);
			if (returnType != null) {
				impl.returnType = this.returnType.build();
			}

		}
		return impl;
	}

	void addParameter(CdsParameterBuilder para) {
		this.parameters.add(para);
	}

	void addParameters(List params) {
		params.forEach(this::addParameter);
	}

	void setReturnType(CdsTypeBuilder returnType) {
		this.returnType = returnType;
	}

	private static class CdsFunctionImpl extends CdsDefinitionImpl implements CdsFunction {

		private final List parameters = new ArrayList<>(); // ordered

		private CdsType returnType;

		private CdsFunctionImpl(List> annotations, String qualifiedName,
				List parameters) {
			super(annotations, qualifiedName);
			this.parameters.addAll(parameters);
		}

		@Override
		public Stream parameters() {
			return parameters.stream();
		}

		@Override
		public CdsType getReturnType() {
			return returnType;
		}

	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy