Java program uses the following construction: (provided mainly for context)
public class Utils {
public static final byte PROVIDER_FOO = 0;
public static final byte PROVIDER_MOO = 1;
public static final byte PROVIDER_BAR = 2;
public static final byte PROVIDER_FUR = 3;
public static final byte PROVIDER_BOO = 4;
public static final byte PROVIDER_ANOTHER_FOO = 5;
public static final byte PROVIDER_YET_ANOTHER = 6;
public static String [] PROVIDERS = {"FOO", "MOO", "BAR", "FUR", "BOO",
"PROVIDER_ANOTHER_FOO", "PROVIDER_YET_ANOTHER"};
...
}
So, each element of array has a number. And each array index in range 0..6
has text representation. Text representation is using in outer world: reading configs, printing results. Number representation is using in internal tasks. So, there a problem to transform text representation to number and vice versa. Number is transferring to string as following:
// someObject.getProviderNumeric() returns numeric value for someObj's provider
String strProvider = Utils.PROVIDERS[someObj.getProviderNumeric()]
String is transferring to number like this:
int getNumericByText(String strProvider) {
for (int i = 0; i < Utils.PROVIDER_YET_ANOTHER; ++i) {
if (strProvider.equals(Utils.PROVIDERS[i])) {
return i;
}
return -1;
}
It looks pretty bulky. What is the best way to rewrite?