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

fun.langel.cql.rv.Row Maven / Gradle / Ivy

The newest version!
package fun.langel.cql.rv;

import fun.langel.cql.exception.MappingException;
import fun.langel.cql.node.Value;
import fun.langel.cql.reflect.Field;
import fun.langel.cql.reflect.Klass;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author [email protected](GuHan)
 * @since 2021/10/22 8:35 下午
 **/
public class Row implements ReturnValue>> {

    private final Map> columns = new HashMap<>();

    public Row() {
        this(null);
    }

    public Row(final Map> cols) {
        if (cols == null || cols.isEmpty()) {
            return;
        }
        this.columns.putAll(cols);
    }

    public void put(final java.lang.String k, ReturnValue v) {
        this.columns.put(k, v);
    }

    @Override
    public Map> getValue() {
        return this.columns;
    }

    @Override
    public Object mapTo(Class klass) throws MappingException {
        Type t=klass;

        Klass cKls = Klass.of(klass);
        Object o = null;
        try {
            o = cKls.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new MappingException(e.getMessage(), e);
        }
        for (Field f : cKls.fields()) {
            ReturnValue v = null;
            List alias = f.alias();
            if (alias != null) {
                for (java.lang.String an : alias) {
                    v = columns.get(an);
                    if (v != null) {
                        break;
                    }
                }
            }
            if (v == null) {
                v = columns.get(f.getName());
            }
            if (v == null) {
                continue;
            }
            try {
                f.setValue(o, v.mapTo(f.getKlass()));
            } catch (IllegalAccessException e) {
                throw new MappingException(e.getMessage(), e);
            }
        }
        return o;

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy