Saturday 22 June 2019

java - How to condense large switch statements

I have a large switch like following:



public int procList(int prov, ArrayList txValueList, Context context)
{
switch(prov)
{
case Foo.PROV_ONE:
return proc_one(txValueList, context);


case Foo.PROV_NOE:
return proc_noe(txValueList, context);

case Foo.PROV_BAR:
return proc_bar(txValueList, context);

case Foo.PROV_CABAR:
return proc_cabar(txValueList, context);


case Foo.PROV_FAR:
return proc_far(txValueList, context);

case Foo.PROV_TAR:
return proc_tar(txValueList, context);

case Foo.PROV_LBI:
return 408;

default:

return -1;
}
}


In c++ I can use std::map and use it in manner as following:



map[prov](txValueList, context); 



There is not pointer to function in Java. However, it uses abstract classes like it is in the answer. So, is there a best way to eliminate huge switch clauses in java?

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...