更改不可变 Map 的构建方式;改用静态工厂方法创建 ValueSet 对象。

feature/net-util
ZhouXY108 2023-04-30 11:14:28 +08:00
parent 404188abda
commit 1c5d5d4129
2 changed files with 19 additions and 13 deletions

View File

@ -17,11 +17,13 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
/** /**
* *
*/ */
@ -30,8 +32,9 @@ public abstract class Enumeration<T extends Enumeration<T>> implements Comparabl
protected final String name; protected final String name;
protected Enumeration(final int id, final String name) { protected Enumeration(final int id, final String name) {
Assert.hasText(name, "Name of enumeration must has text.");
this.id = id; this.id = id;
this.name = Objects.requireNonNull(name); this.name = name;
} }
public final int getId() { public final int getId() {
@ -70,16 +73,20 @@ public abstract class Enumeration<T extends Enumeration<T>> implements Comparabl
} }
protected static final class ValueSet<T extends Enumeration<T>> { protected static final class ValueSet<T extends Enumeration<T>> {
private final Map<Integer, T> valueMap; private final ImmutableMap<Integer, T> valueMap;
private ValueSet(ImmutableMap<Integer, T> valueMap) {
this.valueMap = valueMap;
}
@SafeVarargs @SafeVarargs
public ValueSet(T... values) { @StaticFactoryMethod(ValueSet.class)
Map<Integer, T> temp = new HashMap<>(values.length); public static <T extends Enumeration<T>> ValueSet<T> of(T... values) {
Builder<Integer, T> builder = ImmutableMap.builder();
for (T value : values) { for (T value : values) {
Assert.notNull(value, "Value must not be null."); builder.put(value.getId(), value);
temp.put(value.getId(), value);
} }
this.valueMap = Collections.unmodifiableMap(temp); return new ValueSet<>(builder.build());
} }
public T get(int id) { public T get(int id) {

View File

@ -38,8 +38,7 @@ final class EntityStatus extends Enumeration<EntityStatus> {
public static final EntityStatus AVAILABLE = new EntityStatus(0, "正常"); public static final EntityStatus AVAILABLE = new EntityStatus(0, "正常");
public static final EntityStatus DISABLED = new EntityStatus(1, "禁用"); public static final EntityStatus DISABLED = new EntityStatus(1, "禁用");
private static final ValueSet<EntityStatus> VALUE_SET = new ValueSet<>( private static final ValueSet<EntityStatus> VALUE_SET = ValueSet.of(AVAILABLE, DISABLED);
AVAILABLE, DISABLED);
public static EntityStatus of(int value) { public static EntityStatus of(int value) {
return VALUE_SET.get(value); return VALUE_SET.get(value);
@ -58,7 +57,7 @@ final class Result extends Enumeration<Result> {
public static final Result SUCCESSFUL = new Result(1, "成功"); public static final Result SUCCESSFUL = new Result(1, "成功");
public static final Result FAILURE = new Result(0, "失败"); public static final Result FAILURE = new Result(0, "失败");
private static final ValueSet<Result> VALUE_SET = new ValueSet<>(SUCCESSFUL, FAILURE); private static final ValueSet<Result> VALUE_SET = ValueSet.of(SUCCESSFUL, FAILURE);
public static Result of(int id) { public static Result of(int id) {
return VALUE_SET.get(id); return VALUE_SET.get(id);