
net.projectmonkey.object.mapper.ObjectMappingService Maven / Gradle / Ivy
package net.projectmonkey.object.mapper;
import net.projectmonkey.object.mapper.analysis.PropertyMappingProvider;
import net.projectmonkey.object.mapper.analysis.result.PropertyMapping;
import net.projectmonkey.object.mapper.construction.PopulationContext;
import net.projectmonkey.object.mapper.construction.PopulationService;
import net.projectmonkey.object.mapper.util.Types;
import java.util.List;
/*
*
* * 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.
*
*/
/**
* @author Andy Moody
*/
public class ObjectMappingService
{
private final PropertyMappingProvider propertyMappingProvider;
private final PopulationService populationService;
public ObjectMappingService()
{
this(new PropertyMappingProvider(), new PopulationService());
}
ObjectMappingService(final PropertyMappingProvider propertyMappingProvider, final PopulationService populationService)
{
this.propertyMappingProvider = propertyMappingProvider;
this.populationService = populationService;
}
/**
* Resolves mappings for the source and destination types and maps the source to the destinationType.
* @param source
* @param destinationType
* @return
*/
public T map(final K source, final Class destinationType)
{
T destination = null;
if (source != null)
{
Class sourceType = Types.deProxy(source.getClass());
Class destType = Types.deProxy(destinationType);
List mappings = propertyMappingProvider.getOrCreate(sourceType, destType);
destination = populationService.constructAndPopulate(destType, source, mappings);
}
return destination;
}
/**
* Resolves mappings for the source and destination types and maps the source to the destinationType
* using information from the provided context.
* @param source
* @param destinationType
* @param context
* @return
*/
public T map(final K source, final Class destinationType, final PopulationContext context)
{
T destination = null;
if (source != null)
{
Class sourceType = Types.deProxy(source.getClass());
Class destType = Types.deProxy(destinationType);
List mappings = propertyMappingProvider.getOrCreate(sourceType, destType);
destination = populationService.constructAndPopulate(source, mappings, context);
}
return destination;
}
/**
* Resolves mappings and merges the source into the destination.
* @param source
* @param destination
* @return
*/
public T merge(final K source, final T destination)
{
if (source != null)
{
if (destination == null)
{
throw new IllegalArgumentException("unable to merge source " + source + " with null destination");
}
Class sourceType = Types.deProxy(source.getClass());
Class destType = Types.deProxy(destination.getClass());
List mappings = propertyMappingProvider.getOrCreate(sourceType, destType);
populationService.populate(destination, source, mappings);
}
return destination;
}
/**
* Resolves mappings for the source and destinations and merges the source into the destination
* using information from the current context.
* @param destination
* @param context
* @return
*/
public T merge(final T destination, PopulationContext context)
{
K source = context.getSource();
if (source != null)
{
if (destination == null)
{
throw new IllegalArgumentException("unable to merge source " + source + " with null destination");
}
Class sourceType = Types.deProxy(source.getClass());
Class destType = Types.deProxy(destination.getClass());
List mappings = propertyMappingProvider.getOrCreate(sourceType, destType);
populationService.populate(destination, mappings, context);
}
return destination;
}
/**
* Instantiates the specified destinationType.
* @param destinationType
* @return
*/
public D instantiate(final Class destinationType)
{
return populationService.instantiate(destinationType);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy