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

it.auties.protobuf.serialization.model.object.ProtobufObjectElement Maven / Gradle / Ivy

package it.auties.protobuf.serialization.model.object;

import it.auties.protobuf.annotation.ProtobufProperty;
import it.auties.protobuf.serialization.model.property.ProtobufPropertyElement;
import it.auties.protobuf.serialization.model.property.ProtobufPropertyType;

import javax.lang.model.element.*;
import java.util.*;

public class ProtobufObjectElement {
    private final TypeElement typeElement;
    private final Map properties;
    private final List builders;
    private final Map constants;
    private final ProtobufEnumMetadata enumMetadata;
    private final ExecutableElement deserializer;
    private ProtobufUnknownFieldsElement unknownFieldsElement;

    public ProtobufObjectElement(TypeElement typeElement, ProtobufEnumMetadata enumMetadata, ExecutableElement deserializer) {
        this.typeElement = typeElement;
        this.enumMetadata = enumMetadata;
        this.deserializer = deserializer;
        this.builders = new ArrayList<>();
        this.properties = new LinkedHashMap<>();
        this.constants = new LinkedHashMap<>();
    }

    public TypeElement element() {
        return typeElement;
    }

    public String getGeneratedClassNameBySuffix(String suffix) {
       return getGeneratedClassNameByName(element().getSimpleName() + suffix);
    }

    public String getGeneratedClassNameByName(String className) {
        var name = new StringBuilder();
        var element = element();
        while (element.getEnclosingElement() instanceof TypeElement parent) {
            name.append(parent.getSimpleName());
            element = parent;
        }

        return name + className;
    }

    public Optional enumMetadata() {
        return Optional.of(enumMetadata);
    }

    public List properties() {
        return List.copyOf(properties.values());
    }

    public boolean isEnum() {
        return typeElement.getKind() == ElementKind.ENUM;
    }

    public Map constants() {
        return Collections.unmodifiableMap(constants);
    }

    public Optional addConstant(int fieldIndex, String fieldName) {
        return Optional.ofNullable(constants.put(fieldIndex, fieldName));
    }

    public Optional addProperty(Element element, Element accessor, ProtobufPropertyType type, ProtobufProperty property) {
        var fieldName = element.getSimpleName().toString();
        var result = new ProtobufPropertyElement(fieldName, accessor, type, property, element instanceof ExecutableElement);
        return Optional.ofNullable(properties.put(property.index(), result));
    }

    public void addBuilder(String className, List parameters, ExecutableElement executableElement) {
        var builderElement = new ProtobufBuilderElement(className, parameters, executableElement);
        builders.add(builderElement);
    }

    public List builders() {
        return Collections.unmodifiableList(builders);
    }

    public Optional deserializer() {
        return Optional.ofNullable(deserializer);
    }

    public Optional unknownFieldsElement() {
        return Optional.ofNullable(unknownFieldsElement);
    }

    public void setUnknownFieldsElement(ProtobufUnknownFieldsElement unknownFieldsElement) {
        this.unknownFieldsElement = unknownFieldsElement;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy