I have a complex strings like the following:
$company=>Company(code->MyCompany)
$brand=>Brand(code->Brand_1, company->$company)
and then I prepare objects out of them as follows:
Macro [key=$company, type=Company, map={code=MyCompany}]
Macro [key=$brand, type=Brand, map={ company=$company, code=Brand_1}]
using the following code for parsing. Is there a better way?
public class Macro {
private String key;
private String type;
private Map<String, String> map;
}
public static Macro getMacro(String str) {
final Macro macro = new Macro();
final Map<String, String> map = new HashMap<String, String>();
final Matcher matcher = Pattern.compile("\\(([^)]+)\\)").matcher(str);
while (matcher.find()) {
final List<String> list = Arrays.asList(matcher.group(1).split(",")).stream()
.collect(Collectors.toList());
list.stream().forEach(a ->
{
final String[] ar = a.split("->");
map.put(ar[0], ar[1]);
});
}
str = str.replaceAll("\\(.*\\)", "");
final String[] s = str.split("=>");
macro.setMap(map);
macro.setKey(s[0]);
macro.setType(s[1]);
return macro;
}