package cn.edu.seu.ise.dca.util;

import java.util.List;

import cn.edu.seu.ise.dca.app.php.model.PHPClass;
import cn.edu.seu.ise.dca.app.php.model.PHPFunction;

public class ListUtil {
	/**
	 * Check whether the phpClass is in the list of phpClasses
	 * @param phpClass
	 * @param phpClasses
	 * @return
	 */
	public static boolean isInList(PHPClass phpClass, List<PHPClass> phpClasses) {
		for(PHPClass aPhpClass : phpClasses) {
			if(aPhpClass.getFullName().equalsIgnoreCase(phpClass.getFullName())) {
				return true;
			}	
		}
		return false;
	}
	
	/**
	 * Check whether the phpFunction is in the list of phpFunctions
	 * @param phpFunction
	 * @param phpFunctions
	 * @return
	 */
	public static boolean isInList(PHPFunction phpFunction, List<PHPFunction> phpFunctions) {
		for(PHPFunction aPhpFunction : phpFunctions) {
			if(aPhpFunction.getFullName().equalsIgnoreCase(phpFunction.getFullName())) {
				return true;
			}	
		}
		return false;
	}
	
	/**
	 * Check whether the number is in the list
	 * @param num
	 * @param numbers
	 * @return
	 */
	public static boolean isInList(long num, List<Long> numbers) {
		for(Long number : numbers) {
			if(number.longValue() == num) {
				return true;
			}	
		}
		return false;
	}
	
	/**
	 * Return the index of specific PHP class in PHP class list 
	 * @param phpClass
	 * @param phpClasses
	 * @return
	 */
	public static int indexOf(PHPClass phpClass, List<PHPClass> phpClasses) {
		int index = -1;
		for(PHPClass aPhpClass : phpClasses) {
			index ++;
			if(aPhpClass.getFullName().equalsIgnoreCase(phpClass.getFullName())) {
				return index;
			}	
		}
		return -1;
	}
	
	/**
	 * Return the index of specific PHP function in PHP function list 
	 * @param phpFunction
	 * @param phpFunctions
	 * @return
	 */
	public static int indexOf(PHPFunction phpFunction, List<PHPFunction> phpFunctions) {
		int index = -1;
		for(PHPFunction aPhpFunction : phpFunctions) {
			index ++;
			if(aPhpFunction.getFullName().equalsIgnoreCase(phpFunction.getFullName())) {
				return index;
			}	
		}
		return -1;
	}
	
	/**
	 * Check whether the String is in the string array
	 * @param e
	 * @param elements
	 * @return
	 */
	public static boolean isInArray(String e, String[] elements) {
		if(e == null) {
			return false;
		}
		
		boolean isIn = false;
		for(String element : elements) {
			if(e.equalsIgnoreCase(element)) {
				isIn = true;
				break;
			}
		}
		return isIn;
	}

	/**
	 * Check whether the String is start with any prefix in the candidate prefix list
	 * @param path
	 * @param prefixes
	 * @return
	 */
	public static boolean isStartWith(String path, List<String> prefixes) {
		boolean isIn = false;
		for(String prefix : prefixes) {
			if(path.startsWith(prefix)) {
				isIn = true;
				break;
			}
		}
		return isIn;
	}

}
