<commit_before>/*
 * Created on Jul 31, 2007
 */
package org.griphyn.vdl.karajan.lib;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.globus.cog.karajan.arguments.Arg;
import org.globus.cog.karajan.arguments.ArgUtil;
import org.globus.cog.karajan.arguments.VariableArguments;
import org.globus.cog.karajan.stack.VariableStack;
import org.globus.cog.karajan.workflow.ExecutionException;
import org.globus.cog.karajan.workflow.futures.FutureNotYetAvailable;
import org.griphyn.vdl.karajan.PairIterator;
import org.griphyn.vdl.mapping.DSHandle;
import org.griphyn.vdl.type.Type;
import org.griphyn.vdl.type.Types;

public abstract class SwiftArg extends Arg {
	public static final Logger logger = Logger.getLogger(SwiftArg.class);

	public SwiftArg(String name, int index) {
		super(name, index);
	}

	public SwiftArg(String name) {
		super(name, IMPLICIT);
	}

	protected Object unwrap(VariableStack stack, Object val) throws ExecutionException {
		if (val instanceof DSHandle) {
			DSHandle handle = (DSHandle) val;
			if (handle.getType().isArray()) {
				Map value = handle.getArrayValue();
				synchronized(handle.getRoot()) {
					if (handle.isClosed()) {
						return new PairIterator(value);
					}
					else {
						return VDLFunction.addFutureListListener(stack, handle, value);
					}
				}
			}
			if (logger.isDebugEnabled()) {
				logger.debug("SwiftArg.getValue(" + handle + ")");
			}
			synchronized (handle.getRoot()) {
				if (!handle.isClosed()) {
					if (logger.isDebugEnabled()) {
						logger.debug("Waiting for " + handle);
					}
					throw new FutureNotYetAvailable(VDLFunction.addFutureListener(stack, handle));
				}
				else {
					return handle.getValue();
				}
			}
		}
		else {
			throw new ExecutionException("Expected Swift data, but got some primitive type (" + val
					+ ")");
		}
	}

	public Object getValue(VariableStack stack) throws ExecutionException {
		Object v = super.getValue(stack);
		if(v == null) return v;
			else return unwrap(stack, super.getValue(stack));
	}

	public double getDoubleValue(VariableStack stack) throws ExecutionException {
		return checkDouble(getValue(stack));
	}
	
	public static double checkDouble(Object dbl) throws ExecutionException {
		if (dbl instanceof Double) {
			return ((Double) dbl).doubleValue();
		}
		else {
			throw new ExecutionException("Internal type error. Expected a Double. Got " + classOf(dbl));
		}
	}

	public DSHandle getRawValue(VariableStack stack) throws ExecutionException {
		Object v = super.getValue(stack);
		if(v instanceof DSHandle) {
			return (DSHandle)v;<commit_msg>handle nulls in SwiftArg - this makes misc/ordering-extern-notlazy test work again<commit_after>/*
 * Created on Jul 31, 2007
 */
package org.griphyn.vdl.karajan.lib;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.globus.cog.karajan.arguments.Arg;
import org.globus.cog.karajan.arguments.ArgUtil;
import org.globus.cog.karajan.arguments.VariableArguments;
import org.globus.cog.karajan.stack.VariableStack;
import org.globus.cog.karajan.workflow.ExecutionException;
import org.globus.cog.karajan.workflow.futures.FutureNotYetAvailable;
import org.griphyn.vdl.karajan.PairIterator;
import org.griphyn.vdl.mapping.DSHandle;
import org.griphyn.vdl.type.Type;
import org.griphyn.vdl.type.Types;

public abstract class SwiftArg extends Arg {
	public static final Logger logger = Logger.getLogger(SwiftArg.class);

	public SwiftArg(String name, int index) {
		super(name, index);
	}

	public SwiftArg(String name) {
		super(name, IMPLICIT);
	}

	protected Object unwrap(VariableStack stack, Object val) throws ExecutionException {
		if (val instanceof DSHandle) {
			DSHandle handle = (DSHandle) val;
			if (handle.getType().isArray()) {
				Map value = handle.getArrayValue();
				synchronized(handle.getRoot()) {
					if (handle.isClosed()) {
						return new PairIterator(value);
					}
					else {
						return VDLFunction.addFutureListListener(stack, handle, value);
					}
				}
			}
			if (logger.isDebugEnabled()) {
				logger.debug("SwiftArg.getValue(" + handle + ")");
			}
			synchronized (handle.getRoot()) {
				if (!handle.isClosed()) {
					if (logger.isDebugEnabled()) {
						logger.debug("Waiting for " + handle);
					}
					throw new FutureNotYetAvailable(VDLFunction.addFutureListener(stack, handle));
				}
				else {
					return handle.getValue();
				}
			}
		}
		else {
			throw new ExecutionException("Expected Swift data, but got some primitive type (" + val
					+ ")");
		}
	}

	public Object getValue(VariableStack stack) throws ExecutionException {
		Object v = super.getValue(stack);
		if(v == null) return v;
			else return unwrap(stack, super.getValue(stack));
	}

	public double getDoubleValue(VariableStack stack) throws ExecutionException {
		return checkDouble(getValue(stack));
	}
	
	public static double checkDouble(Object dbl) throws ExecutionException {
		if (dbl instanceof Double) {
			return ((Double) dbl).doubleValue();
		}
		else {
			throw new ExecutionException("Internal type error. Expected a Double. Got " + classOf(dbl));
		}
	}

	public DSHandle getRawValue(VariableStack stack) throws ExecutionException {
		Object v = super.getValue(stack);
		if(v instanceof DSHandle) {
			return (DSHandle)v;
		} else if(v == null) { 
			return null;
{'fraction_non_alphanumeric': 0.11636430273043796, 'fraction_numerical': 0.0025655121861828845, 'mean_word_length': 4.0677808727948, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5705243', 'n_tokens_mistral': 2013, 'n_tokens_neox': 1744, 'n_words': 393}