@Test public void exampleUsage() { List<TextFragment> fragments = TemplateParser.parseTemplateString("hello-{{world}}"); assertThat( fragments, contains(staticFragment("hello-"), TextFragment.dynamicFragment("world"))); }
static List<TextFragment> parseTemplateString(String template) { ArrayList<TextFragment> fragments = new ArrayList<>(); Matcher fragmentMatcher = DYNAMIC_FRAGMENT_PATTERN.matcher(template); int position = 0; while (position < template.length()) { if (!fragmentMatcher.find(position)) { String prefix = template.substring(position); fragments.add(staticFragment(prefix)); break; } final int prefixStart = position; final int prefixEnd = fragmentMatcher.start(1) - "{{".length(); final int dynamicStart = fragmentMatcher.start(1); final int dynamicEnd = fragmentMatcher.end(1); if (prefixEnd - prefixStart > 0) { String prefixText = template.substring(prefixStart, prefixEnd); fragments.add(staticFragment(prefixText)); } String dynamicFragmentText = template.substring(dynamicStart, dynamicEnd); fragments.add(dynamicFragment(dynamicFragmentText)); position = dynamicEnd + "}}".length(); } return fragments; }
TemplateParser { static List<TextFragment> parseTemplateString(String template) { ArrayList<TextFragment> fragments = new ArrayList<>(); Matcher fragmentMatcher = DYNAMIC_FRAGMENT_PATTERN.matcher(template); int position = 0; while (position < template.length()) { if (!fragmentMatcher.find(position)) { String prefix = template.substring(position); fragments.add(staticFragment(prefix)); break; } final int prefixStart = position; final int prefixEnd = fragmentMatcher.start(1) - "{{".length(); final int dynamicStart = fragmentMatcher.start(1); final int dynamicEnd = fragmentMatcher.end(1); if (prefixEnd - prefixStart > 0) { String prefixText = template.substring(prefixStart, prefixEnd); fragments.add(staticFragment(prefixText)); } String dynamicFragmentText = template.substring(dynamicStart, dynamicEnd); fragments.add(dynamicFragment(dynamicFragmentText)); position = dynamicEnd + "}}".length(); } return fragments; } }
TemplateParser { static List<TextFragment> parseTemplateString(String template) { ArrayList<TextFragment> fragments = new ArrayList<>(); Matcher fragmentMatcher = DYNAMIC_FRAGMENT_PATTERN.matcher(template); int position = 0; while (position < template.length()) { if (!fragmentMatcher.find(position)) { String prefix = template.substring(position); fragments.add(staticFragment(prefix)); break; } final int prefixStart = position; final int prefixEnd = fragmentMatcher.start(1) - "{{".length(); final int dynamicStart = fragmentMatcher.start(1); final int dynamicEnd = fragmentMatcher.end(1); if (prefixEnd - prefixStart > 0) { String prefixText = template.substring(prefixStart, prefixEnd); fragments.add(staticFragment(prefixText)); } String dynamicFragmentText = template.substring(dynamicStart, dynamicEnd); fragments.add(dynamicFragment(dynamicFragmentText)); position = dynamicEnd + "}}".length(); } return fragments; } private  TemplateParser(); }
TemplateParser { static List<TextFragment> parseTemplateString(String template) { ArrayList<TextFragment> fragments = new ArrayList<>(); Matcher fragmentMatcher = DYNAMIC_FRAGMENT_PATTERN.matcher(template); int position = 0; while (position < template.length()) { if (!fragmentMatcher.find(position)) { String prefix = template.substring(position); fragments.add(staticFragment(prefix)); break; } final int prefixStart = position; final int prefixEnd = fragmentMatcher.start(1) - "{{".length(); final int dynamicStart = fragmentMatcher.start(1); final int dynamicEnd = fragmentMatcher.end(1); if (prefixEnd - prefixStart > 0) { String prefixText = template.substring(prefixStart, prefixEnd); fragments.add(staticFragment(prefixText)); } String dynamicFragmentText = template.substring(dynamicStart, dynamicEnd); fragments.add(dynamicFragment(dynamicFragmentText)); position = dynamicEnd + "}}".length(); } return fragments; } private  TemplateParser();  }
TemplateParser { static List<TextFragment> parseTemplateString(String template) { ArrayList<TextFragment> fragments = new ArrayList<>(); Matcher fragmentMatcher = DYNAMIC_FRAGMENT_PATTERN.matcher(template); int position = 0; while (position < template.length()) { if (!fragmentMatcher.find(position)) { String prefix = template.substring(position); fragments.add(staticFragment(prefix)); break; } final int prefixStart = position; final int prefixEnd = fragmentMatcher.start(1) - "{{".length(); final int dynamicStart = fragmentMatcher.start(1); final int dynamicEnd = fragmentMatcher.end(1); if (prefixEnd - prefixStart > 0) { String prefixText = template.substring(prefixStart, prefixEnd); fragments.add(staticFragment(prefixText)); } String dynamicFragmentText = template.substring(dynamicStart, dynamicEnd); fragments.add(dynamicFragment(dynamicFragmentText)); position = dynamicEnd + "}}".length(); } return fragments; } private  TemplateParser();   }