From d369391acadf9668e3914f2924625499e0b5449f Mon Sep 17 00:00:00 2001 From: machen Date: Mon, 13 Mar 2023 21:15:48 +0800 Subject: [PATCH] Refactor blocking queue enum --- .../support/BlockingQueueTypeEnum.java | 80 +++++++++++++------ 1 file changed, 55 insertions(+), 25 deletions(-) diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/executor/support/BlockingQueueTypeEnum.java b/hippo4j-common/src/main/java/cn/hippo4j/common/executor/support/BlockingQueueTypeEnum.java index 31ab11da..6d4d2d26 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/executor/support/BlockingQueueTypeEnum.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/executor/support/BlockingQueueTypeEnum.java @@ -47,6 +47,11 @@ public enum BlockingQueueTypeEnum { BlockingQueue of(Integer capacity) { return new ArrayBlockingQueue<>(capacity); } + + @Override + BlockingQueue of() { + return new ArrayBlockingQueue<>(DEFAULT_CAPACITY); + } }, /** @@ -57,12 +62,22 @@ public enum BlockingQueueTypeEnum { BlockingQueue of(Integer capacity) { return new LinkedBlockingQueue<>(capacity); } + + @Override + BlockingQueue of() { + return new LinkedBlockingQueue<>(); + } }, /** * {@link java.util.concurrent.LinkedBlockingDeque} */ LINKED_BLOCKING_DEQUE(3, "LinkedBlockingDeque") { + @Override + BlockingQueue of(Integer capacity) { + return new LinkedBlockingDeque<>(capacity); + } + @Override BlockingQueue of() { return new LinkedBlockingDeque<>(); @@ -73,6 +88,11 @@ public enum BlockingQueueTypeEnum { * {@link java.util.concurrent.SynchronousQueue} */ SYNCHRONOUS_QUEUE(4, "SynchronousQueue") { + @Override + BlockingQueue of(Integer capacity) { + return new SynchronousQueue<>(); + } + @Override BlockingQueue of() { return new SynchronousQueue<>(); @@ -83,6 +103,11 @@ public enum BlockingQueueTypeEnum { * {@link java.util.concurrent.LinkedTransferQueue} */ LINKED_TRANSFER_QUEUE(5, "LinkedTransferQueue") { + @Override + BlockingQueue of(Integer capacity) { + return new LinkedTransferQueue<>(); + } + @Override BlockingQueue of() { return new LinkedTransferQueue<>(); @@ -97,6 +122,11 @@ public enum BlockingQueueTypeEnum { BlockingQueue of(Integer capacity) { return new PriorityBlockingQueue<>(capacity); } + + @Override + BlockingQueue of() { + return new PriorityBlockingQueue<>(); + } }, /** @@ -107,6 +137,11 @@ public enum BlockingQueueTypeEnum { BlockingQueue of(Integer capacity) { return new ResizableCapacityLinkedBlockingQueue<>(capacity); } + + @Override + BlockingQueue of() { + return new ResizableCapacityLinkedBlockingQueue<>(); + } }; @Getter @@ -115,24 +150,6 @@ public enum BlockingQueueTypeEnum { @Getter private String name; - BlockingQueueTypeEnum(int type, String name) { - this.type = type; - this.name = name; - } - - private static Map typeToEnumMap; - private static Map nameToEnumMap; - - static { - final BlockingQueueTypeEnum[] values = BlockingQueueTypeEnum.values(); - typeToEnumMap = new HashMap<>(values.length); - nameToEnumMap = new HashMap<>(values.length); - for (BlockingQueueTypeEnum value : values) { - typeToEnumMap.put(value.type, value); - nameToEnumMap.put(value.name, value); - } - } - /** * Create the specified implement of BlockingQueue with init capacity. * Abstract method, depends on sub override @@ -141,9 +158,7 @@ public enum BlockingQueueTypeEnum { * @param the class of the objects in the BlockingQueue * @return a BlockingQueue view of the specified T */ - BlockingQueue of(Integer capacity) { - throw new NotSupportedException("该队列必须有界"); - } + abstract BlockingQueue of(Integer capacity); /** * Create the specified implement of BlockingQueue,has no capacity limit. @@ -153,8 +168,24 @@ public enum BlockingQueueTypeEnum { * @return a BlockingQueue view of the specified T * @throws NotSupportedException */ - BlockingQueue of() { - throw new NotSupportedException("该队列不支持有界"); + abstract BlockingQueue of(); + + BlockingQueueTypeEnum(int type, String name) { + this.type = type; + this.name = name; + } + + private static Map typeToEnumMap; + private static Map nameToEnumMap; + + static { + final BlockingQueueTypeEnum[] values = BlockingQueueTypeEnum.values(); + typeToEnumMap = new HashMap<>(values.length); + nameToEnumMap = new HashMap<>(values.length); + for (BlockingQueueTypeEnum value : values) { + typeToEnumMap.put(value.type, value); + nameToEnumMap.put(value.name, value); + } } /** @@ -188,7 +219,7 @@ public enum BlockingQueueTypeEnum { if (typeEnum == null) { return null; } - return Objects.isNull(capacity) ? typeEnum.of() : typeEnum.of(capacity); + return typeEnum.of(); } private static final int DEFAULT_CAPACITY = 1024; @@ -197,7 +228,6 @@ public enum BlockingQueueTypeEnum { DynamicThreadPoolServiceLoader.register(CustomBlockingQueue.class); } - private static BlockingQueue customOrDefaultQueue(Integer capacity, Predicate predicate) { Collection customBlockingQueues = DynamicThreadPoolServiceLoader .getSingletonServiceInstances(CustomBlockingQueue.class);