⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 algorithmgroup.java

📁 用JAVA实现排序等简单算法的演示
💻 JAVA
字号:
package contextsModelManager;

import java.util.*;

/**
 * 用于表示算法组的类
 * @author 周晓聪
 * @version 1.0
 * @since 2007/10/15
 *
 */
public class AlgorithmGroup {
	private String groupName = "";							// 算法组名称
	private ArrayList<AlgorithmData> algorithms = null; 	// 算法组中的算法信息

	public AlgorithmGroup(String name) {
		groupName = name;
		algorithms = new ArrayList<AlgorithmData>(10);
	}
	
	/**
	 * 添加算法信息
	 * @param name 算法名称
	 * @param extendsAlgorithms 算法所扩展的算法,也即实现该算法的类将是实现哪个算法的类的子类
	 * @param className 实现算法的类名
	 * @param methodName 实现算法的方法名
	 * @throws ContextModelException 可能会由于算法上下文模型描述不正确而产生一些异常!
	 */
	public void addAlgorithm(String name, String extendsAlgorithms, 
			String className, String methodName) throws ContextModelException {
		// 对参数做一些预处理,以方面后续的检查
		if (name == null) name = "";
		else name = name.trim();
		if (extendsAlgorithms == null) extendsAlgorithms = "";
		else extendsAlgorithms = extendsAlgorithms.trim();
		if (className == null) className = "";
		else className = className.trim();
		if (methodName == null) methodName = "";
		else methodName = methodName.trim();
		
		
		if (name.equals("")) throw new ContextModelException("Incorrect algorithm name!");
		
		if (className.equals("") || methodName.equals("")) {
			// 实现算法的类名为空,或者方法名为空,都意味着需要从该算法所扩展的算法中获取相应的信息,因此这时不允许所扩展的算法信息为空
			if (extendsAlgorithms.equals("")) throw new ContextModelException("Algorithm [" + name + "] information is incomplete, expect implementation information or extension information!");
			
			// 从已经加入的算法中查找当前算法所扩展的算法
			boolean foundSuper = false;
			for (int index = 0; index < algorithms.size(); index++) {
				AlgorithmData info = algorithms.get(index);
				if (info.getName().equals(extendsAlgorithms)) {
					if (className.equals("")) className = info.getImplementationClass();
					// 如果 className 为空,则不管这里是否设置方法名,都应该使用所继承的算法的实现方法
					methodName = info.getImplementationMethod();
					foundSuper = true;
					break;
				}
			}
			
			if (foundSuper == false) throw new ContextModelException("Can not found the super algorithm [" + extendsAlgorithms + "] for algorithm [" + name + "]!");
		}
		
		algorithms.add(new AlgorithmData(name, className, methodName, extendsAlgorithms));
	}
	
	/**
	 * 获取算法组名
	 */
	public String getAlgorithmGroupName() {
		return groupName;
	}

	/**
	 * 获取实现算法 algorithmName 的类名
	 */
	public String getAlgorithmImplementationClass(String algorithmName) {
		for (int index = 0; index < algorithms.size(); index++) {
			AlgorithmData info = algorithms.get(index);
			if (info.getName().equals(algorithmName)) return info.getImplementationClass();
		}
		
		return null;
	}

	/**
	 * 获取实现算法 algorithmName 的方法名
	 */
	public String getAlgorithmImplementationMethod(String algorithmName) {
		for (int index = 0; index < algorithms.size(); index++) {
			AlgorithmData info = algorithms.get(index);
			if (info.getName().equals(algorithmName)) return info.getImplementationMethod();
		}
		
		return null;
	}

	/**
	 * 返回算法组中所包含的算法个数
	 */
	public int getAlgorithmNum() {
		return algorithms.size();
	}
	
	/**
	 * 获取算法组中下标为 index 的算法的信息,这可用于遍历算法组中的所有算法信息
	 */
	public AlgorithmData getAlgorithmData(int index) {
		return algorithms.get(index);
	}

	/**
	 * 将算法组的信息转换为字符串,以方便调试和测试时输出
	 */
	public String toString() {
		StringBuffer result = new StringBuffer("Algorithm group: " + groupName + "\n");

		for (int index = 0; index < algorithms.size(); index++) {
			AlgorithmData info = algorithms.get(index);
			result.append("    " + info.toString() + "\n");
		}
		return result.toString();
	}
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -