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

com.envimate.mapmate.serialization.Serializer Maven / Gradle / Ivy

Go to download

MapMate is a modern mapping framework in the scope of mapping data in Json, XML, or YAML format into DTOs composed and vice versa.

There is a newer version: 1.6.8
Show newest version
/*
 * Copyright (C) 2017 [Richard Hauswald, Nune Isabekyan] (envimate GmbH - https://envimate.com/)
 */

package com.envimate.mapmate.serialization;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;

public class Serializer {
    private final Map, TypeSpecificSerializer> typeSpecificSerializers;
    private final Map, TypeSpecificGeneralizer> typeSpecificGeneralizers;
    private final GeneralizationSerializer generalizationSerializer;

    Serializer(final Map, TypeSpecificSerializer> typeSpecificSerializers,
               final Map, TypeSpecificGeneralizer> typeSpecificGeneralizers,
               final GeneralizationSerializer generalizationSerializer) {
        this.typeSpecificSerializers = typeSpecificSerializers;
        this.typeSpecificGeneralizers = typeSpecificGeneralizers;
        this.generalizationSerializer = generalizationSerializer;
    }

    public static SerializerBuilder aSerializer() {
        return new SerializerBuilder();
    }

    public String serialize(final Object object) {
        final Class type = object.getClass();
        if (object instanceof String) {
            return (String) object;
        } else if (canBeSerialized(type)) {
            final TypeSpecificSerializer typeSpecificSerializer = this.typeSpecificSerializers.get(type);
            return typeSpecificSerializer.serialize(object);
        } else if (canBeGeneralized(type)) {
            final TypeSpecificGeneralizer typeSpecificGeneralizer = this.typeSpecificGeneralizers.get(object.getClass());
            final Map generalization = typeSpecificGeneralizer.generalize(object);
            return this.generalizationSerializer.serialize(generalization);
        } else {
            throw new UnsupportedOperationException(String.format("Cannot serialize %s of type %s", object, type));
        }
    }

    Object generalize(final Object object) {
        final Class type = object.getClass();
        if (type.isArray()) {
            return generalizeArray((Object[]) object);
        } else if (Iterable.class.isAssignableFrom(type)) {
            final Iterable iterable = (Iterable) object;
            final LinkedList linkedList = new LinkedList();
            for (final Object o : iterable) {
                linkedList.add(o);
            }
            return generalizeArray(linkedList.toArray());
        } else if (Map.class.isAssignableFrom(type)) {
            final Map map = (Map) object;
            final Map generalization = new HashMap<>(map.size());
            for (final Entry entry : map.entrySet()) {
                final String key = serialize(entry.getKey());
                final String value = serialize(entry.getValue());
                generalization.put(key, value);
            }

            return generalization;
        }
        final TypeSpecificGeneralizer typeSpecificGeneralizer = this.typeSpecificGeneralizers.get(object.getClass());
        return typeSpecificGeneralizer.generalize(object);
    }

    private Object generalizeArray(final Object[] array) {
        final Object[] generalization = new Object[array.length];
        for (int i = 0; i < generalization.length; i++) {
            generalization[i] = serialize(array[i]);
        }
        return generalization;
    }

    public boolean canBeSerialized(final Class type) {
        return this.typeSpecificSerializers.containsKey(type);
    }

    public boolean canBeGeneralized(final Class type) {
        final boolean canBeGeneralized;
        if (type.isArray()) {
            canBeGeneralized = this.typeSpecificGeneralizers
                    .containsKey(type.getComponentType()) || this.typeSpecificSerializers
                    .containsKey(type.getComponentType());
        } else if (Iterable.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type)) {
            canBeGeneralized = true;
        } else {
            return this.typeSpecificGeneralizers.containsKey(type);
        }
        return canBeGeneralized;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy