4 Replies Latest reply: Jul 13, 2010 1:10 PM by 800387 RSS

    What is the best way to organize a tree-like structure of constants?

    843853
      Hello everone!

      Need help with organizing my constants structrure.

      I have several tables, and each of them need a list of column names to define the fields returned after query, and also a corresponding list of hashmap keys because that columnnames are ugly. Hashmap is where I store the result of searching through the table. This question is not about JDBC because I search through tables via COM interface, no JDBC ResultSets and other stuff here.

      Finally, for each table I have two constant lists and I have a list of tables. What is the best way to store this?

      My first idea was to create a enum and store column data in it as String array attributes: String[] columnNames; etc.
      But in this case I cannot use each column and key separately.

      Another option is to create two separate enums for each table: columnNames enum and keys enum. That doesn't look great also.

      The third option is to create inner class for each table and store two enums in each of that classes.

      Also I can store all data in hashmaps, Strings etc.

      (1) Finally, from your experience, what is the best way to organize all that stuff?

      (2) I have heard that smart Java programmer should avoid using the enums by any means. Do you agree?

      (3) Generally what will you prefer when creating a constant which has two values: two final Integers or enum?

      Edited by: Dmitry_MSK on Jul 8, 2010 5:22 AM
        • 1. Re: What is the best way to organize a tree-like structure of constants?
          800387
          I'm not sure why you don't just invent a generic data structure (e.g., table name, list of column names and aliases of column names) such as:
          class QueryMetaData {
            private final String tableName;
            private final String[] columnNames;
            private final String[] columnAliases;
          }
          Read into the above structure from a properties file, database table, etc. You can store meta-data itself in places other than enum constants, particularly if would like to change the meta-data without requiring a new build of your application.

          That having been said, WRT to your specific questions:
          (1) Finally, from your experience, what is the best way to organize all that stuff?
          See above
          (2) I have heard that smart Java programmer should avoid using the enums by any means. Do you agree?
          Enums are better than simple constants using int or String or something similar. If there are known, discrete values unlikely to change frequently, I see no issues with an enum. They improve the readability of code, and there are enough syntactic sugar features in the language (switch statements come to mind) to make them appealing.

          (3) Generally what will you prefer when creating a constant which has two values: two final Integers or enum?

          See above. Enums were introduced (in large part) to do away with storing constants as integers.

          - Saish
          • 2. Re: What is the best way to organize a tree-like structure of constants?
            791266
            >
            (2) I have heard that smart Java programmer should avoid using the enums by any means. Do you agree?
            Where did you hear that? I don't agree. It's better to have something that is typesafe than a set if ints.
            (3) Generally what will you prefer when creating a constant which has two values: two final Integers or enum?
            enum
            • 3. Re: What is the best way to organize a tree-like structure of constants?
              843853
              Guys, this helps! Thank you for the replies.
              • 4. Re: What is the best way to organize a tree-like structure of constants?
                800387
                You are welcome. Best of luck.

                - Saish