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

net.projectmonkey.object.mapper.context.ExecutionContext Maven / Gradle / Ivy

package net.projectmonkey.object.mapper.context;

import net.projectmonkey.object.mapper.ObjectMappingService;
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.construction.PopulationContext;
import net.projectmonkey.object.mapper.util.Iterables;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 *
 *  * 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 ExecutionContext
{

	private static ThreadLocal context = new ThreadLocal();

	private ConversionConfiguration configuration;
	private ObjectMappingService mappingService;
	private Map, List> pathsByClass;
	private Map, List> mappingsByTypes;
	private Map destinationsBySource;


	public ExecutionContext(final ConversionConfiguration conversionConfiguration, final ObjectMappingService mappingService)
	{
		this.configuration = conversionConfiguration;
		this.mappingService = mappingService;
		destinationsBySource = new HashMap();
		pathsByClass = new HashMap, List>();
		mappingsByTypes = new HashMap, List>();
	}

	public ConversionConfiguration getConversionConfiguration()
	{
		return configuration;
	}

	public ObjectMappingService getObjectMappingService()
	{
		return mappingService;
	}

	public Map, List> getPathsByClass()
	{
		return pathsByClass;
	}

	public Map, List> getMappingsByTypes()
	{
		return mappingsByTypes;
	}

	public Map getDestinationsBySource()
	{
		return destinationsBySource;
	}

	public static void set(ExecutionContext executionContext)
	{
		context.set(executionContext);
	}

	public static void clear()
	{
		context.remove();
	}

	public static ConversionConfiguration getConfiguration(){
		ExecutionContext executionContext = get();
		return executionContext == null ? null : executionContext.getConversionConfiguration();
	}

	public static ObjectMappingService getMappingService()
	{
		ExecutionContext executionContext = get();
		return executionContext == null ? null : executionContext.getObjectMappingService();
	}

	public static void registerDestination(PopulationContext context, Object object)
	{
		Object source = context.getSource();
		if(source !=null && !Iterables.isIterable(source.getClass()))
		{
			SourceDestinationTypeCacheKey cacheKey = createCacheKey(context);
			Map destinationsBySource = assertNoValueForKey(get().getDestinationsBySource(), cacheKey, "Object %s already exists for context %s");
			destinationsBySource.put(cacheKey, object);
		}
	}

	public static Object getDestination(PopulationContext context)
	{
		return get().getDestinationsBySource().get(createCacheKey(context));
	}

	public static void registerPaths(Class clazz, List paths)
	{
		Map, List> pathsByClass = assertNoValueForKey(get().getPathsByClass(), clazz, "Paths %s already exist for class %s");
		pathsByClass.put(clazz, paths);
	}

	public static List getPaths(Class clazz)
	{
		return get().getPathsByClass().get(clazz);
	}

	public static void registerMappings(TypePair types, List mappings)
	{
		Map, List> mappingsByTypes = assertNoValueForKey(get().getMappingsByTypes(), types, "Mappings %s already exist for types %s");
		mappingsByTypes.put(types, mappings);
	}

	public static List getMappings(TypePair types)
	{
		return get().getMappingsByTypes().get(types);
	}

	public static ExecutionContext get()
	{
		return context.get();
	}

	private static  Map assertNoValueForKey(Map map, K key, String errorMessage)
	{
		if(map.containsKey(key) && map.get(key) != null)
		{
			throw new IllegalStateException(String.format(errorMessage, map.get(key), key));
		}
		return map;
	}

	private static SourceDestinationTypeCacheKey createCacheKey(PopulationContext context)
	{
		return new SourceDestinationTypeCacheKey(context.getDestinationType(), context.getSource());
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy