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

io.atleon.avro.GenericDatas Maven / Gradle / Ivy

There is a newer version: 0.28.3
Show newest version
package io.atleon.avro;

import io.atleon.util.Instantiation;
import org.apache.avro.Conversion;
import org.apache.avro.Conversions;
import org.apache.avro.Schema;
import org.apache.avro.data.TimeConversions;
import org.apache.avro.generic.GenericData;
import org.apache.avro.specific.SpecificData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Type;
import java.util.function.Function;
import java.util.stream.Stream;

public final class GenericDatas {

    private static final Logger LOGGER = LoggerFactory.getLogger(GenericDatas.class);

    private GenericDatas() {

    }

    public static void addLogicalTypeConversion(GenericData genericData) {
        instantiateConversionsFrom(Conversions.class).forEach(genericData::addLogicalTypeConversion);
        instantiateConversionsFrom(TimeConversions.class).forEach(genericData::addLogicalTypeConversion);
    }

    static Function createTypeSchemaLoader(GenericData genericData) {
        if (genericData instanceof SpecificData) {
            return SpecificData.class.cast(genericData)::getSchema;
        } else {
            return type -> {
                throw new UnsupportedOperationException("Cannot load schema using GenericData for type=" + type);
            };
        }
    }

    private static Stream> instantiateConversionsFrom(Class outerClass) {
        return Stream.of(outerClass.getDeclaredClasses())
            .filter(Conversion.class::isAssignableFrom)
            .flatMap(GenericDatas::tryInstantiateConversion);
    }

    private static Stream> tryInstantiateConversion(Class conversionClass) {
        try {
            return Stream.of((Conversion) Instantiation.one(conversionClass));
        } catch (Throwable error) {
            LOGGER.debug("Failed to instantiate Conversion of type={}", conversionClass, error);
            return Stream.empty();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy