Reputation: 3736
I have a java application which uses a class "Country". The business logic associates each country in the world with:
Normally, this is data is perfect for a database. However, given the fact that this data is quite stable (sure, it changes now and then, but it is normally not used to perform create/update/delete operations on those records) I was thinking about maybe putting the information in an enumeration like this:
public enum Country
{
US(1,new String[]{"America\New York","America\California",...}),
...
NL(31, new String[]{"Europe\Amsterdam"});
...
public Country(int telephoneCode, String[] timezoneIds)
{
...
}
}
Possible advantages to this approach:
The disadvantages could be:
I would like to ask about the bolded question (disadvantage #3), but any comments regarding the bigger picture are welcome.
I am aware of the Locale class in java which can be used to get a list of all possible countries known in the java system. Unfortunately, telephone and timezone information is not available. My intent is too merge this data into one enumeration. Is this practice considered to be a plausible alternative or hackity anti pattern?
Upvotes: 4
Views: 829
Reputation: 29959
I can only agree with the comments so far. A list of countries may change but it will always be small enough to keep the whole list in memory. Any difference in performance doesn't really matter if it is only queried once and then cached.
It probably depends on who is using the software (personal use or a large company) but I would say the biggest concern here is #2, not #3. In code vs. in database should be decided based on who might has to change the values later. Are you really ok that you need a developer to change a value, recompile everything and roll out a new version or patch of the software? Just updating a text file or the database will be much easier.
If it's a personal project that argument probably doesn't matter for you, but even then a text file (e.g. in CSV format) might be the easier to maintain than code. I would still recommend to use a simple database - just for the learning experience. You don't need to setup a huge database system like MySQL. You could use a small embeddable database like h2 or Apache Derby.
Just for reference how often country codes and names can change:
ISO 3611-1 (which defines the language and country codes for existing countries) has been changed 17 times in the years 2007-2014. That's about 2.4 times per year.
Upvotes: 5
Reputation: 7717
No problem to create an Enum: generally speaking it is infrequent a change of Countries.
My suggestions:
Upvotes: 1