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

org.eclipse.jnosql.mapping.reflection.AbstractFieldMetadata 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.NoSQLException;
import org.eclipse.jnosql.communication.Value;
import jakarta.nosql.AttributeConverter;
import org.eclipse.jnosql.mapping.metadata.FieldMetadata;
import org.eclipse.jnosql.mapping.metadata.MappingType;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

/**
 * Base class to all {@link FieldMetadata}
 *
 * @see FieldMetadata
 */
abstract class AbstractFieldMetadata implements FieldMetadata {

    protected final MappingType mappingType;

    protected final Field field;

    protected final String name;

    protected final String fieldName;

    protected final Class> converter;

    protected final FieldReader reader;

    protected final FieldWriter writer;

    protected final Class type;

    protected final String udt;

    AbstractFieldMetadata(MappingType mappingType, Field field, String name,
                          Class> converter,
                          FieldReader reader, FieldWriter writer, String udt) {
        this.mappingType = mappingType;
        this.field = field;
        this.name = name;
        this.fieldName = field.getName();
        this.converter = converter;
        this.reader = reader;
        this.writer = writer;
        this.type = field.getType();
        this.udt = udt;
    }

    @Override
    public MappingType mappingType() {
        return mappingType;
    }

    @Override
    public String name() {
        return name;
    }

    @Override
    public String fieldName() {
        return fieldName;
    }

    @Override
    public Optional udt(){
        if(udt == null|| udt.isEmpty() || udt.isBlank()){
            return Optional.empty();
        }
        return Optional.of(udt);
    }

    @Override
    public Object read(Object bean) {
        Objects.requireNonNull(bean, "bean is required");
        return this.reader.read(bean);
    }

    @Override
    public void write(Object bean, Object value) {
        Objects.requireNonNull(bean, "bean is required");
        this.writer.write(bean, value);
    }

    @Override
    public Class type() {
        return this.type;
    }

    @SuppressWarnings("unchecked")
    @Override
    public > Optional> converter() {
        return Optional.ofNullable((Class) converter);
    }

    @SuppressWarnings("unchecked")
    @Override
    public > Optional newConverter() {
        return (Optional) Optional.ofNullable(converter).map(Reflections::newInstance);
    }

    @Override
    public Object value(Value value) {
        return value.get(field.getType());
    }


    @Override
    public  Optional value(Class type){
        Objects.requireNonNull(type, "type is required");
        Optional method = Arrays.stream(type.getDeclaredMethods()).filter(m -> "value".equals(m.getName()))
                .findFirst();
        T annotation = this.field.getAnnotation(type);
        if (method.isEmpty() || annotation == null) {
            return Optional.empty();
        }
        return method.map(m -> {
            try {
                Object invoke = m.invoke(annotation);
                return invoke.toString();
            } catch (IllegalAccessException | InvocationTargetException e) {
                throw new NoSQLException("There is an issue invoking the method: " + m + " using the annotation: "
                + type, e);
            }
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy