java - String to String mapping using Spring? -
from external system receive string representation of abbreviations , have make transformation(conversion) string example:
"o" -> open "c" -> closed "e" -> exit
for object object conversion using spring custom converter
import org.springframework.core.convert.converter.converter; public class converter<source, target> implements converter<source, target> public final target convert(@nonnull source source) { ... }
but can't create string string converter. not want use external mapping library spring capabilities. can't this. simplest thing can switch
string input = "o"; string result = null; switch(input){ case "o": result ="open" break; case "c": result ="close" break; ....
in matter of fact have on 100 mapings. can spring offer better solution?
when don't have logic execute in switch-case, can use static hashmap<string,string>
static hashmap<string,string> map = new hashmap<>(); static { map.put("o","open"); map.put("c","close"); ..................... }
instead of switch-case use
map.get(input);
if suing java 8, can use
map.getordefault(input,"");
Comments
Post a Comment