All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.rx.bean.NEnum Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package org.rx.bean;

import lombok.NonNull;
import lombok.SneakyThrows;
import org.rx.core.Extends;
import org.rx.exception.InvalidException;

import java.io.Serializable;

import static org.rx.core.Constants.NON_UNCHECKED;

public interface NEnum & NEnum> extends Serializable {
    static  & NEnum> T valueOf(@NonNull Class type, int value) {
        return valueOf(type, value, true);
    }

    static  & NEnum> T valueOf(@NonNull Class type, int value, boolean throwOnEmpty) {
        for (T nEnum : type.getEnumConstants()) {
            if (nEnum.getValue() == value) {
                return nEnum;
            }
        }
        if (throwOnEmpty) {
            throw new InvalidException("Enum {} not contains {}", type, value);
        }
        return null;
    }

    int getValue();

    default FlagsEnum flags() {
        return new FlagsEnum<>(this);
    }

    @SuppressWarnings(NON_UNCHECKED)
    default FlagsEnum flags(T... nEnum) {
        FlagsEnum flagsEnum = flags();
        flagsEnum.add(nEnum);
        return flagsEnum;
    }

    @SuppressWarnings(NON_UNCHECKED)
    @SneakyThrows
    default String description() {
        Class type = this.getClass();
        return Extends.metadata(type.getField(((T) this).name()));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy