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

com.mageddo.tobby.factory.SerializerCreator Maven / Gradle / Ivy

There is a newer version: 2.1.6-alpha
Show newest version
package com.mageddo.tobby.factory;

import java.lang.reflect.InvocationTargetException;

import com.mageddo.tobby.internal.utils.StringUtils;

import org.apache.kafka.common.serialization.Serializer;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SerializerCreator {

  public static  Serializer create(
      Class> clazz, Class> defaultClass
  ) {
    return create(clazz.getName(), defaultClass == null ? null : defaultClass.getName());
  }

  public static Serializer create(String clazz, String defaultClass) {
    if (StringUtils.isBlank(clazz)) {
      return (Serializer) newInstance(defaultClass);
    }
    return (Serializer) newInstance(clazz);
  }

  private static Object newInstance(String keySerializer) {
    try {
      return Class.forName(keySerializer)
          .getDeclaredConstructor()
          .newInstance();
    } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | ClassNotFoundException e) {
      throw new RuntimeException(e);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy