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

shz.core.enums.IEnum Maven / Gradle / Ivy

There is a newer version: 2024.0.2
Show newest version
package shz.core.enums;

import shz.core.*;

import java.io.Serializable;
import java.util.*;
import java.util.function.Predicate;

public interface IEnum extends Serializable {
    String name();

    int ordinal();

    C getCode();

    V getValue();

    static > T get(Class cls, Predicate predicate) {
        List ts = EnumHelp.enumSet(cls);
        if (ts.isEmpty()) return null;
        return ts.stream().filter(predicate).findFirst().orElse(null);
    }

    static > List gets(Class cls, Predicate predicate) {
        List ts = EnumHelp.enumSet(cls);
        if (ts.isEmpty()) return Collections.emptyList();
        return ToList.explicitCollect(ts.stream().filter(predicate), ts.size());
    }

    static > T getByName(Class cls, String name) {
        return get(cls, e -> Objects.equals(name, e.name()));
    }

    static > List getByNames(Class cls, String... names) {
        if (NullHelp.isEmpty(names)) return Collections.emptyList();
        Set set = ToSet.explicitCollect(Arrays.stream(names).filter(Objects::nonNull), names.length);
        if (set.isEmpty()) return Collections.emptyList();
        return gets(cls, e -> set.contains(e.name()));
    }

    static > T getByCode(Class cls, Object code) {
        return get(cls, e -> Objects.equals(code, e.getCode()));
    }

    static > List getByCodes(Class cls, Object... codes) {
        if (NullHelp.isEmpty(codes)) return Collections.emptyList();
        Set set = ToSet.asSet(codes);
        return gets(cls, e -> set.contains(e.getCode()));
    }

    static > T getByValue(Class cls, Object value) {
        return get(cls, e -> Objects.equals(value, e.getValue()));
    }

    static > List getByValues(Class cls, Object... values) {
        if (NullHelp.isEmpty(values)) return Collections.emptyList();
        Set set = ToSet.asSet(values);
        return gets(cls, e -> set.contains(e.getValue()));
    }
}