package com.kurento.kms.idl.codegen.function;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.kurento.kms.idl.model.ComplexType;
import com.kurento.kms.idl.model.Method;
import com.kurento.kms.idl.model.Param;
import com.kurento.kms.idl.model.RemoteClass;
import com.kurento.kms.idl.model.Return;
import com.kurento.kms.idl.model.Type;

import freemarker.ext.beans.StringModel;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;

public class RemoteClassDependencies implements TemplateMethodModelEx {

	@Override
	public Object exec(@SuppressWarnings("rawtypes") List arguments)
			throws TemplateModelException {

		Object type = arguments.get(0);

		if (type instanceof StringModel) {
			type = ((StringModel) type).getWrappedObject();
			if (type instanceof Return) {
				type = ((Return) type).getType();
			}
		}

		Set<Type> types = new HashSet<Type>();

		if (type instanceof RemoteClass) {
			RemoteClass remoteClass = (RemoteClass) type;

			for (Method method : remoteClass.getConstructors()) {
				types.addAll(getMethodTypes(method));
			}

			for (Method method : remoteClass.getMethods()) {
				types.addAll(getMethodTypes(method));
			}

			if (remoteClass.getExtends() != null)
				types.remove(remoteClass.getExtends().getType());

			types.remove(remoteClass);
		}

		return types;
	}

	private Set<Type> getMethodTypes(Method method) {
		Set<Type> types = new HashSet<Type>();

		for (Param p : method.getParams()) {

			if (p.getType().getType() instanceof RemoteClass
					|| p.getType().getType() instanceof ComplexType) {
				types.add(p.getType().getType());
			}

		}

		Return ret = method.getReturn();

		if (ret != null
				&& (ret.getType().getType() instanceof RemoteClass || ret
						.getType().getType() instanceof ComplexType))
			types.add(ret.getType().getType());

		return types;
	}
}

@@ -57,22 +57,25 @@
 va.util.
-HashSe
+LinkedLis
 t;%0Aimpor
@@ -95,30 +95,8 @@
 ist;
-%0Aimport java.util.Set;
 %0A%0Aim
@@ -882,18 +882,19 @@
 %0A%09%09%7D%0A%0A%09%09
-Se
+Lis
 t%3CType%3E 
@@ -897,38 +897,41 @@
 pe%3E types = new 
-HashSe
+LinkedLis
 t%3CType%3E();%0A%0A%09%09if
@@ -1378,18 +1378,19 @@
 private 
-Se
+Lis
 t%3CType%3E 
@@ -1423,18 +1423,19 @@
 od) %7B%0A%09%09
-Se
+Lis
 t%3CType%3E 
@@ -1450,14 +1450,17 @@
 new 
-HashSe
+LinkedLis
 t%3CTy

Make cpp code generator deterministic avoiding use of sets