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

com.ecfeed.core.model.RootNodeHelper 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 java.util.Optional;

import com.ecfeed.core.utils.PackageClassHelper;

public class RootNodeHelper {

	public static String generateNewClassName(RootNode rootNode, String startClassName) {
		boolean defaultPackage = !PackageClassHelper.hasPackageName(startClassName);

		for (int i = 1;   ; i++) {
			String newClassName = startClassName + String.valueOf(i);
			Optional validatedNewClassName = validateClassName(rootNode, newClassName, defaultPackage);
			
			if (validatedNewClassName.isPresent()) {
				return validatedNewClassName.get();
			}
		}
	}
	
	private static Optional validateClassName(RootNode rootNode, String newClassName, boolean defaultPackage) {
		
		if (rootNode.getClass(newClassName) == null) {
			return validateClassPackage(rootNode, newClassName, defaultPackage);
		}
		
		return Optional.empty();
	}
	
	private static Optional validateClassPackage(RootNode rootNode, String newClassName, boolean defaultPackage) {
		
		if (defaultPackage) {
			if (isUniqueAcrossPackages(rootNode, newClassName)) {
				return Optional.of(newClassName);
			}
		} else {
			return Optional.of(newClassName);
		}
		
		return Optional.empty();
	}
	
	private static boolean isUniqueAcrossPackages(RootNode rootNode, String newClassName) {
		
		for (ClassNode node : rootNode.getClasses()) {
			if (node.getFullName().endsWith(newClassName)) {
				return false;
			}
		}
		
		return true;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy