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

com.github.cosycode.ext.io.cache.ObjCacheChain Maven / Gradle / Ivy

Go to download

扩展模块, 用于存放一些非常用的工具或模块的扩展类, 例如在poi基础上扩展的excel的导入模块, 模拟按键模块

The newest version!
package com.github.cosycode.ext.io.cache;

import com.github.cosycode.common.lang.BaseRuntimeException;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

import java.util.Iterator;
import java.util.List;
import java.util.Objects;

/**
 * Description :  改对象维持一个 ICacheStack 的对象列表,
 * 获取元素的时候从头开始, 从 ICacheStack 的实例对象里面获取数据.
 * 获取到则直接返回, 获取不到则到下一个 ICacheStack 的实例对象里面获取元素, 直到获取到元素为止.
 * 获取到之后, 还会讲元素存储到前面的 ICacheStack 实例中去.
 * 

* created in 2022/12/8 *

* * @author CPF * @since 0.2.2 **/ @Slf4j @AllArgsConstructor public class ObjCacheChain { final List> cacheHandlerList; public T getData() { Objects.requireNonNull(cacheHandlerList); if (cacheHandlerList.isEmpty()) { throw new BaseRuntimeException("cacheHandlerList is empty"); } AbstractObjCacheHandler firstHandler = cacheHandlerList.get(0); T t = firstHandler.get(); if (t == null || !firstHandler.validate(t)) { synchronized (cacheHandlerList) { t = firstHandler.get(); if (t == null || ! firstHandler.validate(t)) { Iterator> iterator = cacheHandlerList.iterator(); return getData(iterator); } } } return t; } /** * 该方法不应该返回 null, 在调用该方法之前, 请确保 iterator 中的最后一个 AbstractCacheHandler 对象实例一定可以获取到合法的对象实例. * * @param iterator 迭代器 * @return 从 iterator 里面获取到的对象. */ private T getData(@NonNull Iterator> iterator) { if (! iterator.hasNext()) { // 假如说最后 一级从 http 上面获取, 那么若该级别的 get 获取不到合法数据, 那么还应该有下一级. throw new BaseRuntimeException("The cacheHandlerList is empty. Please make sure that the last AbstractCacheHandler instance object can correctly obtain the element."); } /* 若是获取到元素则返回, */ AbstractObjCacheHandler item = iterator.next(); T pop = item.get(); if (pop != null) { boolean validate = item.validate(pop); if (validate) { return pop; } else { log.debug("{}[{}] clear the invalid element", item.getClass(), item.getTag()); item.clear(); } } pop = getData(iterator); // 从下一个 AbstractCacheHandler 获取到的元素不应该为 null Objects.requireNonNull(pop); log.debug("Get the valid elements and cache them in {}[{}]", item.getClass(), item.getTag()); item.put(pop); return pop; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy