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

org.eclipse.ocl.internal.evaluation.IterationTemplateCollect Maven / Gradle / Ivy

/**
 * 
 *
 * Copyright (c) 2005, 2008 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   IBM - Initial API and implementation
 *
 * 
 *
 * $Id: IterationTemplateCollect.java,v 1.5 2009/09/01 20:11:22 ewillink Exp $
 */

package org.eclipse.ocl.internal.evaluation;

import java.util.Collection;
import java.util.List;

import org.eclipse.ocl.EvaluationEnvironment;
import org.eclipse.ocl.EvaluationVisitor;
import org.eclipse.ocl.expressions.Variable;
import org.eclipse.ocl.util.CollectionUtil;

/**
 *
 */
public class IterationTemplateCollect
		extends IterationTemplate {
	private IterationTemplateCollect(
			EvaluationVisitor v) {
		super(v);
	}
	
	public static
	IterationTemplate getInstance(
			EvaluationVisitor v) {
		return new IterationTemplateCollect(
				v);
	}
	
	@Override
    protected Object evaluateResult(List> iterators, String resultName, Object bodyVal) {
		EvaluationEnvironment env = getEvalEnvironment();
		
		@SuppressWarnings("unchecked")
		Collection currVal = (Collection) env.getValueOf(resultName);
		
		// If the body result is invalid then the entire expression's value
		// is invalid, because OCL does not permit invalid in a collection
		if (bodyVal == getInvalid()) {
			setDone(true);
			return bodyVal;
		}
		
		if (bodyVal instanceof Collection) {
			Collection bodyColl = (Collection) bodyVal;
			currVal.addAll(CollectionUtil.flatten(bodyColl));
		}
		else
			currVal.add(bodyVal);
		return currVal;
	}
}