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

📄 element.java

📁  EasyDBO是一个超轻量级对象-关系映射(Object/Relation Mapping
💻 JAVA
字号:
package com.easyjf.cache;

import org.apache.log4j.*;
import java.io.*;
/**
 * 
 * <p>
 * Title: 缓存中的元素
 * </p>
 * 
 * <p>
 * Description: 缓存中的数据通过Element进行统一封装,Element负责记录、处理缓存中对象的访问情况!
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2006
 * </p>
 * 
 * <p>
 * Company: EasyJF开源团队-EasyDBO项目组
 * </p>
 * 
 * @author 大峡
 * @version 1.0
 */
public class Element implements Serializable, Cloneable {
	static final long serialVersionUID = -7401070179721710788L;

	private final static Logger logger = Logger.getLogger(Element.class);

	// ~--- fields -------------------------------------------------------------

	/**
	 * 元素创立时间,也即对象进入缓存的时间
	 */
	private long creationTime;

	/**
	 * 命中数
	 */
	private long hitCount;

	/**
	 * 元素的key,
	 */
	private final Object key;

	/**
	 * 最后一次访问时间
	 */
	private long lastAccessTime;

	/**
	 * 倒数第二次访问时间
	 */
	private long nextToLastAccessTime;

	/**
	 * 缓存的具体对象
	 */
	private Object value;

	/**
	 * 版本
	 */
	private long version;

	// ~--- constructors -------------------------------------------------------
	/**
	 * 构造
	 * 
	 * @param key
	 *            Object
	 * @param value
	 *            Object
	 */
	public Element(Object key, Object value) {
		this(key, value, 1L);
	}

	/**
	 * 
	 * @param key
	 *            Object
	 * @param value
	 *            Object
	 * @param version
	 *            long
	 */
	public Element(Object key, Object value, long version) {
		this.key = key;
		this.value = value;
		this.version = version;
		creationTime = System.currentTimeMillis();
		hitCount = 0;
	}

	// ~--- methods ------------------------------------------------------------
	/**
	 * 
	 * @return Object
	 * @throws CloneNotSupportedException
	 */
	public Object clone() throws CloneNotSupportedException {
		Element element = new Element(deepCopy(key), deepCopy(value), version);

		element.creationTime = creationTime;
		element.lastAccessTime = lastAccessTime;
		element.nextToLastAccessTime = nextToLastAccessTime;
		element.hitCount = hitCount;

		return element;
	}

	/**
	 * 
	 * @param oldValue
	 *            Object
	 * @return Serializable
	 */
	private Serializable deepCopy(Object oldValue) {
		Serializable newValue = null;
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		ObjectOutputStream oos = null;
		ObjectInputStream ois = null;

		try {
			oos = new ObjectOutputStream(bout);
			oos.writeObject(oldValue);

			ByteArrayInputStream bin = new ByteArrayInputStream(bout
					.toByteArray());

			ois = new ObjectInputStream(bin);
			newValue = (Serializable) ois.readObject();
		} catch (IOException e) {
			logger.error("Error cloning Element with key " + key
					+ " during serialization and deserialization of value");
		} catch (ClassNotFoundException e) {
			logger.error("Error cloning Element with key " + key
					+ " during serialization and deserialization of value");
		} finally {
			try {
				oos.close();
				ois.close();
			} catch (IOException e) {
				logger.error("Error closing Stream");
			}
		}

		return newValue;
	}

	/**
	 * 
	 * @param obj
	 *            Object
	 * @return boolean
	 */
	public boolean equals(Object obj) {
		Element element = (Element) obj;
		if (key.equals(element.getKey())) {
			return true;
		} else if (value != null && element.getValue() != null
				&& (value.getClass() == element.getValue().getClass())) {
			return value.equals(element.getValue());
		}
		return false;
	}

	/**
	 * 
	 * @return int
	 */
	public int hashCode() {
		return key.hashCode();
	}

	/**
	 * 
	 */
	public void resetAccessStatistics() {
		lastAccessTime = 0;
		nextToLastAccessTime = 0;
		hitCount = 0;
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();

		sb.append("[ key = ").append(key).append(", value=").append(value)
				.append(", version=").append(version).append(", hitCount=")
				.append(hitCount).append(", CreationTime = ").append(
						this.getCreationTime()).append(", LastAccessTime = ")
				.append(this.getLastAccessTime()).append(" ]");

		return sb.toString();
	}

	/**
	 * 
	 */
	public void updateAccessStatistics() {
		nextToLastAccessTime = lastAccessTime;
		lastAccessTime = System.currentTimeMillis();
		hitCount++;
	}

	// ~--- get methods --------------------------------------------------------
	/**
	 * 
	 * @return long
	 */
	public long getCreationTime() {
		return creationTime;
	}

	/**
	 * 
	 * @return long
	 */
	public long getHitCount() {
		return hitCount;
	}

	/**
	 * 
	 * @return Object
	 */
	public Object getKey() {
		return key;
	}

	/**
	 * 
	 * @return long
	 */
	public long getLastAccessTime() {
		return lastAccessTime;
	}

	/**
	 * 
	 * @return long
	 */
	long getNextToLastAccessTime() {
		return nextToLastAccessTime;
	}

	/**
	 * 
	 * @return long
	 */
	public long getSerializedSize() {
		long size = 0;
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		ObjectOutputStream oos = null;

		try {
			oos = new ObjectOutputStream(bout);
			oos.writeObject(this);
			size = bout.size();

			return size;
		} catch (IOException e) {
			logger.error("Error measuring element size for element with key "
					+ key);
		} finally {
			try {
				oos.close();
			} catch (IOException e) {
				logger.error("Error closing ObjectOutputStream");
			}
		}

		return size;
	}

	public Object getValue() {
		return value;
	}

	public long getVersion() {
		return version;
	}

	// ~--- set methods --------------------------------------------------------

	public void setCreateTime() {
		creationTime = System.currentTimeMillis();
	}

	public void setVersion(long version) {
		this.version = version;
	}
}

⌨️ 快捷键说明

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