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

net.projectmonkey.object.mapper.construction.converter.MapConverter Maven / Gradle / Ivy

Go to download

Object mapping implementation written as an alternative to modelmapper which is able to support inheritance, handles flattening / expanding in a precise way, and is extensible / configurable

The newest version!
package net.projectmonkey.object.mapper.construction.converter;

import net.projectmonkey.object.mapper.ObjectMappingService;
import net.projectmonkey.object.mapper.construction.ElementPopulationContext;
import net.projectmonkey.object.mapper.construction.PopulationContext;
import net.projectmonkey.object.mapper.construction.type.GenericTypeUtils;
import net.projectmonkey.object.mapper.context.ExecutionContext;
import net.projectmonkey.object.mapper.util.TypeResolver;

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

import static net.projectmonkey.object.mapper.construction.type.GenericTypeUtils.TypeAndClass;

/*
 *
 *  * Copyright 2012 the original author or authors.
 *  *
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 *
 */

/**
 * Converts {@link java.util.Map} instances to each other.
 * 

* Adapted from ModelMapper (modelmapper.org) * * N.B. This implementation ALWAYS replaces the destination map * with the converted contents of the source map. * * An alternative strategy would be to attempt to merge the destination * objects based on their keys in the map and remove any superfluous * destination key / value pairs but this seemed likely to be error prone. * * @author Jonathan Halterman * @author Andy Moody */ public class MapConverter implements Converter, Map> { public static final MapConverter INSTANCE = new MapConverter(); private MapConverter(){} @Override public Map convert(final PopulationContext, Map> context) { Map source = context.getSource(); Map destination = createDestination(context); TypeAndClass[] elementTypes = GenericTypeUtils.INSTANCE.getArgumentTypes(context); TypeAndClass keyElementType = new TypeAndClass(Object.class); TypeAndClass valueElementType = new TypeAndClass(Object.class); if(elementTypes != null) { TypeAndClass firstElementType = elementTypes[0]; TypeAndClass secondElementType = elementTypes[1]; Class keyClass = firstElementType.getClazz(); keyElementType = keyClass == TypeResolver.Unknown.class ? keyElementType : firstElementType; Class valueClass = elementTypes[1].getClazz(); valueElementType = valueClass == TypeResolver.Unknown.class ? valueElementType : secondElementType; } ObjectMappingService mappingService = getMappingService(); for(Entry entry : source.entrySet()) { Object key = map(context, keyElementType, mappingService, entry.getKey()); Object value = map(context, valueElementType, mappingService, entry.getValue()); destination.put(key, value); } return destination; } private Object map(final PopulationContext context, final TypeAndClass elementType, final ObjectMappingService mappingService, final Object source) { ElementPopulationContext keyContext = new ElementPopulationContext(source, elementType, context); return mappingService.map(source, elementType.getClazz(), keyContext); } @Override public boolean canConvert(final PopulationContext context) { return context.getSource() instanceof Map && Map.class.isAssignableFrom(context.getDestinationType()); } protected Map createDestination(PopulationContext, Map> context) { Class> destinationType = context.getDestinationType(); if(destinationType.isInterface()) { return new HashMap(); } return getMappingService().instantiate(destinationType); } private ObjectMappingService getMappingService() { return ExecutionContext.getMappingService(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy