稍修改 Enumeration;修改测试内容。

feature/net-util
ZhouXY108 2023-04-15 03:59:04 +08:00
parent 8712d954e2
commit 49408d91d6
2 changed files with 40 additions and 11 deletions

View File

@ -31,7 +31,7 @@ public abstract class Enumeration<T extends Enumeration<T>> implements Comparabl
@Nonnull @Nonnull
protected final String name; protected final String name;
protected Enumeration(int id, @Nonnull String name) { protected Enumeration(final int id, @Nonnull final String name) {
this.id = id; this.id = id;
this.name = name; this.name = name;
} }
@ -46,7 +46,7 @@ public abstract class Enumeration<T extends Enumeration<T>> implements Comparabl
} }
@Override @Override
public final int compareTo(T o) { public final int compareTo(final T o) {
return Integer.compare(this.id, o.id); return Integer.compare(this.id, o.id);
} }
@ -56,7 +56,7 @@ public abstract class Enumeration<T extends Enumeration<T>> implements Comparabl
} }
@Override @Override
public final boolean equals(Object obj) { public final boolean equals(final Object obj) {
if (this == obj) if (this == obj)
return true; return true;
if (obj == null) if (obj == null)
@ -70,7 +70,7 @@ public abstract class Enumeration<T extends Enumeration<T>> implements Comparabl
@Override @Override
@Nonnull @Nonnull
public final String toString() { public final String toString() {
return getClass().getSimpleName() + "[" + id + ": " + name + "]"; return getClass().getSimpleName() + '(' + id + ":" + name + ')';
} }
protected static final class ValueSet<T extends Enumeration<T>> { protected static final class ValueSet<T extends Enumeration<T>> {
@ -83,12 +83,12 @@ public abstract class Enumeration<T extends Enumeration<T>> implements Comparabl
} }
} }
private void put(@Nonnull T value) { private void put(@Nonnull final T value) {
this.values.put(value.getId(), value); this.values.put(value.getId(), value);
} }
@Nonnull @Nonnull
public T get(int id) { public T get(final int id) {
return Objects.requireNonNull(this.values.get(id)); return Objects.requireNonNull(this.values.get(id));
} }

View File

@ -1,7 +1,9 @@
package xyz.zhouxy.plusone.commons; package xyz.zhouxy.plusone.commons;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -15,8 +17,14 @@ class EnumerationTests {
@Test @Test
void testEnumeration() { void testEnumeration() {
assertEquals(EntityStatus.AVAILABLE, EntityStatus.of(0)); assertTrue(EntityStatus.AVAILABLE == EntityStatus.of(0));
log.info(EntityStatus.constants().toString()); assertTrue(Result.SUCCESSFUL == Result.of(1));
Collection<Comparable<? extends Enumeration<?>>> enums = Lists.newArrayList();
enums.addAll(EntityStatus.constants());
enums.addAll(Result.constants());
for (Comparable<? extends Enumeration<?>> anEnum : enums) {
log.info(anEnum.toString());
}
} }
} }
@ -30,16 +38,37 @@ 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> ENUMERATION_VALUES = new ValueSet<>( private static final ValueSet<EntityStatus> VALUE_SET = new ValueSet<>(
AVAILABLE, DISABLED); AVAILABLE, DISABLED);
@Nonnull @Nonnull
public static EntityStatus of(int value) { public static EntityStatus of(int value) {
return ENUMERATION_VALUES.get(value); return VALUE_SET.get(value);
} }
@Nonnull @Nonnull
public static Collection<EntityStatus> constants() { public static Collection<EntityStatus> constants() {
return ENUMERATION_VALUES.getValues(); return VALUE_SET.getValues();
}
}
final class Result extends Enumeration<Result> {
private Result(int id, @Nonnull String name) {
super(id, name);
}
public static final Result SUCCESSFUL = new Result(1, "成功");
public static final Result FAILURE = new Result(0, "失败");
private static final ValueSet<Result> VALUE_SET = new ValueSet<>(SUCCESSFUL, FAILURE);
@Nonnull
public static Result of(int id) {
return VALUE_SET.get(id);
}
@Nonnull
public static Collection<Result> constants() {
return VALUE_SET.getValues();
} }
} }