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

com.clickhouse.data.mapper.CustomRecordMappers Maven / Gradle / Ivy

There is a newer version: 0.7.1-patch1
Show newest version
package com.clickhouse.data.mapper;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.clickhouse.data.ClickHouseRecord;
import com.clickhouse.data.ClickHouseUtils;

final class CustomRecordMappers {
    static final class RecordConstructor extends AbstractRecordMapper {
        private final Constructor constructor;

        RecordConstructor(Class objClass, Constructor constructor) {
            super(objClass);
            this.constructor = constructor;
        }

        @SuppressWarnings("unchecked")
        @Override
        public  T mapTo(ClickHouseRecord r, Class objClass, T obj) {
            return (T) newInstance(constructor, r);
        }
    }

    static final class RecordCreator extends AbstractRecordMapper {
        private final Method method;

        RecordCreator(Class objClass, Method createMethod) {
            super(objClass);
            this.method = createMethod;
        }

        @SuppressWarnings("unchecked")
        @Override
        public  T mapTo(ClickHouseRecord r, Class objClass, T obj) {
            if (method == null) {
                throw new IllegalArgumentException("No method to create instance");
            }

            try {
                return (T) method.invoke(null, r);
            } catch (IllegalAccessException | InvocationTargetException e) {
                throw new IllegalArgumentException(
                        ClickHouseUtils.format("Failed to create instance of [%s] using [%s]", clazz.getName(), method),
                        e);
            }
        }
    }

    private CustomRecordMappers() {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy