
cn.taketoday.lang.Enumerable Maven / Gradle / Ivy
/*
* Original Author -> Harry Yang ([email protected]) https://taketoday.cn
* Copyright © TODAY & 2017 - 2022 All Rights Reserved.
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see [http://www.gnu.org/licenses/]
*/
package cn.taketoday.lang;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
/**
* Enumerable for {@link Enum}
*
* @author Harry Yang
* @since 4.0
*/
public interface Enumerable extends Descriptive {
@SuppressWarnings("unchecked")
default V getValue() {
return (V) name();
}
@Override
default String getDescription() {
return name();
}
/**
* The default name of the enumeration, this method does not need
* to be implemented, the enumeration class is automatically inherited
*/
String name();
/**
* Returns Enumerable by {@link Enumerable#getValue() enum value}
*
* @param enumerable enum
* @param value enumeration value
* @param enumeration type
* @param enumeration value type
* @return enumeration instance
* @throws NullPointerException if enumerable is {@code null}
* @see Enumerable#getValue()
*/
@Nullable
static , V> T of(Class enumerable, @Nullable V value) {
if (value != null) {
T[] enumConstants = enumerable.getEnumConstants();
if (enumConstants != null) {
for (T constant : enumConstants) {
if (Objects.equals(value, constant.getValue())) {
return constant;
}
}
}
}
return null;
}
/**
* Get the value corresponding to the name
*
* @param enumerable enumeration class
* @param name enumeration name
* @param enum type
* @param enumeration value type
* @return enumeration value
* @see Enumerable#getValue()
*/
@Nullable
static , V> V getValue(Class enumerable, String name) {
T[] enumConstants = enumerable.getEnumConstants();
if (enumConstants != null) {
for (T constant : enumConstants) {
if (Objects.equals(name, constant.name())) {
return constant.getValue();
}
}
}
return null;
}
/**
* @param enum type
* @param enumeration value type
* @param defaultValue default value
* @see #of(Class, V)
*/
static , V> T of(Class enumerable, V value, Supplier defaultValue) {
return find(enumerable, value).orElseGet(defaultValue);
}
/**
* @param defaultValue default value
* @param enum type
* @param enumeration value type
* @see #of(Class, V)
*/
static , V> T of(Class enumerable, V value, T defaultValue) {
return find(enumerable, value).orElse(defaultValue);
}
/**
* @return Optional of T
*/
static , V> Optional find(Class enumerable, V value) {
return Optional.ofNullable(of(enumerable, value));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy