Refactor blocking queue enum

pull/1103/head
machen 2 years ago
parent 64a7656dff
commit d369391aca

@ -47,6 +47,11 @@ public enum BlockingQueueTypeEnum {
<T> BlockingQueue<T> of(Integer capacity) { <T> BlockingQueue<T> of(Integer capacity) {
return new ArrayBlockingQueue<>(capacity); return new ArrayBlockingQueue<>(capacity);
} }
@Override
<T> BlockingQueue<T> of() {
return new ArrayBlockingQueue<>(DEFAULT_CAPACITY);
}
}, },
/** /**
@ -57,12 +62,22 @@ public enum BlockingQueueTypeEnum {
<T> BlockingQueue<T> of(Integer capacity) { <T> BlockingQueue<T> of(Integer capacity) {
return new LinkedBlockingQueue<>(capacity); return new LinkedBlockingQueue<>(capacity);
} }
@Override
<T> BlockingQueue<T> of() {
return new LinkedBlockingQueue<>();
}
}, },
/** /**
* {@link java.util.concurrent.LinkedBlockingDeque} * {@link java.util.concurrent.LinkedBlockingDeque}
*/ */
LINKED_BLOCKING_DEQUE(3, "LinkedBlockingDeque") { LINKED_BLOCKING_DEQUE(3, "LinkedBlockingDeque") {
@Override
<T> BlockingQueue<T> of(Integer capacity) {
return new LinkedBlockingDeque<>(capacity);
}
@Override @Override
<T> BlockingQueue<T> of() { <T> BlockingQueue<T> of() {
return new LinkedBlockingDeque<>(); return new LinkedBlockingDeque<>();
@ -73,6 +88,11 @@ public enum BlockingQueueTypeEnum {
* {@link java.util.concurrent.SynchronousQueue} * {@link java.util.concurrent.SynchronousQueue}
*/ */
SYNCHRONOUS_QUEUE(4, "SynchronousQueue") { SYNCHRONOUS_QUEUE(4, "SynchronousQueue") {
@Override
<T> BlockingQueue<T> of(Integer capacity) {
return new SynchronousQueue<>();
}
@Override @Override
<T> BlockingQueue<T> of() { <T> BlockingQueue<T> of() {
return new SynchronousQueue<>(); return new SynchronousQueue<>();
@ -83,6 +103,11 @@ public enum BlockingQueueTypeEnum {
* {@link java.util.concurrent.LinkedTransferQueue} * {@link java.util.concurrent.LinkedTransferQueue}
*/ */
LINKED_TRANSFER_QUEUE(5, "LinkedTransferQueue") { LINKED_TRANSFER_QUEUE(5, "LinkedTransferQueue") {
@Override
<T> BlockingQueue<T> of(Integer capacity) {
return new LinkedTransferQueue<>();
}
@Override @Override
<T> BlockingQueue<T> of() { <T> BlockingQueue<T> of() {
return new LinkedTransferQueue<>(); return new LinkedTransferQueue<>();
@ -97,6 +122,11 @@ public enum BlockingQueueTypeEnum {
<T> BlockingQueue<T> of(Integer capacity) { <T> BlockingQueue<T> of(Integer capacity) {
return new PriorityBlockingQueue<>(capacity); return new PriorityBlockingQueue<>(capacity);
} }
@Override
<T> BlockingQueue<T> of() {
return new PriorityBlockingQueue<>();
}
}, },
/** /**
@ -107,6 +137,11 @@ public enum BlockingQueueTypeEnum {
<T> BlockingQueue<T> of(Integer capacity) { <T> BlockingQueue<T> of(Integer capacity) {
return new ResizableCapacityLinkedBlockingQueue<>(capacity); return new ResizableCapacityLinkedBlockingQueue<>(capacity);
} }
@Override
<T> BlockingQueue<T> of() {
return new ResizableCapacityLinkedBlockingQueue<>();
}
}; };
@Getter @Getter
@ -115,24 +150,6 @@ public enum BlockingQueueTypeEnum {
@Getter @Getter
private String name; private String name;
BlockingQueueTypeEnum(int type, String name) {
this.type = type;
this.name = name;
}
private static Map<Integer, BlockingQueueTypeEnum> typeToEnumMap;
private static Map<String, BlockingQueueTypeEnum> 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. * Create the specified implement of BlockingQueue with init capacity.
* Abstract method, depends on sub override * Abstract method, depends on sub override
@ -141,9 +158,7 @@ public enum BlockingQueueTypeEnum {
* @param <T> the class of the objects in the BlockingQueue * @param <T> the class of the objects in the BlockingQueue
* @return a BlockingQueue view of the specified T * @return a BlockingQueue view of the specified T
*/ */
<T> BlockingQueue<T> of(Integer capacity) { abstract <T> BlockingQueue<T> of(Integer capacity);
throw new NotSupportedException("该队列必须有界");
}
/** /**
* Create the specified implement of BlockingQueue,has no capacity limit. * 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 * @return a BlockingQueue view of the specified T
* @throws NotSupportedException * @throws NotSupportedException
*/ */
<T> BlockingQueue<T> of() { abstract <T> BlockingQueue<T> of();
throw new NotSupportedException("该队列不支持有界");
BlockingQueueTypeEnum(int type, String name) {
this.type = type;
this.name = name;
}
private static Map<Integer, BlockingQueueTypeEnum> typeToEnumMap;
private static Map<String, BlockingQueueTypeEnum> 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) { if (typeEnum == null) {
return null; return null;
} }
return Objects.isNull(capacity) ? typeEnum.of() : typeEnum.of(capacity); return typeEnum.of();
} }
private static final int DEFAULT_CAPACITY = 1024; private static final int DEFAULT_CAPACITY = 1024;
@ -197,7 +228,6 @@ public enum BlockingQueueTypeEnum {
DynamicThreadPoolServiceLoader.register(CustomBlockingQueue.class); DynamicThreadPoolServiceLoader.register(CustomBlockingQueue.class);
} }
private static <T> BlockingQueue<T> customOrDefaultQueue(Integer capacity, Predicate<CustomBlockingQueue> predicate) { private static <T> BlockingQueue<T> customOrDefaultQueue(Integer capacity, Predicate<CustomBlockingQueue> predicate) {
Collection<CustomBlockingQueue> customBlockingQueues = DynamicThreadPoolServiceLoader Collection<CustomBlockingQueue> customBlockingQueues = DynamicThreadPoolServiceLoader
.getSingletonServiceInstances(CustomBlockingQueue.class); .getSingletonServiceInstances(CustomBlockingQueue.class);

Loading…
Cancel
Save