
net.projectmonkey.object.mapper.analysis.PropertyMappingProvider Maven / Gradle / Ivy
package net.projectmonkey.object.mapper.analysis;
import net.projectmonkey.object.mapper.analysis.matching.MatchingStrategy;
import net.projectmonkey.object.mapper.context.ExecutionContext;
import net.projectmonkey.object.mapper.util.Logger;
import net.projectmonkey.object.mapper.analysis.cache.MappingsCacheKey;
import net.projectmonkey.object.mapper.analysis.cache.PropertiesCacheKey;
import net.projectmonkey.object.mapper.analysis.cache.TypePair;
import net.projectmonkey.object.mapper.analysis.result.PropertyMapping;
import net.projectmonkey.object.mapper.analysis.result.PropertyPath;
import net.projectmonkey.object.mapper.analysis.result.PropertyPaths;
import net.projectmonkey.object.mapper.context.ConversionConfiguration;
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.
*
*/
/**
* The main coordinator for class analysis.
*
* @author Andy Moody
*/
public class PropertyMappingProvider
{
private static final Logger logger = Logger.getLogger(PropertyMappingProvider.class);
private MappingCache cache;
public PropertyMappingProvider()
{
this(new MappingCache());
}
PropertyMappingProvider(final MappingCache cache)
{
this.cache = cache;
}
/**
* Retrieves a {@link net.projectmonkey.object.mapper.analysis.result.PropertyMapping}
* PropertyMappings are cached using a key which is a combination of the current propertyResolver
* and matchingStrategy as well as he source and destination class types
*
* @param sourceType
* @param destinationType
* @return the list of {@link net.projectmonkey.object.mapper.analysis.result.PropertyMapping} representing the possible matches between
* the source and destination classes given the currently configured {@link net.projectmonkey.object.mapper.analysis.matching.MatchingStrategy}
* and {@link net.projectmonkey.object.mapper.analysis.resolver.PropertyResolver}
*/
public List getOrCreate(Class> sourceType, Class> destinationType)
{
ConversionConfiguration configuration = getConfiguration();
MappingsCacheKey cacheKey = createMappingsCacheKey(sourceType, destinationType);
if(!cache.containsMappings(cacheKey))
{
PropertiesCacheKey propertiesCacheKey = createPropertiesCacheKey(sourceType, destinationType);
if(!cache.containsProperties(propertiesCacheKey))
{
logger.debug("No cache key found for %s, analysing from %s to %s", propertiesCacheKey, sourceType, destinationType);
List sourcePaths = configuration.getSourcePropertyResolver().resolvePropertyPaths(sourceType);
List destinationPaths = configuration.getDestinationPropertyResolver().resolvePropertyPaths(destinationType);
PropertyPaths paths = new PropertyPaths(sourcePaths, destinationPaths);
cache.addProperties(propertiesCacheKey, paths);
}
logger.debug("No complete mappings cache key found for %s, matching from %s to %s", cacheKey, sourceType, destinationType);
PropertyPaths paths = cache.getProperties(propertiesCacheKey);
MatchingStrategy matchingStrategy = configuration.getMatchingStrategy();
List mappings = matchingStrategy.resolveMatches(TypePair.of(sourceType, destinationType), paths.getSourcePaths(), paths.getDestinationPaths());
cache.addMappings(cacheKey, mappings);
}
return cache.getMappings(cacheKey);
}
void addToCache(Class> sourceType, Class> destType, List mappings)
{
MappingsCacheKey cacheKey = createMappingsCacheKey(sourceType, destType);
cache.addMappings(cacheKey, mappings);
}
void addToCache(Class> sourceType, Class> destType, List sourcePaths,
List destPaths)
{
PropertiesCacheKey cacheKey = createPropertiesCacheKey(sourceType, destType);
cache.addProperties(cacheKey, new PropertyPaths(sourcePaths, destPaths));
}
private MappingsCacheKey createMappingsCacheKey(final Class> sourceType, final Class> destType)
{
TypePair, ?> pair = TypePair.of(sourceType, destType);
ConversionConfiguration configuration = getConfiguration();
return new MappingsCacheKey(configuration, pair);
}
private PropertiesCacheKey createPropertiesCacheKey(final Class> sourceType, final Class> destType)
{
TypePair, ?> pair = TypePair.of(sourceType, destType);
ConversionConfiguration configuration = getConfiguration();
return new PropertiesCacheKey(configuration.getSourcePropertyResolver(), configuration.getDestinationPropertyResolver(), pair);
}
protected ConversionConfiguration getConfiguration()
{
return ExecutionContext.getConfiguration();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy