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

org.enodeframework.eventing.impl.DefaultEventSerializer Maven / Gradle / Ivy

There is a newer version: 1.1.10
Show newest version
package org.enodeframework.eventing.impl;

import com.google.common.collect.Maps;
import org.enodeframework.common.serializing.ISerializeService;
import org.enodeframework.eventing.IDomainEvent;
import org.enodeframework.eventing.IEventSerializer;
import org.enodeframework.infrastructure.ITypeNameProvider;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author [email protected]
 */
public class DefaultEventSerializer implements IEventSerializer {

    private final ITypeNameProvider typeNameProvider;

    private final ISerializeService serializeService;

    public DefaultEventSerializer(ITypeNameProvider typeNameProvider, ISerializeService serializeService) {
        this.typeNameProvider = typeNameProvider;
        this.serializeService = serializeService;
    }

    @Override
    public Map serialize(List> evnts) {
        LinkedHashMap dict = Maps.newLinkedHashMap();
        evnts.forEach(evnt -> {
            String typeName = typeNameProvider.getTypeName(evnt.getClass());
            String eventData = serializeService.serialize(evnt);
            dict.put(typeName, eventData);
        });
        return dict;
    }

    @Override
    public List> deserialize(Map data) {
        List> evnts = new ArrayList<>();
        data.forEach((key, value) -> {
            Class eventType = typeNameProvider.getType(key);
            IDomainEvent evnt = (IDomainEvent) serializeService.deserialize(value, eventType);
            evnts.add(evnt);
        });
        return evnts;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy