0

I have a problem making the initialization of a KCache cache generic. The code is as follows:

class MyCache(name: String, test: Boolean = true) {
    private val cache: SortedMap<String, MyAvroObject>
    init {
        if (test) {
            cache = sortedMapOf()
        }
        else {
            val cacheProps = Properties()
            cacheProps[KafkaCacheConfig.KAFKACACHE_BOOTSTRAP_SERVERS_CONFIG] =
                appConfig.propertyOrNull("kafka.bootstrapServers")?.getString()
            cacheProps[KafkaCacheConfig.KAFKACACHE_GROUP_ID_CONFIG] =
                appConfig.propertyOrNull("applicationId")?.getString()
            cacheProps[KafkaCacheConfig.KAFKACACHE_CLIENT_ID_CONFIG] = "GeneratingUnitsCache"
            cacheProps[KafkaCacheConfig.KAFKACACHE_TOPIC_CONFIG] = "app.$name.kcache"
            val serdeConfig = Collections.singletonMap(
                AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG,
                appConfig.property("kafka.schemaRegistryUrl").getString(),
            )
            //*** WISH TO MAKE THIS PART GENERIC
            val avroSerdes: Serde<MyAvroObject> = SpecificAvroSerde()
            avroSerdes.configure(serdeConfig, false)
            cache = KafkaCache(
                KafkaCacheConfig(cacheProps),
                Serdes.String(),
                avroSerdes
            )
            cache.init()
            //***
        }
    }
}

Depending on which topic you consume from, the cache will contain different objects. For example, the cache could contain MyAvroObject, String or MyAvroObject2. The problem then becomes to initialize the cache with the correct Serdes (serializer / deserializer) depending on what kind of objects the cache will contain.

I have tried to make the actual definition of the cache generic:

class MyCache<T>(name: String, test: Boolean = true){
    private val cache: SortedMap<String, T>
...

but then I struggle to get the type of T when I have to choose the right Serdes. Are there any Kotlin wizards out there who know if it is possible to make the initialization generic?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Liverbird97
  • 111
  • 2
  • 14
  • "get the type of T"... That defeats the point of generics. If you're using Avro, you should just use GenericRecord – OneCricketeer Sep 17 '21 at 13:48
  • Say that I also wanted to handle `String` objects? The whole class is made generic, except for the initialization of the `KafkaCache`. The choice of the value `Serdes` object is to my understanding dependent on the type of `T`. For instance, if `T` is a string then `Serdes.string()` should be used. Essentially my problem is to initialize the cache correctly. – Liverbird97 Sep 17 '21 at 14:00
  • Your class definition seems correct (although I don't know Kotlin), but sounds like you want [`is` or `when`](https://stackoverflow.com/a/48213377/2308683) to get a Serde object. Or perhaps [`Class.isAssignableFrom`](https://stackoverflow.com/a/4704933/2308683) – OneCricketeer Sep 17 '21 at 14:19

0 Answers0