最高序数枚举值 [英] Highest ordinal enum value

查看:73
本文介绍了最高序数枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望从bean列表中的枚举属性列表中计算出最高序数枚举值.

I'm looking to compute the highest ordinal enum value from a list of enum properties in a list of beans.

例如,我有:

@Data
public class MyBean {
    private Priority priority;
}

public enum Priority {
    URGENT("Urgent"),
    HIGH("High"),
    MEDIUM("Medium"),
    LOW("Low");

    @Getter
    private final String value;

    Priority(String value) {

        this.value = value;
    }

    @Override
    public String toString() {

        return getValue();
    }
}

如果我的ListMyBeans,如何在列表中找到该豆的priority的最大序数值?

If I have a Listof MyBeans, how can I find the max ordinal value of the bean's priority in the list?

示例:

   {myBean1, myBean2, myBean3, myBean4} where

    myBean1.getPriority() = Priority.LOW
    myBean2.getPriority() = Priority.URGENT
    myBean3.getPriority() = Priority.HIGH
    myBean4.getPriority() = null

    returns Priority.URGENT

我认为最坏的情况是我可以在以Collections.min(Arrays.asList(Priority.values()));开头的枚举中迭代values(),并循环遍历每个bean以查看值是否匹配.但这似乎很乏味.

I'm thinking the worst case is that I could iterate the values() in the enum starting with Collections.min(Arrays.asList(Priority.values())); and loop through each bean to see whether the value matches. But this seems tedious.

推荐答案

我将为每个优先级指定一个特定的数值,并添加一个可以比较它们的方法.不幸的是,枚举无法实现Comparable(为了保持一致性),这在这里实在是太可惜了.

I would give each priority a specific numeric value and add a method which can compare them. Unfortunately enums can't implement Comparable (for consistency) which is a bit of a bummer here.

您将返回空的前哨值这一事实使事情变得有些复杂.如果我是你,我会重新考虑的.考虑使用默认"优先级,如果缺少它,它可以作为我们的优先级.

The fact that you are returning null sentinel values complicates things slightly. I would rethink this if I were you. Consider a "Default" priority instead which can act as our priority if one is missing.

我添加了默认优先级作为一个完全唯一的选项,但是根据您的需要,您可以只使用中级或低级作为默认值.

I've added the default priority as a totally unique option, but depending on what you want you could just use medium or low as the default.

public enum Priority {
    URGENT ("Urgent", 10),
    HIGH   ("High",    5),
    MEDIUM ("Medium",  2),
    LOW    ("Low",     0),
    DEFAULT("Default",-1);

    @Getter
    private final String name;

    private final int value;

    Priority(String name, int value) {
        this.name  = name;
        this.value = value;
    }

    @Override
    public String toString() {
        return getName();
    }

    public int compare(Priority that) {
        return Integer.compare(this.value, that.value);
    }
}

然后您将需要更改优先级获取器以返回此新默认值,而不是null,否则稍后我们将获得空指针异常.

You will then need to change your priority getter to return this new default rather than null, else we'll get null pointer exceptions later on.

public class MyBean {
    private Priority priority;

    // Never returns null :)
    public Priority getPriority() {
        return (priority != null) ? priority : Priority.DEFAULT;
    }
}

现在我们已经完成了艰巨"的工作,获得最高优先级是非常容易的:

Now we've done the "hard" work, getting the highest priority is super easy:

Priority max = Collections.max(beans, (a,b) ->
    a.getPriority().compare(b.getPriority())
).getPriority();

这篇关于最高序数枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆