org.enodeframework.eventing.impl.DefaultEventSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of enode Show documentation
Show all versions of enode Show documentation
The enodeframework core implementation.
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;
}
}