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

📄 context.java

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

import java.util.*;

/**
 * 简单的上下文类,目前上下文被看作一串相关的名字和值构成的对
 * 
 * @author 周晓聪
 * @since 2007/10/15
 * @version 2.0
 *
 */

public class Context {
	private String contextName = null;			// 上下文名称
	private String contextId = null;			// 自动生成的一个上下文惟一标识
	private static int id = 0;				// 用于辅助生成上下文惟一标识
	
	// 使用列表容器存放该上下文真正的信息 
	private ArrayList<ContextData> contextData = null;
	
	public Context(String name) { 
		contextName = new String(name);
		// 利用当前时间转换而成的串作为上下文的惟一标识
		contextId = System.currentTimeMillis()+ id + "";  id++;
		contextData = new ArrayList<ContextData>(5);
	}
	
	/**
	 *  往上下文中添加名字、类型和值对
	 */
	public void addContextData(String name, String type, Object value) {
		contextData.add(new ContextData(name, type, value));
	}
	
	/**
	 * 往上下文中添加名字和类型
	 * @param name
	 * @param type
	 */
	public void addContextData(String name, String type) {
		contextData.add(new ContextData(name, type, null));
	}

	/**
	 * 添加上下文信息
	 */
	public void addContextData(ContextData data) {
		contextData.add(data);
	}
	
	/**
	 * 返回上下文名字
	 */
	public String getContextName() {
		return contextName;
	}
	
	/**
	 *  通过名字取值
	 * @param name 需要取值的名字
	 * @return 返回该名字相应的值
	 */
	public Object getValue(String name) {
		for (int index = 0; index < contextData.size(); index++) {
			ContextData data = contextData.get(index);
			if (data.getName().equals(name)) {
				return data.getValue();
			}
		}
		return null;
	}

	/**
	 *  通过名字取类型信息
	 * @param name 需要取值的名字
	 * @return 返回该名字相应的值
	 */
	public String getType(String name) {
		for (int index = 0; index < contextData.size(); index++) {
			ContextData data = contextData.get(index);
			if (data.getName().equals(name)) {
				return data.getType();
			}
		}
		return null;
	}
	
	/**
	 * 获取上下文中信息对的个数
	 */
	public int getContextDataSize() {
		return contextData.size();
	}
	
	/**
	 * 返回下标为 index 的上下文信息对 
	 */
	public ContextData getContextData(int index) {
		return contextData.get(index);
	}

	/**
	 * 自动生成的 hashCode() 方法,将上下文惟一标识字段进行散列值的计算
	 */
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + ((contextId == null) ? 0 : contextId.hashCode());
		return result;
	}

	/**
	 * 自动生成的 equals() 方法,两个上下文只要惟一标识字段相同就是相同的上下文
	 */
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (obj == null) return false;
		if (getClass() != obj.getClass()) return false;
		final Context other = (Context) obj;
		if (contextId == null) {
			if (other.contextId != null) return false;
		} else if (!contextId.equals(other.contextId)) 	return false;
		return true;
	}
	
	/**
	 * 转换成字符,以方便调试和测试时输出
	 */
	public String toString() {
		StringBuffer result = new StringBuffer("[");
		
		result.append(contextName + "<" + contextId + ">: ");
		Iterator iterator = contextData.iterator();
		while (iterator.hasNext()) {
			ContextData obj = (ContextData)iterator.next();
			result.append("(" + obj.getName() + ", " + obj.getType() + ", " + obj.getValue() + ") ");
		}
		result.append("]\n");
		return result.toString();
	}
}

⌨️ 快捷键说明

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