Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EnumSet for pre-1.5 fake enums?

Recently I've been doing a lot of these

enum Thing {

    /* etc etc */

    static final Set<Thing> allThings = EnumSet.allOf(Thing.class);

}

I want something similar in pre 1.5 Java, i.e. I want something like:

final class Thing {

    private Thing(); // control instances within class

    static final Thing instance0 = new Thing();
    static final Thing instance1 = new Thing();

    static final Set allThings = // ?????
}

How should I do this?

like image 398
jjujuma Avatar asked Mar 01 '23 20:03

jjujuma


1 Answers

What about that:

final class Thing {

  static final Set allThings = new HashSet();

  private Thing() {
      allThings.add(this);
  } // control instances within class

  static final Thing instance0 = new Thing();
  static final Thing instance1 = new Thing();
}
like image 148
pgras Avatar answered Mar 12 '23 05:03

pgras