Iterate enum values using java generics

前端 未结 10 1031
余生分开走
余生分开走 2020-12-02 10:55

I\'m trying to find a way to iterate through an enum\'s values while using generics. Not sure how to do this or if it is possible.

The following code illustrates

10条回答
  •  情话喂你
    2020-12-02 11:39

    Here below an example of a wrapper class around an Enum. Is a little bit weird bu is what i need :

    public class W2UIEnum & Resumable> {
    
    public String id;
    public String caption;
    
    public W2UIEnum(ApplicationContext appContext, T t) {
        this.id = t.getResume();
        this.caption = I18N.singleInstance.getI18nString(t.name(), "enum_"
                + t.getClass().getSimpleName().substring(0, 1).toLowerCase()
                + t.getClass().getSimpleName().substring(1,
                        t.getClass().getSimpleName().length()), appContext
                .getLocale());
    }
    
    public static  & Resumable> List> values(
            ApplicationContext appContext, Class enumType) {
        List> statusList = new ArrayList>();
        for (T status : enumType.getEnumConstants()) {
            statusList.add(new W2UIEnum(appContext, status));
        }
        return statusList;
    }
    

    }

提交回复
热议问题