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

io.micronaut.configuration.kafka.serde.SerdeRegistry Maven / Gradle / Ivy

There is a newer version: 5.7.0
Show newest version
/*
 * Copyright 2017-2020 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micronaut.configuration.kafka.serde;

import io.micronaut.core.async.publisher.Publishers;
import io.micronaut.core.order.Ordered;
import io.micronaut.core.type.Argument;
import org.apache.kafka.common.serialization.ByteArrayDeserializer;
import org.apache.kafka.common.serialization.Deserializer;
import org.apache.kafka.common.serialization.Serde;
import org.apache.kafka.common.serialization.Serializer;

import java.util.Optional;
import java.util.concurrent.Future;


/**
 * A registry of Kafka {@link org.apache.kafka.common.serialization.Serde} instances.
 *
 * @author Graeme Rocher
 * @since 1.0
 */
public interface SerdeRegistry extends Ordered {

    /**
     * Obtain a {@link Serde} for the given type.
     *
     * @param   The generic type
     * @param type The type
     * @return The {@link Serde}
     */
     Serde getSerde(Class type);

    /**
     * Obtain a {@link Serializer} for the given type.
     *
     * @param   The generic type
     * @param type The type
     * @return The {@link Serde}
     */
    @SuppressWarnings("unchecked")
    default  Serializer getSerializer(Class type) {
        return getSerde(type).serializer();
    }

    /**
     * Obtain a {@link Deserializer} for the given type.
     *
     * @param   The generic type
     * @param type The type
     * @return The {@link Serde}
     */
    @SuppressWarnings("unchecked")
    default  Deserializer getDeserializer(Class type) {
        return getSerde(type).deserializer();
    }

    /**
     * Picks the most appropriate {@link Deserializer} for the given argument.
     *
     * @param argument The argument
     * @param  The generic type
     * @return The {@link Deserializer}
     */
    @SuppressWarnings("unchecked")
    default  Deserializer pickDeserializer(Argument argument) {
        Class type = argument.getType();

        if (Publishers.isConvertibleToPublisher(type) || Future.class.isAssignableFrom(type)) {
            Optional> typeArg = argument.getFirstTypeVariable();

            if (typeArg.isPresent()) {
                type = (Class) typeArg.get().getType();
            } else {
                return (Deserializer) new ByteArrayDeserializer();
            }
        }

        return getDeserializer(type);
    }

    /**
     * Picks the most appropriate {@link Deserializer} for the given argument.
     *
     * @param argument The argument
     * @param  The generic type
     * @return The {@link Deserializer}
     */
    @SuppressWarnings("unchecked")
    default  Serializer pickSerializer(Argument argument) {
        Class type = argument.getType();

        if (Publishers.isConvertibleToPublisher(type) || Future.class.isAssignableFrom(type)) {
            Optional> typeArg = argument.getFirstTypeVariable();

            if (typeArg.isPresent()) {
                type = (Class) typeArg.get().getType();
            } else {
                return (Serializer) new ByteArrayDeserializer();
            }
        }

        return getSerializer(type);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy