com.sap.cds.reflect.impl.CdsElementBuilder Maven / Gradle / Ivy
/**************************************************************************
* (C) 2020-2024 SAP SE or an SAP affiliate company. All rights reserved. *
**************************************************************************/
package com.sap.cds.reflect.impl;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import com.sap.cds.ql.cqn.CqnValue;
import com.sap.cds.reflect.CdsAnnotatable;
import com.sap.cds.reflect.CdsAnnotation;
import com.sap.cds.reflect.CdsElement;
import com.sap.cds.reflect.CdsElementDefinition;
import com.sap.cds.reflect.CdsEnumType;
import com.sap.cds.reflect.CdsEnumType.Enumeral;
import com.sap.cds.reflect.CdsType;
public class CdsElementBuilder extends CdsAnnotableBuilder {
private static final byte IS_KEY = 1;
private static final byte IS_VIRTUAL = 2;
private static final byte IS_NOT_NULL = 4;
private static final byte IS_LOCALIZED = 8;
private String name;
private final CdsTypeBuilder typeBuilder;
public CdsTypeBuilder getTypeBuilder() {
return typeBuilder;
}
private T type;
private boolean isKey;
private boolean isNotNull;
private final boolean isVirtual;
private final boolean isLocalized;
private final Object defaultValue;
private final String doc;
private CqnValue calculatedBy;
public CdsElementBuilder(List> annotations, String name, CdsTypeBuilder typeBuilder,
boolean isKey, boolean isVirtual, boolean isNotNull, boolean isLocalized, Object defaultValue, String doc) {
super(annotations);
this.name = name;
this.typeBuilder = typeBuilder;
this.isKey = isKey;
this.isVirtual = isVirtual;
this.isNotNull = isNotNull;
this.isLocalized = isLocalized;
this.defaultValue = defaultValue;
this.doc = doc;
}
public static CdsElementBuilder element(String name) {
return new CdsElementBuilder<>(Collections.emptyList(), name, null, false, false, false, false, null, null);
}
public static CdsElementBuilder> copy(CdsElement element) {
List> annotations = element.annotations().collect(Collectors.toList());
String doc = null;
Optional elementDoc = ((CdsAnnotatable) element).getDoc();
if (elementDoc.isPresent()) {
doc = elementDoc.get();
}
return new CdsElementBuilder<>(annotations, element.getName(), null, element.isKey(), element.isVirtual(),
element.isNotNull(), element.isLocalized(), element.defaultValue().orElse(null), doc)
.type(element.getType());
}
public CdsElementBuilder name(String name) {
this.name = name;
return this;
}
public CdsElementBuilder isKey(boolean isKey) {
this.isKey = isKey;
return this;
}
public CdsElementBuilder isNotNull(boolean isNotNull) {
this.isNotNull = isNotNull;
return this;
}
public CdsElementBuilder type(T type) {
this.type = type;
return this;
}
public String getName() {
return name;
}
public CdsElementBuilder value(CqnValue value) {
this.calculatedBy = value;
return this;
}
public CdsElementDefinition build() {
if (type == null) {
type = typeBuilder.build();
}
return new CdsElementImpl<>(annotations, name, type, isKey, isVirtual, isNotNull, isLocalized, defaultValue,
doc, calculatedBy);
}
private static class CdsElementImpl extends CdsAnnotatableImpl implements CdsElementDefinition {
private final String name;
private final U type;
private final byte flags;
private final Object defaultValue;
private final CqnValue expression;
public CdsElementImpl(Collection> annotations, String name, U type, boolean isKey,
boolean isVirtual, boolean isNotNull, boolean isLocalized, Object defaultValue, String doc,
CqnValue calculatedBy) {
super(annotations, doc);
this.name = name.intern();
this.type = type;
this.defaultValue = resolveDefault(type, defaultValue);
this.expression = calculatedBy;
byte flags = 0;
if (isKey)
flags |= IS_KEY;
if (isVirtual)
flags |= IS_VIRTUAL;
if (isNotNull)
flags |= IS_NOT_NULL;
if (isLocalized)
flags |= IS_LOCALIZED;
this.flags = flags;
}
@Override
public String getName() {
return name;
}
@Override
@SuppressWarnings("unchecked")
public U getType() {
return type;
}
@Override
public boolean isKey() {
return (flags & IS_KEY) != 0;
}
@Override
public boolean isVirtual() {
return (flags & IS_VIRTUAL) != 0;
}
@Override
public boolean isNotNull() {
return (flags & IS_NOT_NULL) != 0;
}
@Override
public boolean isLocalized() {
return (flags & IS_LOCALIZED) != 0;
}
@Override
public boolean isUnique() {
throw new UnsupportedOperationException();
}
@Override
public Optional
© 2015 - 2025 Weber Informatics LLC | Privacy Policy