refactor BlockingQueueTypeEnum (#1109)

pull/1116/head
Kevin 3 years ago committed by GitHub
parent f65dbb5521
commit 72aa732688
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -20,18 +20,8 @@ package cn.hippo4j.common.executor.support;
import cn.hippo4j.common.spi.DynamicThreadPoolServiceLoader; import cn.hippo4j.common.spi.DynamicThreadPoolServiceLoader;
import lombok.Getter; import lombok.Getter;
import java.util.Collection; import java.util.*;
import java.util.HashMap; import java.util.concurrent.*;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.function.Predicate; import java.util.function.Predicate;
/** /**
@ -145,10 +135,10 @@ public enum BlockingQueueTypeEnum {
}; };
@Getter @Getter
private Integer type; private final Integer type;
@Getter @Getter
private String name; private final String name;
/** /**
* Create the specified implement of BlockingQueue with init capacity. * Create the specified implement of BlockingQueue with init capacity.
@ -175,16 +165,16 @@ public enum BlockingQueueTypeEnum {
this.name = name; this.name = name;
} }
private static Map<Integer, BlockingQueueTypeEnum> typeToEnumMap; private static final Map<Integer, BlockingQueueTypeEnum> TYPE_TO_ENUM_MAP;
private static Map<String, BlockingQueueTypeEnum> nameToEnumMap; private static final Map<String, BlockingQueueTypeEnum> NAME_TO_ENUM_MAP;
static { static {
final BlockingQueueTypeEnum[] values = BlockingQueueTypeEnum.values(); final BlockingQueueTypeEnum[] values = BlockingQueueTypeEnum.values();
typeToEnumMap = new HashMap<>(values.length); TYPE_TO_ENUM_MAP = new HashMap<>(values.length);
nameToEnumMap = new HashMap<>(values.length); NAME_TO_ENUM_MAP = new HashMap<>(values.length);
for (BlockingQueueTypeEnum value : values) { for (BlockingQueueTypeEnum value : values) {
typeToEnumMap.put(value.type, value); TYPE_TO_ENUM_MAP.put(value.type, value);
nameToEnumMap.put(value.name, value); NAME_TO_ENUM_MAP.put(value.name, value);
} }
} }
@ -198,7 +188,7 @@ public enum BlockingQueueTypeEnum {
* @return a BlockingQueue view of the specified T * @return a BlockingQueue view of the specified T
*/ */
private static <T> BlockingQueue<T> of(String blockingQueueName, Integer capacity) { private static <T> BlockingQueue<T> of(String blockingQueueName, Integer capacity) {
final BlockingQueueTypeEnum typeEnum = nameToEnumMap.get(blockingQueueName); final BlockingQueueTypeEnum typeEnum = NAME_TO_ENUM_MAP.get(blockingQueueName);
if (typeEnum == null) { if (typeEnum == null) {
return null; return null;
} }
@ -215,7 +205,7 @@ public enum BlockingQueueTypeEnum {
* @return a BlockingQueue view of the specified T * @return a BlockingQueue view of the specified T
*/ */
private static <T> BlockingQueue<T> of(int type, Integer capacity) { private static <T> BlockingQueue<T> of(int type, Integer capacity) {
final BlockingQueueTypeEnum typeEnum = typeToEnumMap.get(type); final BlockingQueueTypeEnum typeEnum = TYPE_TO_ENUM_MAP.get(type);
if (typeEnum == null) { if (typeEnum == null) {
return null; return null;
} }
@ -262,7 +252,7 @@ public enum BlockingQueueTypeEnum {
} }
return customOrDefaultQueue(capacity, return customOrDefaultQueue(capacity,
(customeQueue) -> Objects.equals(customeQueue.getName(), blockingQueueName)); (customerQueue) -> Objects.equals(customerQueue.getName(), blockingQueueName));
} }
/** /**
@ -293,7 +283,7 @@ public enum BlockingQueueTypeEnum {
* @return {@link BlockingQueueTypeEnum#name BlockingQueueTypeEnum.name } or "". * @return {@link BlockingQueueTypeEnum#name BlockingQueueTypeEnum.name } or "".
*/ */
public static String getBlockingQueueNameByType(int type) { public static String getBlockingQueueNameByType(int type) {
return Optional.ofNullable(typeToEnumMap.get(type)) return Optional.ofNullable(TYPE_TO_ENUM_MAP.get(type))
.map(value -> value.getName()) .map(value -> value.getName())
.orElse(""); .orElse("");
} }
@ -306,7 +296,7 @@ public enum BlockingQueueTypeEnum {
* @return enum {@link BlockingQueueTypeEnum} * @return enum {@link BlockingQueueTypeEnum}
*/ */
public static BlockingQueueTypeEnum getBlockingQueueTypeEnumByName(String name) { public static BlockingQueueTypeEnum getBlockingQueueTypeEnumByName(String name) {
return Optional.ofNullable(nameToEnumMap.get(name)) return Optional.ofNullable(NAME_TO_ENUM_MAP.get(name))
.orElse(LINKED_BLOCKING_QUEUE); .orElse(LINKED_BLOCKING_QUEUE);
} }
} }

Loading…
Cancel
Save