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;
|
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-04-15 21:52:15 +08:00
|
|
|
import javax.annotation.CheckForNull;
|
2023-04-15 02:47:11 +08:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
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;
|
|
|
|
@Nonnull
|
2022-11-07 17:49:27 +08:00
|
|
|
protected final String name;
|
|
|
|
|
2023-04-15 03:59:04 +08:00
|
|
|
protected Enumeration(final int id, @Nonnull final String name) {
|
2023-04-15 02:47:11 +08:00
|
|
|
this.id = id;
|
2022-11-07 17:49:27 +08:00
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
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
|
2023-04-15 03:59:04 +08:00
|
|
|
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
|
2023-04-15 03:59:04 +08:00
|
|
|
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() {
|
2023-04-15 03:59:04 +08:00
|
|
|
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> values = new ConcurrentHashMap<>();
|
2023-02-18 11:58:06 +08:00
|
|
|
|
|
|
|
@SafeVarargs
|
2023-04-15 02:47:11 +08:00
|
|
|
public ValueSet(T... values) {
|
2023-02-18 11:58:06 +08:00
|
|
|
for (T value : values) {
|
2023-04-15 02:47:11 +08:00
|
|
|
put(Objects.requireNonNull(value));
|
2023-02-18 11:58:06 +08:00
|
|
|
}
|
|
|
|
}
|
2023-04-15 02:47:11 +08:00
|
|
|
|
2023-04-15 03:59:04 +08:00
|
|
|
private void put(@Nonnull final T value) {
|
2023-04-15 02:47:11 +08:00
|
|
|
this.values.put(value.getId(), value);
|
2023-02-18 11:58:06 +08:00
|
|
|
}
|
2023-04-15 02:47:11 +08:00
|
|
|
|
2023-04-15 21:52:15 +08:00
|
|
|
@CheckForNull
|
2023-04-15 03:59:04 +08:00
|
|
|
public T get(final int id) {
|
2023-04-15 21:52:15 +08:00
|
|
|
return this.values.get(id);
|
2023-04-15 02:47:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<T> getValues() {
|
2023-04-15 21:52:15 +08:00
|
|
|
return this.values.values();
|
2023-02-18 11:58:06 +08:00
|
|
|
}
|
|
|
|
}
|
2022-11-07 17:49:27 +08:00
|
|
|
}
|