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

com.github.leeonky.map.MapConverter Maven / Gradle / Ivy

package com.github.leeonky.map;

import ma.glasnost.orika.MappingContext;
import ma.glasnost.orika.metadata.Type;

import java.util.Map;

class MapConverter extends BaseConverter {
    private final Mapper mapper;
    private final String keyPropertyName;
    private final String propertyName;
    private final String desName;

    MapConverter(Mapper mapper, String keyPropertyName, String propertyName, String desName) {
        this.mapper = mapper;
        this.keyPropertyName = keyPropertyName;
        this.propertyName = propertyName;
        this.desName = desName;
    }

    @Override
    public String buildConvertId() {
        return String.format("MapConverter:%s:%s-%s[%d]", keyPropertyName, propertyName, desName, mapper.hashCode());
    }

    @Override
    public Object convert(Object source, Type destinationType, MappingContext mappingContext) {
        Class rawType = destinationType.getRawType();
        Iterable collection = source instanceof Map ? wrapperEntry((Map) source) : (Iterable) source;
        return mapMap(collection, createMap(rawType), ((Type) destinationType.getActualTypeArguments()[1]).getRawType());
    }

    @SuppressWarnings("unchecked")
    private Map mapMap(Iterable source, Map result, Class elementType) {
        source.forEach(e -> result.put(getPropertyValue(e, keyPropertyName), mapper.mapTo(getPropertyValue(e, propertyName), elementType)));
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy