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

cn.hutool.cache.impl.CacheObjIterator Maven / Gradle / Ivy

Go to download

Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。

There is a newer version: 5.8.34
Show newest version
package cn.hutool.cache.impl;

import java.io.Serializable;
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * {@link cn.hutool.cache.impl.AbstractCache} 的CacheObj迭代器.
 *
 * @author looly
 *
 * @param  键类型
 * @param  值类型
 * @since 4.0.10
 */
public class CacheObjIterator implements Iterator>, Serializable {
	private static final long serialVersionUID = 1L;

	private final Iterator> iterator;
	private CacheObj nextValue;

	/**
	 * 构造
	 *
	 * @param iterator 原{@link Iterator}
	 */
	CacheObjIterator(Iterator> iterator) {
		this.iterator = iterator;
		nextValue();
	}

	/**
	 * @return 是否有下一个值
	 */
	@Override
	public boolean hasNext() {
		return nextValue != null;
	}

	/**
	 * @return 下一个值
	 */
	@Override
	public CacheObj next() {
		if (false == hasNext()) {
			throw new NoSuchElementException();
		}
		final CacheObj cachedObject = nextValue;
		nextValue();
		return cachedObject;
	}

	/**
	 * 从缓存中移除没有过期的当前值,此方法不支持
	 */
	@Override
	public void remove() {
		throw new UnsupportedOperationException("Cache values Iterator is not support to modify.");
	}

	/**
	 * 下一个值,当不存在则下一个值为null
	 */
	private void nextValue() {
		while (iterator.hasNext()) {
			nextValue = iterator.next();
			if (nextValue.isExpired() == false) {
				return;
			}
		}
		nextValue = null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy