0

My Enum

public enum ExamStausEnum {

    RESULTAWAITED("Result Awaiting"), 
    PASSED("Passed"), 
    FAILED("Failed");

    private String value;

    ExamStausEnum(String value) {
        this.value = value;
    }

    @JsonValue
    public String getValue() {
        return value;
    }
}

Generic Iterator Enum

static <E extends Enum <E>> void foo(Class<E> elemType) {
    for (E e : java.util.EnumSet.allOf(elemType)) {
        System.out.println(e);
    }
}

Result :

RESULTAWAITED
PASSED
FAILED

How can I print the constructor values ?

Result Awaiting
Passed
Failed
6
  • You mean System.out.println(e.getValue())? Commented Oct 11, 2017 at 11:29
  • @khelwood, but this is generic for all enums, and getValue is for this enum called ExamStausEnum Commented Oct 11, 2017 at 11:30
  • 1
    Can you override toString() on your enum? Commented Oct 11, 2017 at 11:31
  • you cannot do it generically. If you have a couple enums with value field, you can get that field using reflection. Commented Oct 11, 2017 at 11:31
  • @khelwood yes ! want same result Commented Oct 11, 2017 at 11:32

2 Answers 2

6

It’s unavoidable to add another parameter to have an abstraction of the getValue() call:

static <E extends Enum <E>> void foo(Class<E> elemType, Function<? super E, ?> f) {
    for(E e : java.util.EnumSet.allOf(elemType)) {
        System.out.println(f.apply(e));
    }
}

Then, you may it invoke for arbitrary enum types not necessarily having that method, e.g.

foo(Thread.State.class, Object::toString);

or for your specific enum having the method:

foo(ExamStausEnum.class, ExamStausEnum::getValue);

Even more use cases are possible:

foo(Thread.State.class, Enum::name);

or

foo(ExamStausEnum.class, Enum::ordinal);

Of course, you may also let your ExamStausEnum type override the toString() method, eliminating the need for foo to call the getValue() method.

Sign up to request clarification or add additional context in comments.

Comments

1

the more foreward way is to create an interface declaring the getValue() method implemented by all your enums.

interface EnumWithValue{
   @JsonValue
   String getValue();
}

public enum ExamStausEnum implements EnumWithValue {

    RESULTAWAITED("Result Awaiting"), 
    PASSED("Passed"), 
    FAILED("Failed");

    private String value;

    ExamStausEnum(String value) {
        this.value = value;
    }

    @Override
    public String getValue() {
        return value;
    }
}

then you can cast your enum in foo:

static <E extends Enum <E>> void foo(Class<E> elemType) {
    for (E e : java.util.EnumSet.allOf(elemType)) {
        System.out.println(((EnumWithValue)e).getValue());
    }
}

3 Comments

I’d rather change the declaration to <E extends Enum<E>&EnumWithValue> void foo(Class<E> elemType) instead of inserting a type cast that can fail at runtime.
I think your suggestion will also fail at runtime in the same situation...
When you use the declaration <E extends Enum<E>&EnumWithValue>, the compiler will already reject Class objects of enums not implementing EnumWithValue. With your code, you will not even get a warning when passing a wrong type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.