StringTemplateFunction implements Function { public Object evaluate(Object object) { return evaluate(object, Object.class); } StringTemplateFunction();  StringTemplateFunction(List<Expression> parameters, Literal fallback); String getName(); FunctionName getFunctionName(); List<Expression> getParameters(); Object accept(ExpressionVisitor visitor, Object extraData); Object evaluate(Object object); T evaluate(Object object, Class<T> context); Literal getFallbackValue(); static final FunctionName NAME; }
@Test public void testConcatenate() { parameters.add(ff2.literal("123")); parameters.add(ff2.literal(".*")); parameters.add(ff2.literal("start${0}end")); Function fn = finder.findFunction("stringTemplate", parameters, fallback); Object result = fn.evaluate(null); assertEquals("start123end", result); }
@Test public void testCapturingGroups() { parameters.add(ff2.literal("abc123_678")); parameters.add(ff2.literal(".*(\\d{3})_(\\d{3})")); parameters.add(ff2.literal("first${1}_second${2}_full${0}")); Function fn = finder.findFunction("stringTemplate", parameters, fallback); Object result = fn.evaluate(null); assertEquals("first123_second678_fullabc123_678", result); }
@Test public void testFallback() { parameters.add(ff2.literal("abc12_67")); parameters.add(ff2.literal(".*(\\d{3})_(\\d{3})")); parameters.add(ff2.literal("first${1}_second${2}_full${0}")); parameters.add(ff2.literal("noMatch")); Function fn = finder.findFunction("stringTemplate", parameters, fallback); Object result = fn.evaluate(null); assertEquals("noMatch", result); }
@Test public void testDynamic() throws SchemaException { parameters.add(ff2.property("input")); parameters.add(ff2.property("pattern")); parameters.add(ff2.property("template")); Function fn = finder.findFunction("stringTemplate", parameters, fallback); SimpleFeatureType type = DataUtilities.createType("test", "input:string,pattern:string,template:string"); SimpleFeature f1 = SimpleFeatureBuilder.build(type, new Object[] { "abc123_567", ".*(\\d{3})_(\\d{3})", "${1}|${2}" }, null); assertEquals("123|567", fn.evaluate(f1)); SimpleFeature f2 = SimpleFeatureBuilder.build(type, new Object[] { "abc12_67", "(.*)_(\\d{3})", "${1}_${2}" }, null); assertEquals(null, fn.evaluate(f2)); SimpleFeature f3 = SimpleFeatureBuilder.build(type, new Object[] { "abc12_67", "(.*)_(.*)", "${1}|${2}" }, null); assertEquals("abc12|67", fn.evaluate(f3)); }