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

com.googlecode.objectify.impl.load.EmbeddedMapSetter Maven / Gradle / Ivy

Go to download

*** THIS VERSION UPLOADED FOR USE WITH CEDAR-COMMON, TO AVOID DEPENDENCIES ON GOOGLE CODE-BASED MAVEN REPOSITORIES. *** The simplest convenient interface to the Google App Engine datastore

The newest version!
package com.googlecode.objectify.impl.load;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Map;

import com.googlecode.objectify.impl.LoadContext;
import com.googlecode.objectify.impl.Transmog;
import com.googlecode.objectify.impl.TypeUtils;
import com.googlecode.objectify.impl.conv.Conversions;

/**
 * Creates objects or stores primitive values within a map embedded in a component.
 */
public class EmbeddedMapSetter extends CollisionDetectingSetter
{

	Field field;
	/** Constructor for the component type, or null if this is a primitive value map. */
	Constructor componentTypeCtor;
	/** Nested Transmog that handles object loading for the component type, or null for primitives. */
	Transmog nestedTransmog;

	@SuppressWarnings("unchecked")
	public EmbeddedMapSetter(Field field, Class componentType, Conversions conversions,
			Collection collisionPaths)
	{
		super(collisionPaths);
		this.field = field;
		if (Object.class.equals(componentType))
		{
			// If the Map value type is object, we assume a map of primitive datastore values
			this.componentTypeCtor = null;
			this.nestedTransmog = null;
		}
		else
		{
			this.componentTypeCtor = TypeUtils.getConstructor(componentType);
			this.nestedTransmog = new Transmog(conversions, (Class) componentType);
		}
	}

	/**
	 * Set the value within our map.
	 */
	@Override
	protected void safeSet(Object toPojo, Object value, LoadContext context)
	{
		@SuppressWarnings("unchecked")
		// we know this is a Map from construction
		Map map = (Map) TypeUtils.field_get(field, toPojo);
		String objectName = context.getMapEntryName();
		String suffix = context.getMapSuffix();
		if (componentTypeCtor != null)
		{
			Object nestedPojo = map.get(objectName);
			if (nestedPojo == null)
			{
				// Not present yet, create a new object
				nestedPojo = TypeUtils.newInstance(componentTypeCtor);
				map.put(objectName, nestedPojo);
			}
			// Delegate to the nested Transmog to set the individual value.
			nestedTransmog.loadSingleValue(suffix, value, nestedPojo, context);
		}
		else
		{
			map.put(objectName, value);
		}
	}
}