From 4d752ece910a5dbd4f4eb28b56ac7021be911470 Mon Sep 17 00:00:00 2001 From: Looly Date: Mon, 3 Apr 2023 19:12:09 +0800 Subject: [PATCH] fix code --- .../collection/queue/DiscardingQueue.java | 68 ++++++++++ .../concurrent/ConcurrentLinkedHashMap.java | 117 ++++-------------- .../core/map/concurrent/package-info.java | 8 +- 3 files changed, 98 insertions(+), 95 deletions(-) create mode 100755 hutool-core/src/main/java/org/dromara/hutool/core/collection/queue/DiscardingQueue.java diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/collection/queue/DiscardingQueue.java b/hutool-core/src/main/java/org/dromara/hutool/core/collection/queue/DiscardingQueue.java new file mode 100755 index 000000000..453fec734 --- /dev/null +++ b/hutool-core/src/main/java/org/dromara/hutool/core/collection/queue/DiscardingQueue.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2023 looly(loolly@aliyun.com) + * Hutool is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package org.dromara.hutool.core.collection.queue; + +import java.util.AbstractQueue; +import java.util.Collections; +import java.util.Iterator; +import java.util.Queue; + +/** + * 始终为空的队列,所有新增节点都丢弃 + * + * @author Guava, looly + * @since 6.0.0 + */ +public class DiscardingQueue extends AbstractQueue { + private static final DiscardingQueue INSTANCE = new DiscardingQueue(); + + /** + * 获取单例的空队列 + * @return DiscardingQueue + * @param 节点类型 + */ + @SuppressWarnings("unchecked") + public static Queue getInstance() { + return (Queue) INSTANCE; + } + + @Override + public boolean add(final Object e) { + return true; + } + + @Override + public boolean offer(final Object e) { + return true; + } + + @Override + public Object poll() { + return null; + } + + @Override + public Object peek() { + return null; + } + + @Override + public int size() { + return 0; + } + + @Override + public Iterator iterator() { + return Collections.emptyIterator(); + } +} diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/map/concurrent/ConcurrentLinkedHashMap.java b/hutool-core/src/main/java/org/dromara/hutool/core/map/concurrent/ConcurrentLinkedHashMap.java index f2fcd0ed1..6e5d615d9 100755 --- a/hutool-core/src/main/java/org/dromara/hutool/core/map/concurrent/ConcurrentLinkedHashMap.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/map/concurrent/ConcurrentLinkedHashMap.java @@ -15,9 +15,12 @@ */ package org.dromara.hutool.core.map.concurrent; +import org.dromara.hutool.core.collection.queue.DiscardingQueue; import org.dromara.hutool.core.collection.queue.Linked; import org.dromara.hutool.core.collection.queue.LinkedDeque; +import org.dromara.hutool.core.lang.Assert; import org.dromara.hutool.core.map.SafeConcurrentHashMap; +import org.dromara.hutool.core.util.RuntimeUtil; import java.io.InvalidObjectException; import java.io.ObjectInputStream; @@ -128,7 +131,7 @@ public final class ConcurrentLinkedHashMap extends AbstractMap /** * The number of CPUs */ - static final int NCPU = Runtime.getRuntime().availableProcessors(); + static final int NCPU = RuntimeUtil.getProcessorCount(); /** * The maximum weighted capacity of the map. @@ -170,11 +173,6 @@ public final class ConcurrentLinkedHashMap extends AbstractMap */ static final int WRITE_BUFFER_DRAIN_THRESHOLD = 16; - /** - * A queue that discards all entries. - */ - static final Queue DISCARDING_QUEUE = new DiscardingQueue(); - @SuppressWarnings("SameParameterValue") static int ceilingNextPowerOfTwo(final int x) { // From Hacker's Delight, Chapter 3, Harry S. Warren Jr. @@ -243,35 +241,7 @@ public final class ConcurrentLinkedHashMap extends AbstractMap // The notification queue and listener listener = builder.listener; pendingNotifications = (listener == DiscardingListener.INSTANCE) - ? (Queue>) DISCARDING_QUEUE - : new ConcurrentLinkedQueue<>(); - } - - /** - * Ensures that the object is not null. - */ - static void checkNotNull(final Object o) { - if (o == null) { - throw new NullPointerException(); - } - } - - /** - * Ensures that the argument expression is true. - */ - static void checkArgument(final boolean expression) { - if (!expression) { - throw new IllegalArgumentException(); - } - } - - /** - * Ensures that the state expression is true. - */ - static void checkState(final boolean expression) { - if (!expression) { - throw new IllegalStateException(); - } + ? DiscardingQueue.getInstance() : new ConcurrentLinkedQueue<>(); } /* ---------------- Eviction Support -------------- */ @@ -293,7 +263,7 @@ public final class ConcurrentLinkedHashMap extends AbstractMap * @throws IllegalArgumentException if the capacity is negative */ public void setCapacity(final long capacity) { - checkArgument(capacity >= 0); + Assert.isTrue(capacity >= 0); evictionLock.lock(); try { this.capacity.lazySet(Math.min(capacity, MAXIMUM_CAPACITY)); @@ -679,7 +649,7 @@ public final class ConcurrentLinkedHashMap extends AbstractMap @Override public boolean containsValue(final Object value) { - checkNotNull(value); + Assert.notNull(value); for (final Node node : data.values()) { if (node.getValue().equals(value)) { @@ -736,8 +706,8 @@ public final class ConcurrentLinkedHashMap extends AbstractMap * @return the prior value in the data store or null if no mapping was found */ V put(final K key, final V value, final boolean onlyIfAbsent) { - checkNotNull(key); - checkNotNull(value); + Assert.notNull(key); + Assert.notNull(value); final int weight = weigher.weightOf(key, value); final WeightedValue weightedValue = new WeightedValue<>(value, weight); @@ -814,8 +784,8 @@ public final class ConcurrentLinkedHashMap extends AbstractMap @Override public V replace(final K key, final V value) { - checkNotNull(key); - checkNotNull(value); + Assert.notNull(key); + Assert.notNull(value); final int weight = weigher.weightOf(key, value); final WeightedValue weightedValue = new WeightedValue<>(value, weight); @@ -843,9 +813,9 @@ public final class ConcurrentLinkedHashMap extends AbstractMap @Override public boolean replace(final K key, final V oldValue, final V newValue) { - checkNotNull(key); - checkNotNull(oldValue); - checkNotNull(newValue); + Assert.notNull(key); + Assert.notNull(oldValue); + Assert.notNull(newValue); final int weight = weigher.weightOf(key, newValue); final WeightedValue newWeightedValue = new WeightedValue<>(newValue, weight); @@ -950,7 +920,7 @@ public final class ConcurrentLinkedHashMap extends AbstractMap } Set orderedKeySet(final boolean ascending, final int limit) { - checkArgument(limit >= 0); + Assert.isTrue(limit >= 0); evictionLock.lock(); try { drainBuffers(); @@ -1060,7 +1030,7 @@ public final class ConcurrentLinkedHashMap extends AbstractMap } Map orderedMap(final boolean ascending, final int limit) { - checkArgument(limit >= 0); + Assert.isTrue(limit >= 0); evictionLock.lock(); try { drainBuffers(); @@ -1277,7 +1247,7 @@ public final class ConcurrentLinkedHashMap extends AbstractMap @Override public void remove() { - checkState(current != null); + Assert.state(current != null); ConcurrentLinkedHashMap.this.remove(current); current = null; } @@ -1329,7 +1299,7 @@ public final class ConcurrentLinkedHashMap extends AbstractMap @Override public void remove() { - checkState(current != null); + Assert.state(current != null); ConcurrentLinkedHashMap.this.remove(current.key); current = null; } @@ -1403,7 +1373,7 @@ public final class ConcurrentLinkedHashMap extends AbstractMap @Override public void remove() { - checkState(current != null); + Assert.state(current != null); ConcurrentLinkedHashMap.this.remove(current.key); current = null; } @@ -1438,14 +1408,14 @@ public final class ConcurrentLinkedHashMap extends AbstractMap final EntryWeigher weigher; BoundedEntryWeigher(final EntryWeigher weigher) { - checkNotNull(weigher); + Assert.notNull(weigher); this.weigher = weigher; } @Override public int weightOf(final K key, final V value) { final int weight = weigher.weightOf(key, value); - checkArgument(weight >= 1); + Assert.isTrue(weight >= 1); return weight; } @@ -1454,41 +1424,6 @@ public final class ConcurrentLinkedHashMap extends AbstractMap } } - /** - * A queue that discards all additions and is always empty. - */ - static final class DiscardingQueue extends AbstractQueue { - @Override - public boolean add(final Object e) { - return true; - } - - @Override - public boolean offer(final Object e) { - return true; - } - - @Override - public Object poll() { - return null; - } - - @Override - public Object peek() { - return null; - } - - @Override - public int size() { - return 0; - } - - @Override - public Iterator iterator() { - return Collections.emptyIterator(); - } - } - /** * A listener that ignores all notifications. */ @@ -1595,7 +1530,7 @@ public final class ConcurrentLinkedHashMap extends AbstractMap * @throws IllegalArgumentException if the initialCapacity is negative */ public Builder initialCapacity(final int initialCapacity) { - checkArgument(initialCapacity >= 0); + Assert.isTrue(initialCapacity >= 0); this.initialCapacity = initialCapacity; return this; } @@ -1610,7 +1545,7 @@ public final class ConcurrentLinkedHashMap extends AbstractMap * negative */ public Builder maximumWeightedCapacity(final long capacity) { - checkArgument(capacity >= 0); + Assert.isTrue(capacity >= 0); this.capacity = capacity; return this; } @@ -1627,7 +1562,7 @@ public final class ConcurrentLinkedHashMap extends AbstractMap * equal to zero */ public Builder concurrencyLevel(final int concurrencyLevel) { - checkArgument(concurrencyLevel > 0); + Assert.isTrue(concurrencyLevel > 0); this.concurrencyLevel = concurrencyLevel; return this; } @@ -1641,7 +1576,7 @@ public final class ConcurrentLinkedHashMap extends AbstractMap * @throws NullPointerException if the listener is null */ public Builder listener(final BiConsumer listener) { - checkNotNull(listener); + Assert.notNull(listener); this.listener = listener; return this; } @@ -1686,7 +1621,7 @@ public final class ConcurrentLinkedHashMap extends AbstractMap * not set */ public ConcurrentLinkedHashMap build() { - checkState(capacity >= 0); + Assert.state(capacity >= 0); return new ConcurrentLinkedHashMap<>(this); } } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/map/concurrent/package-info.java b/hutool-core/src/main/java/org/dromara/hutool/core/map/concurrent/package-info.java index 9ab59a9f0..acbed3583 100755 --- a/hutool-core/src/main/java/org/dromara/hutool/core/map/concurrent/package-info.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/map/concurrent/package-info.java @@ -18,19 +18,19 @@ * This package contains an implementation of a bounded * {@link java.util.concurrent.ConcurrentMap} data structure. *

- * {@link com.googlecode.concurrentlinkedhashmap.Weigher} is a simple interface + * {@link org.dromara.hutool.core.map.concurrent.Weigher} is a simple interface * for determining how many units of capacity an entry consumes. Depending on * which concrete Weigher class is used, an entry may consume a different amount * of space within the cache. The - * {@link com.googlecode.concurrentlinkedhashmap.Weighers} class provides + * {@link org.dromara.hutool.core.map.concurrent.Weighers} class provides * utility methods for obtaining the most common kinds of implementations. *

- * {@link com.googlecode.concurrentlinkedhashmap.EvictionListener} provides the + * {@link org.dromara.hutool.core.map.concurrent.ConcurrentLinkedHashMap#listener} provides the * ability to be notified when an entry is evicted from the map. An eviction * occurs when the entry was automatically removed due to the map exceeding a * capacity threshold. It is not called when an entry was explicitly removed. *

- * The {@link com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap} + * The {@link org.dromara.hutool.core.map.concurrent.ConcurrentLinkedHashMap} * class supplies an efficient, scalable, thread-safe, bounded map. As with the * Java Collections Framework the "Concurrent" prefix is used to * indicate that the map is not governed by a single exclusion lock.