95 lines
2.6 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-04-15 02:47:11 +08:00
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
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-04-15 02:47:11 +08:00
2023-02-24 11:10:27 +08:00
/**
* 枚举类
*/
2023-04-15 02:47:11 +08:00
public abstract class Enumeration<T extends Enumeration<T>> implements Comparable<T> {
protected final int id;
2022-11-07 17:49:27 +08:00
protected final String name;
protected Enumeration(final int id, final String name) {
2023-04-15 02:47:11 +08:00
this.id = id;
this.name = Objects.requireNonNull(name);
2022-11-07 17:49:27 +08:00
}
2023-04-15 02:47:11 +08:00
public final int getId() {
return id;
2022-11-07 17:49:27 +08:00
}
2023-04-15 02:47:11 +08:00
public final String getName() {
2022-11-07 17:49:27 +08:00
return name;
}
@Override
public final int compareTo(final T o) {
2023-04-15 02:47:11 +08:00
return Integer.compare(this.id, o.id);
2022-11-07 17:49:27 +08:00
}
@Override
2023-04-15 02:47:11 +08:00
public final int hashCode() {
return Objects.hash(id);
}
@Override
public final boolean equals(final Object obj) {
2022-11-07 17:49:27 +08:00
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Enumeration<?> other = (Enumeration<?>) obj;
2023-04-15 02:47:11 +08:00
return id == other.id;
2022-11-07 17:49:27 +08:00
}
2022-12-15 11:43:24 +08:00
@Override
2023-04-15 02:47:11 +08:00
public final String toString() {
return getClass().getSimpleName() + '(' + id + ":" + name + ')';
2022-12-15 11:43:24 +08:00
}
2023-02-18 11:58:06 +08:00
2023-04-15 02:47:11 +08:00
protected static final class ValueSet<T extends Enumeration<T>> {
private final Map<Integer, T> valueMap;
2023-02-18 11:58:06 +08:00
@SafeVarargs
2023-04-15 02:47:11 +08:00
public ValueSet(T... values) {
Map<Integer, T> temp = new HashMap<>(values.length);
2023-02-18 11:58:06 +08:00
for (T value : values) {
Assert.notNull(value, "Value must not be null.");
temp.put(value.getId(), value);
2023-02-18 11:58:06 +08:00
}
this.valueMap = Collections.unmodifiableMap(temp);
2023-02-18 11:58:06 +08:00
}
2023-04-15 02:47:11 +08:00
public T get(int id) {
Assert.isTrue(this.valueMap.containsKey(id), "%s 对应的值不存在", id);
return this.valueMap.get(id);
2023-04-15 02:47:11 +08:00
}
public Collection<T> getValues() {
return this.valueMap.values();
2023-02-18 11:58:06 +08:00
}
}
2022-11-07 17:49:27 +08:00
}