重构代码

dev
ZhouXY108 2024-03-15 09:28:45 +08:00
parent 3d5a3ddbee
commit 4fa4b41fae
1 changed files with 6 additions and 6 deletions

View File

@ -16,11 +16,13 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -86,15 +88,13 @@ public abstract class Enumeration<T extends Enumeration<T>> implements Comparabl
@SafeVarargs @SafeVarargs
@StaticFactoryMethod(ValueSet.class) @StaticFactoryMethod(ValueSet.class)
public static <T extends Enumeration<T>> ValueSet<T> of(T... values) { public static <T extends Enumeration<T>> ValueSet<T> of(T... values) {
Map<Integer, T> temp = new HashMap<>(); Map<Integer, T> temp = Arrays.stream(values)
for (T value : values) { .collect(Collectors.toMap(Enumeration::getId, Function.identity(), (a, b) -> b));
temp.put(value.getId(), value);
}
return new ValueSet<>(Collections.unmodifiableMap(temp)); return new ValueSet<>(Collections.unmodifiableMap(temp));
} }
public T get(int id) { public T get(int id) {
Preconditions.checkArgument(this.valueMap.containsKey(id), "%s 对应的值不存在", id); Preconditions.checkArgument(this.valueMap.containsKey(id), "[%s] 对应的值不存在", id);
return this.valueMap.get(id); return this.valueMap.get(id);
} }