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

org.eclipse.jnosql.mapping.reflection.ParameterMetaDataBuilder Maven / Gradle / Ivy

There is a newer version: 1.1.4
Show newest version
/*
 *  Copyright (c) 2022 Contributors to the Eclipse Foundation
 *   All rights reserved. This program and the accompanying materials
 *   are made available under the terms of the Eclipse Public License v1.0
 *   and Apache License v2.0 which accompanies this distribution.
 *   The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 *   and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
 *
 *   You may elect to redistribute this code under either of these licenses.
 *
 *   Contributors:
 *
 *   Otavio Santana
 */
package org.eclipse.jnosql.mapping.reflection;

import jakarta.nosql.Column;
import jakarta.nosql.Id;
import jakarta.nosql.Convert;
import org.eclipse.jnosql.mapping.metadata.MappingType;
import org.eclipse.jnosql.mapping.metadata.ParameterMetaData;

import java.lang.reflect.Parameter;
import java.util.Objects;
import java.util.Optional;

class ParameterMetaDataBuilder {

    private final Parameter parameter;

    private ParameterMetaDataBuilder(Parameter parameter) {
        this.parameter = parameter;
    }

    ParameterMetaData build() {
        Id id = parameter.getAnnotation(Id.class);
        Column column = parameter.getAnnotation(Column.class);
        Convert convert = parameter.getAnnotation(Convert.class);
        Class type = parameter.getType();
        String name = Optional.ofNullable(id)
                .map(Id::value)
                .or(() -> Optional.ofNullable(column).map(Column::value))
                .orElse(null);
        if ((Objects.isNull(name) || name.isBlank())
                && parameter.getDeclaringExecutable().getDeclaringClass().isRecord()) {
            name = parameter.getName();
        }
        MappingType mappingType = MappingType.of(parameter.getType());
        return switch (mappingType) {
            case COLLECTION -> new DefaultCollectionParameterMetaData(name, type,
                    id != null,
                    Optional.ofNullable(convert).map(Convert::value).orElse(null),
                    mappingType, parameter::getParameterizedType);
            case ARRAY -> new DefaultArrayParameterMetaData(name, type,
                    id != null,
                    Optional.ofNullable(convert).map(Convert::value).orElse(null),
                    mappingType, parameter.getType().getComponentType());
            case MAP -> new DefaultMapParameterMetaData(name, type,
                    id != null,
                    Optional.ofNullable(convert).map(Convert::value).orElse(null),
                    mappingType, parameter::getParameterizedType);
            default -> new DefaultParameterMetaData(name, type,
                    id != null,
                    Optional.ofNullable(convert).map(Convert::value).orElse(null),
                    mappingType);
        };

    }

    public static ParameterMetaData of(Parameter parameter) {
        ParameterMetaDataBuilder builder = new ParameterMetaDataBuilder(parameter);
        return builder.build();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy