Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access other enums in enum's constructor

I'd need something like the following

enum EE {
    A("anything"),
    B("beta"),
    ...
    Z("zulu"),
    ALL,
    ;

    EE(String s) {
        this.s = s;
    }
    EE() {
        String s = "";
        for (EE ee : values()) { // PROBLEM HERE
            if (ee != ALL) s += " " + ee.s;
        }
        this.s = s;
    }
}

While creating ALL I'd like to access the other members of the enum. The above doesn't work because of values() returning null at this point. Using A, B, ..., Z explicitly doesn't compile. I understand perfectly why this chicken-egg problem happens, but am looking for a nice workaround.

And no, removing ALL from EE is not an option.

like image 293
maaartinus Avatar asked Jan 21 '23 09:01

maaartinus


2 Answers

Would this work for you? :

enum EE {
    A("anything"),
    B("beta"),
    ...
    Z("zulu"),
    ALL,
    ;

    String s = null;

    EE(String s) {
        this.s = s;
    }

    EE() {
    }

    private void initS() {
        String s = "";
        for (EE ee : values()) { 
            if (ee != ALL) s += " " + ee.s;
        }  

        this.s = s;
    }

    public String getS() {
       if ( this.s == null )  { // assume we are ALL and initialize
         initS();
       }

       return this.s;
    }
}

Static initializer might be cleaner.

public enum EE {
    A("anything"),
    B("beta"),
    Z("zulu"),
    ALL
    ;

    static {
        String s = "";
        for (EE ee : values()) {
            if ( ee != ALL ) s += ee + " ";
        }

        ALL.s = s.trim();
    }

    String s = null;

    EE(String s) {
        this.s = s;
    }

    EE() {
    }
}
like image 148
Mihai Toader Avatar answered Feb 05 '23 04:02

Mihai Toader


You could have a static StringBuilder that each enum constant appends its value to in the constructor, then in your default constructor just set the enum's string to the value of the StringBuilder.

Note however that Java will prevent you accessing static variables directly from an enum's constructor (for good reason!) so you'll have to do the dirty work in another method and then call that from the constructor.

For this to work though you'll have to make sure ALL is declared last.

As a disclaimer though this is a truly horrible workaround and if you can at all, I'd encourage you to explore other possibilities here!

like image 37
Michael Berry Avatar answered Feb 05 '23 02:02

Michael Berry