Java enumeration performance

I was thinking about using the type enumto control i18n in a Java game that I am developing, but I was interested to learn about performance problems that can arise when working with enumerations that have many elements (I think thousands).

I'm actually trying something like:

public enum Text {
  STRING1,
  STRING2,
  STRING3;

  public String text() {
    return text;
  }

  public String setText() {
    this.text = text;
  }
}

Then, to load them, I can simply fill in the fields:

static
{
  Text.STRING1.setText("My localized string1"); 
  Text.STRING2.setText("My localized string2"); 
  Text.STRING3.setText("My localized string3"); 
}

Of course, when I need to manage many languages, I will load them from a file.

What I ask

  • - the selected object (in addition to the line) for each element? (I think yes, since enumerations are implemented with objects)
  • ? ? ( , Text.STRING1.text()). , , , .
  • , - ?

+5
6

ResourceBundle:

public enum Text {
  YELL, SWEAR, BEG, GREET /* and more */ ;

  /** Resources for the default locale */
  private static final ResourceBundle res =
    ResourceBundle.getBundle("com.example.Messages");

  /** @return the locale-dependent message */
  public String toString() {
    return res.getString(name() + ".string");
  }
}

# File com/example/Messages.properties
# default language (english) resources
YELL.string=HEY!
SWEAR.string=§$%&
BEG.string=Pleeeeeease!
GREET.string=Hello player!

# File com/example/Messages_de.properties
# german language resources
YELL.string=HEY!
SWEAR.string=%&$§
BEG.string=Biiiiiitte!
GREET.string=Hallo Spieler!
+13

, java.util.ResourceBundle. .

:

  • , .
  • , Enum.
  • . / enum , , . . , , .
+11

, , enums i18n, . Java i18n, tutorial.

+7

java i18n ResourceBundle. , . , . enum Texts, . .

getText() :

return ResourceBundle.getBundle("texts").getString(name());

, . .

. unit test, , , .

, , . !

+2

, . , :

public static void main(String[] args) throws Exception {
    PrintWriter w = new PrintWriter("C:\\test.java");
    w.println("enum Test {");
    for (int i = 0; i < 3000; i++) {
        w.println("c" + i + ",");
    }
    w.println("}");
    w.close();
}

65535

2000 .

, , .

, ( ) . 2000 , 16 :-) ( 32- Sun, )

, text. , , . , .

, , . , .

:

ResourceBundle, AlexR. : . , UnitTest // , () .

, :

enum Message {
    Hello("Hello", "Hallo", "Salut");

    String en;
    String de;
    String fr;

    Message(String en, String de, String fr) {
        this.en = en;
        this.fr = fr;
        this.it = it;
    }

: ( ), (unicode escapes ...). , , 3 4 .

: / , , " " .

, MessageFormat, R.Bemrose .

, , Enums values ​​():

for (Text t : Text.values()) {

}
+1

, I18n, , . , . . 90% + , , .

public enum Text {
  STRING1("String one"),
  STRING2("String two"),
  STRING3("String two");

  private final String text;
  private Text(String text) { this.text = text; }
}

In terms of performance of listing creation, you should not worry about this for the game, clarify and be flexible in the first place. 1000 transitions can add 1 ms to the launch time of your application. cf Loading text from a file is likely to add 10 ms.

0
source

All Articles