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

📄 bufferpool.java

📁 软件设计课做的一个类似Hibernate的O/R Mapping的框架
💻 JAVA
字号:
package cn.edu.nju.software.sd.torm.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * The BufferPool is a structure used to act as a object buffer. It is actually
 * a two-level map, in which stores all the recently used object of all the
 * pesistentable classes. It is used in
 * <tt>PersistenceManager<tt> to allay the access to the database.
 * 
 * @author Yinfei XU
 * @version 1.0.0
 */
public class BufferPool {
	/**
	 * The two-level map.
	 */
	private Map<Class, Map<Integer, Object>> buffer;

	/**
	 * Create a new instance of BufferPool, by using the setwhich contains all
	 * the persistentable classes.
	 * 
	 * @param classes
	 *            The set which contains all the persistentable classes.
	 */
	public BufferPool(Set<Class> classes) {
		// Initialize the buffer pool;
		buffer = new HashMap<Class, Map<Integer, Object>>();
		Iterator<Class> itr = classes.iterator();
		while (itr.hasNext()) {
			Map<Integer, Object> map = new HashMap<Integer, Object>();
			buffer.put(itr.next(), map);
		}
	}

	/**
	 * To determine whether the object is in the pool.
	 * 
	 * @param o
	 *            The target object.
	 * @param id
	 *            The id of the object.
	 * @return Whether the object exists.
	 */
	public boolean exists(Object o, int id) {
		return (this.exists(o.getClass(), id) || this.exists(o.getClass()
				.getSuperclass(), id));
	}

	/**
	 * To determine whether an object is in the pool.
	 * 
	 * @param c
	 *            The class of the target object.
	 * @param id
	 *            The id of the object.
	 * @return Whether the object exists.
	 */
	public boolean exists(Class c, int id) {
		Map<Integer, Object> map;
		if ((map = buffer.get(c)) != null) {
			Object oo = map.get(id);
			if (oo != null)
				return true;
		}
		return false;
	}

	/**
	 * Get the object of class c with the object id specified by id.
	 * 
	 * @param c
	 *            The class of the object.
	 * @param id
	 *            The id of the object.
	 * @return The target object.
	 */
	public Object get(Class c, int id) {
		Map<Integer, Object> map = buffer.get(c);
		if (map == null)
			return null;
		return map.get(id);
	}

	/**
	 * Delete the object with the object id specified by id.
	 * 
	 * @param o
	 *            The target object.
	 * @param id
	 *            The id of the object.
	 * @return The object which is just been deleted, and null if the object
	 *         dosen't exists.
	 */
	public Object delete(Object o, int id) {
		Object n = this.delete(o.getClass(), id);
		if (n == null)
			n = this.delete(o.getClass().getSuperclass(), id);
		return n;
	}

	/**
	 * Delete the object of class c with the object id specified by id.
	 * 
	 * @param c
	 *            The class of thetarget object.
	 * @param id
	 *            The id of the object.
	 * @return The object which is just been deleted, and null if the object
	 *         dosen't exists.
	 */
	public Object delete(Class c, int id) {
		Map<Integer, Object> map;
		if ((map = buffer.get(c)) != null) {
			return map.remove(id);
		}
		return null;
	}

	/**
	 * Add an object to the pool.
	 * 
	 * @param o
	 *            The object to be added.
	 * @param id
	 *            The id of the object.
	 * @return Whether the object is successfully added.
	 */
	public boolean add(Object o, int id) {
		Map<Integer, Object> map = buffer.get(o.getClass());
		if (map == null)
			map = buffer.get(o.getClass().getSuperclass());
		if (map != null) {
			if (!this.exists(o.getClass(), id)) {
				map.put(id, o);
				return true;
			}
		}
		return false;
	}

	/**
	 * Remove all the objects stored in the pool.
	 * 
	 * @return Whether all the objects are successfully removed.
	 */
	public boolean clearAll() {
		Collection<Map<Integer, Object>> maps = buffer.values();
		Iterator<Map<Integer, Object>> itr = maps.iterator();
		while (itr.hasNext()) {
			itr.next().clear();
		}
		return true;
	}

	/**
	 * Remove all the object of class c stored in the pool
	 * 
	 * @param c
	 *            The type of the objects to be removed.
	 * @return Whether all the objects are successfully removed.
	 */
	public boolean clear(Class c) {
		Map<Integer, Object> map;
		if ((map = buffer.get(c)) != null) {
			map.clear();
			return true;
		}
		return false;
	}
}

⌨️ 快捷键说明

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