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

com.ecfeed.core.model.FixedChoiceValueFactory Maven / Gradle / Ivy

Go to download

An open library used to connect to the ecFeed service. It can be also used as a standalone testing tool. It is integrated with Junit5 and generates a stream of test cases using a selected algorithm (e.g. Cartesian, N-Wise). There are no limitations associated with the off-line version but the user cannot access the on-line computation servers and the model database.

The newest version!
/*******************************************************************************
 *
 * Copyright (c) 2016 ecFeed AS.                                                
 * 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 
 *  
 *******************************************************************************/

package com.ecfeed.core.model;

import com.ecfeed.core.implementation.ModelClassLoader;
import com.ecfeed.core.type.adapter.ITypeAdapter;
import com.ecfeed.core.type.adapter.TypeAdapterProvider;
import com.ecfeed.core.utils.ERunMode;
import com.ecfeed.core.utils.JavaTypeHelper;

public class FixedChoiceValueFactory {

	// TODO - add createValueString method: createValue().toString();
	private ModelClassLoader fLoader;
	boolean fIsExport;

	public FixedChoiceValueFactory(ModelClassLoader loader, boolean isExport){

		fLoader = loader;
		fIsExport = isExport; // TODO - rename
	}

	public Object createValue(ChoiceNode choice){

		if(choice.getParameter() != null) {
			return createValue(choice.getValueString(), choice.isRandomizedValue(), choice.getParameter().getType());
		}
		
		return null;
	}

	public Object createValue(String valueString, boolean isRandomized, String typeName) {

		if (typeName == null || valueString == null) {
			return null;
		}

		String convertedValueString = valueString;

		if (isRandomized) {
			TypeAdapterProvider typeAdapterProvider = new TypeAdapterProvider();
			ITypeAdapter typeAdapter = typeAdapterProvider.getAdapter(typeName);
			convertedValueString = typeAdapter.generateValueAsString(valueString);  
		}

		if (JavaTypeHelper.isJavaType(typeName)) {
			return JavaTypeHelper.parseJavaType(convertedValueString, typeName, ERunMode.QUIET); 
		}
		
		if (fIsExport) {
			return convertedValueString;
		}

		return parseUserTypeValue(convertedValueString, typeName);
	}
	
	private Object parseUserTypeValue(String valueString, String typeName) {

		Object value = null;
		Class typeClass = null;
		
		try {
			typeClass = fLoader.loadClass(typeName);
		} catch(Exception e) {
			return valueString;
		}
		
		if (typeClass != null) {
			for (Object object: typeClass.getEnumConstants()) {
				if ((((Enum)object).name()).equals(valueString)) {
					value = object;
					break;
				}
			}
		}

		return value;
	}


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy