反向查找Java枚举,每个键/常量有多个值? [英] Reverse lookup for Java Enums with more than one value per key/constant?

查看:207
本文介绍了反向查找Java枚举,每个键/常量有多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  ABBR1(long text 1,another description 1,另一个1),
ABBR2(long text 2,another description 2,another another 2),
//等等...

如何通过调用如 getAbbreviation之类的方法来反向查找缩写(常量) (descriptionText)



我基本上正在按照这里,我认为,不同之处在于,每个ENUM键(常量)都有几个值,而且我希望它与 getAbbreviation(long文本1)以及 getAbbreviation(另一个2) ...



有没有一个简单的方法来循环每个ENUM(即 ABBRn 的)值字段,填充一个巨大的地图,或者甚至可能更好解决方案?

决方案

这依赖于枚举成员构造函数静态初始化之前运行的事实。然后,初始化程序缓存成员及其长形式。

  import java.util.Arrays; 
import java.util.HashMap;
import java.util.Map;

public enum缩写{
ABBR1(long text 1,another description 1,another one 1),
ABBR2(long text 2 另一个描述2,另一个2);

private static final Map< String,Abbreviation> ABBREVIATIONS = new HashMap<>();

private String [] longForms;
private缩写(String ... longForms){
this.longForms = longForms;
}

public String toString(){
return Arrays.toString(longForms);
}

static {
for(缩写abbr:values()){
for(String longForm:abbr.longForms){
ABBREVIATIONS.put (longForm,abbr);
}

}
}

public static(String longForm)的缩写{
缩写缩写= ABBREVIATIONS.get(longForm);
if(缩写== null)throw new IllegalArgumentException(longForm +不能缩写);
返回缩写; b


public static void main(String [] args){
缩写a =缩写词;
System.out.println(a == Abbreviation.ABBR2); // true
}
}


With an enum like this one where each key has several values

ABBR1("long text 1", "another description 1", "yet another one 1"), 
ABBR2("long text 2", "another description 2", "yet another one 2"), 
//and so on...

how can I reverse lookup an abbreviation (constant) by calling a method like getAbbreviation(descriptionText)?

I'm basically looking for an implementation as described here, I think, but with the difference that each ENUM key (constant) has several values coming with it, and I want it to work with getAbbreviation("long text 1") as well as with getAbbreviation("yet another one 2")...

Is there an easy way to loop over each ENUM's (i.e. ABBRn's) value fields, to populate a giant map, or is there maybe even a better solution?

解决方案

This relies on the fact that the enum member constructors are run before the static initializer. The initializer then caches the members and their long forms.

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public enum Abbreviation {
    ABBR1("long text 1", "another description 1", "yet another one 1"), 
    ABBR2("long text 2", "another description 2", "yet another one 2"); 

    private static final Map<String, Abbreviation> ABBREVIATIONS = new HashMap<>();

    private String[] longForms;
    private Abbreviation(String... longForms) {
        this.longForms = longForms;
    }

    public String toString () {
        return Arrays.toString(longForms);
    }

    static {
        for(Abbreviation abbr : values()) {
            for(String longForm : abbr.longForms) {
                ABBREVIATIONS.put(longForm, abbr);
            }

        }
    }

    public static Abbreviation of(String longForm) {
        Abbreviation abbreviation = ABBREVIATIONS.get(longForm);
        if(abbreviation == null) throw new IllegalArgumentException(longForm + " cannot be abbreviated");
        return abbreviation;
    }



    public static void main(String[] args) {
        Abbreviation a =  Abbreviation.of("yet another one 2");
        System.out.println(a == Abbreviation.ABBR2); //true
    }
}

这篇关于反向查找Java枚举,每个键/常量有多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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