MappingFunction { @FunctionMeta(description = "Translate code based on a mapping table", example = "${w:translate('2', '1|2|3', 'one|two|three', 'not found')}") public static String translate(String code, String codes, String values, String defaultValue) { List<String> codeList = Splitters.by('|').trim().split(codes); List<String> valueList = Splitters.by('|').trim().split(values); if (codeList != null && valueList != null) { int index = 0; for (String codeItem : codeList) { if (codeItem.equals(code)) { if (index < valueList.size()) { return valueList.get(index); } else { break; } } index++; } } return defaultValue; }  @FunctionMeta(description = "Translate code based on a mapping table", example = "${w:translate('2', '1|2|3', 'one|two|three', 'not found')}") static String translate(String code, String codes, String values, String defaultValue);  }
@Test public void test() { String codes = "1|2|3"; String values = "one|two|three"; String defaultValue = "not found"; Assert.assertEquals("one", MappingFunction.translate("1", codes, values, defaultValue)); Assert.assertEquals("two", MappingFunction.translate("2", codes, values, defaultValue)); Assert.assertEquals("three", MappingFunction.translate("3", codes, values, defaultValue)); Assert.assertEquals(defaultValue, MappingFunction.translate("4", codes, values, defaultValue)); Assert.assertEquals(defaultValue, MappingFunction.translate("1", codes, null, defaultValue)); Assert.assertEquals(defaultValue, MappingFunction.translate("1", null, values, defaultValue)); Assert.assertEquals(defaultValue, MappingFunction.translate("1", "", "", defaultValue)); Assert.assertEquals("2", MappingFunction.translate("TWO", "ONE|TWO|THREE", "1|2", defaultValue)); Assert.assertEquals(defaultValue, MappingFunction.translate("THREE", "ONE|TWO|THREE", "1|2", defaultValue)); }