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

com.elepy.mongo.ElepyCodecRegistry Maven / Gradle / Ivy

There is a newer version: 3.0.0-alpha-29
Show newest version
package com.elepy.mongo;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.DBObject;
import org.bson.Document;
import org.bson.codecs.Codec;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.conversions.Bson;
import org.mongojack.JacksonCodecRegistry;
import org.mongojack.internal.stream.JacksonCodec;
import org.mongojack.internal.stream.JacksonDecoder;
import org.mongojack.internal.stream.JacksonEncoder;
import org.mongojack.internal.util.DocumentSerializationUtils;

import java.util.concurrent.ConcurrentHashMap;

public class ElepyCodecRegistry extends JacksonCodecRegistry {

    private final ObjectMapper objectMapper;
    private final Class view;
    private ConcurrentHashMap, Codec> codecCache = new ConcurrentHashMap<>();

    public ElepyCodecRegistry(ObjectMapper objectMapper, Class view) {
        super(objectMapper, view);
        this.objectMapper = objectMapper;
        this.view = view;
    }

    public  Codec get(Class clazz, CodecRegistry registry) {
        return this.addCodecForClass(clazz);
//        return !DocumentSerializationUtils.isKnownClass(clazz) && !DBObject.class.isAssignableFrom(clazz) && !Document.class.isAssignableFrom(clazz) && !Bson.class.isAssignableFrom(clazz) ? this.addCodecForClass(clazz) : null;
    }
    @SuppressWarnings("unchecked")
    @Override
    public  Codec addCodecForClass(Class clazz) {
        return (Codec) this.codecCache.computeIfAbsent(clazz, (k) -> {
            JacksonEncoder encoder = new JacksonEncoder(clazz, this.view, this.objectMapper);
            JacksonDecoder decoder = new JacksonDecoder(clazz, this.view, this.objectMapper);
            return new ElepyCodec<>(encoder, decoder);
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy