84 lines
2.1 KiB
Java
Raw Normal View History

2023-03-13 14:26:03 +08:00
/*
* Copyright 2022-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2023-02-24 11:10:27 +08:00
package xyz.zhouxy.plusone.commons.util;
2022-11-07 17:49:27 +08:00
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
2023-02-24 11:10:27 +08:00
/**
* 枚举类
*/
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() {
2023-03-13 16:33:38 +08:00
return "[" + value + ": " + name + "]";
2022-12-15 11:43:24 +08:00
}
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
}