All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.libkodi.objectlist.withexpires.PeriodMap Maven / Gradle / Ivy

The newest version!
package io.github.libkodi.objectlist.withexpires;

import java.util.Iterator;
import java.util.Map.Entry;

import io.github.libkodi.objectlist.ObjectList;


/**
 * 数据集,用来管理一个数据列表并使其具备过期性质
 *
 * @param  该数据集保存的数据类型
 */
public class PeriodMap {
	private Object mutex;
	private ObjectList> value = new ObjectList>();
	
	public PeriodMap() {
		mutex = this;
	}
	
	/**
	 * 
	 * 添加一个数据
	 *
	 * @param key 键
	 * @param value 值
	 * @param idleTimeout 最大闲置时间
	 * @param aliveTimeout 最大存活时间
	 */
	public void put(String key, T value, int idleTimeout, int aliveTimeout) {
		synchronized (mutex) {
			this.value.unshift(key, new PeriodMapNode(value, idleTimeout, aliveTimeout));
		}
	}
	
	/**
	 * 
	 * 添加一个数据
	 *
	 * @param key 键
	 * @param value 值
	 */
	public void put(String key, T value) {
		synchronized (mutex) {
			this.value.unshift(key, new PeriodMapNode(value, 0, 0));
		}
	}
	
	/**
	 * 
	 * 替换
	 *
	 * @param key 键
	 * @param value 值
	 */
	public void replace(String key, T value) {
		synchronized (mutex) {
			this.value.replace(key, new PeriodMapNode(value));
		}
	}
	
	/**
	 * 
	 * 获取数据
	 *
	 * @param key 键
	 * @return Object
	 */
	public T get(String key) {
		synchronized (mutex) {
			PeriodMapNode node = value.get(key);
			
			if (node != null) {
				if (node.isIdleTimeout() || node.isAliveTimeout()) {
					value.remove(key);
					return null;
				}
				
				node.renew();
				value.unshift(key, node);
				return node.getValue();
			}
			
			return null;
		}
	}
	
	/**
	 * 
	 * 是否为空
	 *
	 * @return true/false
	 */
	public boolean isEmpty() {
		synchronized (mutex) {
			return value.isEmpty();
		}
	}
	
	/**
	 * 清空列表
	 */
	public void clear() {
		synchronized (mutex) {
			value.clear();
		}
	}
	
	/**
	 * 
	 * 是否包含指定数据
	 *
	 * @param key 键
	 * @return true/false
	 */
	public boolean containsKey(String key) {
		synchronized (mutex) {
			return value.containsKey(key);
		}
	}
	
	/**
	 * 
	 * 移除数据
	 *
	 * @param key 键
	 */
	public T remove(String key) {
		synchronized (mutex) {
			PeriodMapNode node = value.remove(key);
			
			if (node != null) {
				return node.getValue();
			} else {
				return null;
			}
		}
	}
	
	/**
	 * 
	 * 更新数据并移除过期数据
	 *
	 */
	public void update() {
		synchronized (mutex) {
			Iterator>> iter = value.iterator(true);
			
			while(iter.hasNext()) {
				Entry> entry = iter.next();
				PeriodMapNode node = entry.getValue();
				
				if (node.isIdleTimeout() || node.isAliveTimeout()) {
					value.remove(entry.getKey());
				} else {
					break;
				}
			}
		}
	}
	
	/**
	 * 
	 * 获取遍历
	 *
	 * @return
	 */
	public Iterator> iterator() {
		Iterator>> iter = value.iterator();
		
		return new Iterator>() {

			@Override
			public boolean hasNext() {
				return iter.hasNext();
			}

			@Override
			public Entry next() {
				Entry> item = iter.next();
				return new Entry() {

					@Override
					public String getKey() {
						return item.getKey();
					}

					@Override
					public T getValue() {
						PeriodMapNode node = item.getValue();
						return node != null ? node.getValue() : null;
					}

					@Override
					public T setValue(T value) {
						return null;
					}
					
				};
			}
		};
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy