package org.apache.uima.graph.impl;

import org.apache.uima.cas.Feature;
import org.apache.uima.cas.FeatureStructure;
import org.apache.uima.graph.IMapper;
import org.apache.uima.graph.IMappingManager;

import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.Vertex;

public final class MappingUtils {
	public static final String	FEATURE_PREFIX				= "f_";
	public static final String	SIMPLE_FEATURE_NULL_VALUE	= "";

	private MappingUtils() {
	}

	public static void mapFeature(IMappingManager manager,
		FeatureStructure origObj, Feature feat, Graph graph, Vertex origVertex) {
		if (feat.getRange().isPrimitive()) {
			String value = origObj.getFeatureValueAsString(feat);
			if (value == null)
				value = SIMPLE_FEATURE_NULL_VALUE;
			origVertex.setProperty(FEATURE_PREFIX + feat.getShortName(), value);
		} else {
			addLink(
				manager,
				graph,
				origVertex,
				origObj.getFeatureValue(feat),
				FEATURE_PREFIX + feat.getShortName());
		}
	}

	public static Edge addLink(IMappingManager manager, Graph graph,
		Vertex from, Object toObj, String label) {
		Vertex toVertex = manager.enqueueObject(toObj);
		Edge edge = from.addEdge(label, toVertex);
		return edge;
	}

	public static <T> T checkTypeAndCast(Class<? extends IMapper> mapperCls,
		Class<? extends T> acceptableType, Object obj) {
		if (obj == null)
			throw new IllegalArgumentException(String.format(
				"%s was asked to map null (object of type %s expected)",
				mapperCls.getName(),
				acceptableType.getName()));
		if (!acceptableType.isInstance(obj))
			throw new IllegalArgumentException(
				String.format(
					"%s can only map objects of subtypes of '%s', but '%s' was passed",
					mapperCls.getName(),
					acceptableType.getName(),
					obj.getClass()));
		return acceptableType.cast(obj);
	}
}

@@ -115,46 +115,8 @@
 re;%0A
-import org.apache.uima.graph.IMapper;%0A
 impo
@@ -1239,24 +1239,8 @@
 ss%3C?
- extends IMapper
 %3E ma
@@ -1248,16 +1248,16 @@
 perCls,%0A
+
 %09%09Class%3C
@@ -1392,19 +1392,23 @@
 sked to 
-map
+process
  null (o
@@ -1570,21 +1570,16 @@
 ception(
-%0A%09%09%09%09
 String.f
@@ -1593,43 +1593,28 @@
 %09%09%09%09
-%09
 %22%25s 
-can only map objects of subtyp
+expects subclass
 es o
@@ -1625,16 +1625,26 @@
 s', but 
+object of 
 '%25s' was
@@ -1653,17 +1653,16 @@
 assed%22,%0A
-%09
 %09%09%09%09mapp
@@ -1682,17 +1682,16 @@
 (),%0A%09%09%09%09
-%09
 acceptab
@@ -1708,17 +1708,16 @@
 Name(),%0A
-%09
 %09%09%09%09obj.

allow to use MappingUtils not only from IMapper