How to convert one enum to another enum in java?

Personally I would just create a Map<Detailed, Simple> and do it explicitly – or even use a switch statement, potentially.

Another alternative would be to pass the mapping into the constructor – you could only do it one way round, of course:

public enum Detailed {
    PASSED(Simple.DONE),
    INPROCESS(Simple.RUNNING),
    ERROR1(Simple.ERROR),
    ERROR2(Simple.ERROR),
    ERROR3(Simple.ERROR);

    private final Simple simple;

    private Detailed(Simple simple) {
        this.simple = simple;
    }

    public Simple toSimple() {
        return simple;
    }
}

(I find this simpler than Ted’s approach of using polymorphism, as we’re not really trying to provide different behaviour – just a different simple mapping.)

While you could potentially do something cunning with the ordinal value, it would be much less obvious, and take more code – I don’t think there’d be any benefit.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)