Re: Recording an ISO-639-1 language code
Haggis McMutton wrote:
I want to make a class where a variable stores an ISO-639-1 language code.
I'm not sure whether to store this is as string (eg. 'EN', 'ES', 'FR',
etc.) or an 8-bit int where -128 would be 'AA', -127 would be 'AB', etc.
Only that would give you 26*26 possibilities, which wouldn't fit in an
8-bit integer. Valid country codes would fir in one byte, at the moment.
How many million of these are you going to have to make it worth packing?
It seems to me that an 8-bit integer would be better programming. But to
do this I'd have to have a way to convert this 8-bit int into a language
code and back again when it's been entered by a human or given to a human
as it seems bad to me to expect a human to understand my own internal
language code system.
"Better programming" almost always means clearer programming. Having
individual bytes represent country codes probably isn't going to help.
Having Strings for everything similarly isn't particularly helpful. You
might want to think about a country code (or just country) class. You
can share instances, so if you have lots of references to country codes
you are using 4 or 8 bytes per reference (on a machine with perhaps over
1,000,000,000 bytes available).
import java.util.Map;
public class ISOCountry {
private static final Map<String,ISOCountry> instances;
static {
instances = java.util.HashMap<String,ISOCountry>();
for (String code : java.util.Local.getISOCountries()) {
instances.put(code, new ISOCountry(code));
}
}
public static ISOCountry valueOf(String code) {
ISOCountry country = instances.get(code);
if (country == null) {
throw new IllegalArgumentException();
} else {
return country;
}
}
private final String code;
private ISOCountry(String code) {
this.code = code;
}
public String getISOCountry() {
return code;
}
}
(Disclaimer: Code not tested or even compiled.)
Tom Hawtin