From 06e8e0505187064dbae20f717d94b33f33f5beaa Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 17 Oct 2019 11:26:19 +0800 Subject: [PATCH] fix code --- .../main/java/cn/hutool/cache/CacheUtil.java | 18 +++++----- .../cn/hutool/cache/GlobalPruneTimer.java | 35 +++++++++---------- .../cn/hutool/cache/impl/AbstractCache.java | 5 ++- .../java/cn/hutool/cache/impl/FIFOCache.java | 2 +- 4 files changed, 29 insertions(+), 31 deletions(-) diff --git a/hutool-cache/src/main/java/cn/hutool/cache/CacheUtil.java b/hutool-cache/src/main/java/cn/hutool/cache/CacheUtil.java index cac25c555..cddc81e62 100644 --- a/hutool-cache/src/main/java/cn/hutool/cache/CacheUtil.java +++ b/hutool-cache/src/main/java/cn/hutool/cache/CacheUtil.java @@ -24,7 +24,7 @@ public class CacheUtil { * @return {@link FIFOCache} */ public static FIFOCache newFIFOCache(int capacity, long timeout){ - return new FIFOCache(capacity, timeout); + return new FIFOCache<>(capacity, timeout); } /** @@ -36,7 +36,7 @@ public class CacheUtil { * @return {@link FIFOCache} */ public static FIFOCache newFIFOCache(int capacity){ - return new FIFOCache(capacity); + return new FIFOCache<>(capacity); } /** @@ -49,7 +49,7 @@ public class CacheUtil { * @return {@link LFUCache} */ public static LFUCache newLFUCache(int capacity, long timeout){ - return new LFUCache(capacity, timeout); + return new LFUCache<>(capacity, timeout); } /** @@ -61,7 +61,7 @@ public class CacheUtil { * @return {@link LFUCache} */ public static LFUCache newLFUCache(int capacity){ - return new LFUCache(capacity); + return new LFUCache<>(capacity); } @@ -75,7 +75,7 @@ public class CacheUtil { * @return {@link LRUCache} */ public static LRUCache newLRUCache(int capacity, long timeout){ - return new LRUCache(capacity, timeout); + return new LRUCache<>(capacity, timeout); } /** @@ -87,7 +87,7 @@ public class CacheUtil { * @return {@link LRUCache} */ public static LRUCache newLRUCache(int capacity){ - return new LRUCache(capacity); + return new LRUCache<>(capacity); } /** @@ -99,7 +99,7 @@ public class CacheUtil { * @return {@link TimedCache} */ public static TimedCache newTimedCache(long timeout){ - return new TimedCache(timeout); + return new TimedCache<>(timeout); } /** @@ -112,7 +112,7 @@ public class CacheUtil { * @since 3.0.7 */ public static WeakCache newWeakCache(long timeout){ - return new WeakCache(timeout); + return new WeakCache<>(timeout); } /** @@ -123,7 +123,7 @@ public class CacheUtil { * @return {@link NoCache} */ public static NoCache newNoCache(){ - return new NoCache(); + return new NoCache<>(); } } diff --git a/hutool-cache/src/main/java/cn/hutool/cache/GlobalPruneTimer.java b/hutool-cache/src/main/java/cn/hutool/cache/GlobalPruneTimer.java index 4fa05e929..a7f95fcda 100644 --- a/hutool-cache/src/main/java/cn/hutool/cache/GlobalPruneTimer.java +++ b/hutool-cache/src/main/java/cn/hutool/cache/GlobalPruneTimer.java @@ -1,30 +1,34 @@ package cn.hutool.cache; +import cn.hutool.core.thread.ThreadUtil; +import cn.hutool.core.util.StrUtil; + import java.util.List; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; -import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; -import cn.hutool.core.thread.ThreadUtil; -import cn.hutool.core.util.StrUtil; - /** * 全局缓存清理定时器池,用于在需要过期支持的缓存对象中超时任务池 - * - * @author looly * + * @author looly */ public enum GlobalPruneTimer { - /** 单例对象 */ + /** + * 单例对象 + */ INSTANCE; - /** 缓存任务计数 */ + /** + * 缓存任务计数 + */ private AtomicInteger cacheTaskNumber = new AtomicInteger(1); - /** 定时器 */ + /** + * 定时器 + */ private ScheduledExecutorService pruneTimer; /** @@ -36,8 +40,8 @@ public enum GlobalPruneTimer { /** * 启动定时任务 - * - * @param task 任务 + * + * @param task 任务 * @param delay 周期 * @return {@link ScheduledFuture}对象,可手动取消此任务 */ @@ -52,12 +56,7 @@ public enum GlobalPruneTimer { if (null != pruneTimer) { shutdownNow(); } - this.pruneTimer = new ScheduledThreadPoolExecutor(16, new ThreadFactory() { - @Override - public Thread newThread(Runnable r) { - return ThreadUtil.newThread(r, StrUtil.format("Pure-Timer-{}", cacheTaskNumber.getAndIncrement())); - } - }); + this.pruneTimer = new ScheduledThreadPoolExecutor(16, r -> ThreadUtil.newThread(r, StrUtil.format("Pure-Timer-{}", cacheTaskNumber.getAndIncrement()))); } /** @@ -71,7 +70,7 @@ public enum GlobalPruneTimer { /** * 销毁全局定时器 - * + * * @return 销毁时未被执行的任务列表 */ public List shutdownNow() { diff --git a/hutool-cache/src/main/java/cn/hutool/cache/impl/AbstractCache.java b/hutool-cache/src/main/java/cn/hutool/cache/impl/AbstractCache.java index da0fa2e82..799483594 100644 --- a/hutool-cache/src/main/java/cn/hutool/cache/impl/AbstractCache.java +++ b/hutool-cache/src/main/java/cn/hutool/cache/impl/AbstractCache.java @@ -71,7 +71,7 @@ public abstract class AbstractCache implements Cache { * @since 4.5.16 */ private void putWithoutLock(K key, V object, long timeout) { - CacheObj co = new CacheObj(key, object, timeout); + CacheObj co = new CacheObj<>(key, object, timeout); if (timeout != 0) { existCustomTimeout = true; } @@ -190,10 +190,9 @@ public abstract class AbstractCache implements Cache { // ---------------------------------------------------------------- get end @Override - @SuppressWarnings("unchecked") public Iterator iterator() { CacheObjIterator copiedIterator = (CacheObjIterator) this.cacheObjIterator(); - return new CacheValuesIterator(copiedIterator); + return new CacheValuesIterator<>(copiedIterator); } @Override diff --git a/hutool-cache/src/main/java/cn/hutool/cache/impl/FIFOCache.java b/hutool-cache/src/main/java/cn/hutool/cache/impl/FIFOCache.java index f61f740d7..ae25c4bc9 100644 --- a/hutool-cache/src/main/java/cn/hutool/cache/impl/FIFOCache.java +++ b/hutool-cache/src/main/java/cn/hutool/cache/impl/FIFOCache.java @@ -42,7 +42,7 @@ public class FIFOCache extends AbstractCache { this.capacity = capacity; this.timeout = timeout; - cacheMap = new LinkedHashMap>(capacity + 1, 1.0f, false); + cacheMap = new LinkedHashMap<>(capacity + 1, 1.0f, false); } /**