4

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.

falsarella
  • 12,217
  • 9
  • 69
  • 115
maaartinus
  • 44,714
  • 32
  • 161
  • 320

4 Answers4

5

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() {
    }
}
Mihai Toader
  • 12,041
  • 1
  • 29
  • 33
3

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!

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • Actually my case is a bit more complicated, but I always refer to previous elements only. Unfortunately, the StringBuilder can't be a static class member, otherwise this solution would be perfect. – maaartinus Feb 25 '11 at 00:04
1

One of the best ways would be using the enum polymorphism technique:

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

        @Override
        public String getS() {
            if (super.s == null) {
                String s = "";
                for (EE ee : values()) {
                    if (ee != ALL) {
                        s += " " + ee.s;
                    }
                }
            }
            return s;
        }
    },
    ;

    private String s;

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

    EE() {
        this.s = null;
    }

    public String getS() {
        return this.s;
    }
}

Test class:

public class TestEE {

    public static void main(String[] args) {
        for (EE ee : EE.values()) {
            System.out.println(ee.name() + ": " + ee.getS());
        }
    }
}

See also:

Community
  • 1
  • 1
falsarella
  • 12,217
  • 9
  • 69
  • 115
0

why can't you remove all?

consider using EnumSet, this will give you any combination of the EE's.

import java.util.*;
enum EE {
    A("anything"), B("beta"), Z("zulu");
    EE(String s) {
        this.s = s;
    }
    static Set<EE> all=EnumSet.allOf(EE.class);
    final String s;
}
Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
  • I need to get `ALL` with `ALL.s = "anything beta ... zulu"`, no EnumSet can help here. Moreover, I'm going to use `ALL` the same way as all the other members of `EE`. – maaartinus Feb 25 '11 at 01:24