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) {
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) {
return new LinkedBlockingQueue<>(capacity);
}
@Override
<T> BlockingQueue<T> of() {
return new LinkedBlockingQueue<>();
}
},
/**
* {@link java.util.concurrent.LinkedBlockingDeque}
*/
LINKED_BLOCKING_DEQUE(3, "LinkedBlockingDeque") {
@Override
<T> BlockingQueue<T> of(Integer capacity) {
return new LinkedBlockingDeque<>(capacity);
}
@Override
<T> BlockingQueue<T> of() {
return new LinkedBlockingDeque<>();
@ -73,6 +88,11 @@ public enum BlockingQueueTypeEnum {
* {@link java.util.concurrent.SynchronousQueue}
*/
SYNCHRONOUS_QUEUE(4, "SynchronousQueue") {
@Override
<T> BlockingQueue<T> of(Integer capacity) {
return new SynchronousQueue<>();
}
@Override
<T> BlockingQueue<T> of() {
return new SynchronousQueue<>();
@ -83,6 +103,11 @@ public enum BlockingQueueTypeEnum {
* {@link java.util.concurrent.LinkedTransferQueue}
*/
LINKED_TRANSFER_QUEUE(5, "LinkedTransferQueue") {
@Override
<T> BlockingQueue<T> of(Integer capacity) {
return new LinkedTransferQueue<>();
}
@Override
<T> BlockingQueue<T> of() {
return new LinkedTransferQueue<>();
@ -97,6 +122,11 @@ public enum BlockingQueueTypeEnum {
<T> BlockingQueue<T> of(Integer 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) {
return new ResizableCapacityLinkedBlockingQueue<>(capacity);
}
@Override
<T> BlockingQueue<T> 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<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.
* Abstract method, depends on sub override
@ -141,9 +158,7 @@ public enum BlockingQueueTypeEnum {
* @param <T> the class of the objects in the BlockingQueue
* @return a BlockingQueue view of the specified T
*/
<T> BlockingQueue<T> of(Integer capacity) {
throw new NotSupportedException("该队列必须有界");
}
abstract <T> BlockingQueue<T> 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
*/
<T> BlockingQueue<T> of() {
throw new NotSupportedException("该队列不支持有界");
abstract <T> BlockingQueue<T> of();
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) {
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 <T> BlockingQueue<T> customOrDefaultQueue(Integer capacity, Predicate<CustomBlockingQueue> predicate) {
Collection<CustomBlockingQueue> customBlockingQueues = DynamicThreadPoolServiceLoader
.getSingletonServiceInstances(CustomBlockingQueue.class);

Loading…
Cancel
Save