@Test void parseSemantics() { List<Map<String, Object>> params = Arrays.asList( named("a", 1), named("b",2), named("c", 3) ); List<Map<String, Object>> first = Collections.singletonList( named("a", 1) ); List<Map<String, Object>> mid = Collections.singletonList( named("b", 2) ); assertEquals(first, SemanticUtils.parseSemantics("@first").apply(params)); assertEquals(mid, SemanticUtils.parseSemantics("@1").apply(params)); }
public static SemanticFunction parseSemantics(String semantics){ SemanticFunction fn = null; semantics = semantics.trim(); if(semantics.startsWith("@")) { switch (semantics.toLowerCase()) { case _IDENTITY: fn = SemanticUtils::identity; break; case _FIRST: fn = SemanticUtils::first; break; case _LAST: fn = SemanticUtils::last; break; case _MERGE: fn = SemanticUtils::merge; break; default: Matcher m = PATTERN_PARAM.matcher(semantics); if (m.matches()) { int index = Integer.valueOf(m.group(1)); fn = params -> Collections.singletonList(params.get(index)); } break; } }else if(semantics.startsWith("{")){ final Map<String, Object> template = GSON.fromJson( semantics, new TypeToken<HashMap<String, Object>>() { }.getType() ); fn = parseTemplate(template); }else { fn = valueFn(semantics); } return fn; }
SemanticUtils { public static SemanticFunction parseSemantics(String semantics){ SemanticFunction fn = null; semantics = semantics.trim(); if(semantics.startsWith("@")) { switch (semantics.toLowerCase()) { case _IDENTITY: fn = SemanticUtils::identity; break; case _FIRST: fn = SemanticUtils::first; break; case _LAST: fn = SemanticUtils::last; break; case _MERGE: fn = SemanticUtils::merge; break; default: Matcher m = PATTERN_PARAM.matcher(semantics); if (m.matches()) { int index = Integer.valueOf(m.group(1)); fn = params -> Collections.singletonList(params.get(index)); } break; } }else if(semantics.startsWith("{")){ final Map<String, Object> template = GSON.fromJson( semantics, new TypeToken<HashMap<String, Object>>() { }.getType() ); fn = parseTemplate(template); }else { fn = valueFn(semantics); } return fn; } }
SemanticUtils { public static SemanticFunction parseSemantics(String semantics){ SemanticFunction fn = null; semantics = semantics.trim(); if(semantics.startsWith("@")) { switch (semantics.toLowerCase()) { case _IDENTITY: fn = SemanticUtils::identity; break; case _FIRST: fn = SemanticUtils::first; break; case _LAST: fn = SemanticUtils::last; break; case _MERGE: fn = SemanticUtils::merge; break; default: Matcher m = PATTERN_PARAM.matcher(semantics); if (m.matches()) { int index = Integer.valueOf(m.group(1)); fn = params -> Collections.singletonList(params.get(index)); } break; } }else if(semantics.startsWith("{")){ final Map<String, Object> template = GSON.fromJson( semantics, new TypeToken<HashMap<String, Object>>() { }.getType() ); fn = parseTemplate(template); }else { fn = valueFn(semantics); } return fn; }  }
SemanticUtils { public static SemanticFunction parseSemantics(String semantics){ SemanticFunction fn = null; semantics = semantics.trim(); if(semantics.startsWith("@")) { switch (semantics.toLowerCase()) { case _IDENTITY: fn = SemanticUtils::identity; break; case _FIRST: fn = SemanticUtils::first; break; case _LAST: fn = SemanticUtils::last; break; case _MERGE: fn = SemanticUtils::merge; break; default: Matcher m = PATTERN_PARAM.matcher(semantics); if (m.matches()) { int index = Integer.valueOf(m.group(1)); fn = params -> Collections.singletonList(params.get(index)); } break; } }else if(semantics.startsWith("{")){ final Map<String, Object> template = GSON.fromJson( semantics, new TypeToken<HashMap<String, Object>>() { }.getType() ); fn = parseTemplate(template); }else { fn = valueFn(semantics); } return fn; }  static String _N(int item); static String _N_LONG(int item); static String _N_DOUBLE(int item); static ChainMap<String, Object> named(String name, Object value); static ChainMap<String, Object> value(Object value); static SemanticFunction valueFn(Object value); static List<Map<String, Object>> identity(List<Map<String, Object>> params); static List<Map<String, Object>> merge(List<Map<String, Object>> params); static List<Map<String, Object>> first(List<Map<String, Object>> params); static List<Map<String, Object>> last(List<Map<String, Object>> params); static SemanticFunction parseSemantics(String semantics); static SemanticFunction parseTemplate(Map<String, Object> template); }
SemanticUtils { public static SemanticFunction parseSemantics(String semantics){ SemanticFunction fn = null; semantics = semantics.trim(); if(semantics.startsWith("@")) { switch (semantics.toLowerCase()) { case _IDENTITY: fn = SemanticUtils::identity; break; case _FIRST: fn = SemanticUtils::first; break; case _LAST: fn = SemanticUtils::last; break; case _MERGE: fn = SemanticUtils::merge; break; default: Matcher m = PATTERN_PARAM.matcher(semantics); if (m.matches()) { int index = Integer.valueOf(m.group(1)); fn = params -> Collections.singletonList(params.get(index)); } break; } }else if(semantics.startsWith("{")){ final Map<String, Object> template = GSON.fromJson( semantics, new TypeToken<HashMap<String, Object>>() { }.getType() ); fn = parseTemplate(template); }else { fn = valueFn(semantics); } return fn; }  static String _N(int item); static String _N_LONG(int item); static String _N_DOUBLE(int item); static ChainMap<String, Object> named(String name, Object value); static ChainMap<String, Object> value(Object value); static SemanticFunction valueFn(Object value); static List<Map<String, Object>> identity(List<Map<String, Object>> params); static List<Map<String, Object>> merge(List<Map<String, Object>> params); static List<Map<String, Object>> first(List<Map<String, Object>> params); static List<Map<String, Object>> last(List<Map<String, Object>> params); static SemanticFunction parseSemantics(String semantics); static SemanticFunction parseTemplate(Map<String, Object> template); final static String _IDENTITY; final static String _FIRST; final static String _LAST; final static String _MERGE; final static String _APPEND; final static String KEY_UNNAMED; }