2022-11-07 17:49:27 +08:00
|
|
|
package xyz.zhouxy.plusone.util;
|
|
|
|
|
2023-02-18 11:58:06 +08:00
|
|
|
import java.util.Map;
|
2022-11-07 17:49:27 +08:00
|
|
|
import java.util.Objects;
|
2023-02-18 11:58:06 +08:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2022-11-07 17:49:27 +08:00
|
|
|
|
|
|
|
public abstract class Enumeration<T extends Enumeration<T>> {
|
|
|
|
protected final int value;
|
|
|
|
protected final String name;
|
|
|
|
|
|
|
|
protected Enumeration(int value, String name) {
|
|
|
|
this.value = value;
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getValue() {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return Objects.hash(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if (this == obj)
|
|
|
|
return true;
|
|
|
|
if (obj == null)
|
|
|
|
return false;
|
|
|
|
if (getClass() != obj.getClass())
|
|
|
|
return false;
|
|
|
|
Enumeration<?> other = (Enumeration<?>) obj;
|
|
|
|
return value == other.value;
|
|
|
|
}
|
2022-12-15 11:43:24 +08:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
builder.append("[").append(value).append(": ").append(name).append("]");
|
|
|
|
return builder.toString();
|
|
|
|
}
|
2023-02-18 11:58:06 +08:00
|
|
|
|
|
|
|
protected static final class EnumerationValuesHolder<T extends Enumeration<T>> {
|
|
|
|
private final Map<Integer, T> constants = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
@SafeVarargs
|
|
|
|
public EnumerationValuesHolder(T... values) {
|
|
|
|
for (T value : values) {
|
|
|
|
put(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void put(T constant) {
|
|
|
|
this.constants.put(constant.getValue(), constant);
|
|
|
|
}
|
|
|
|
|
|
|
|
public T get(int value) {
|
|
|
|
return this.constants.get(value);
|
|
|
|
}
|
|
|
|
}
|
2022-11-07 17:49:27 +08:00
|
|
|
}
|