1 package lv.id.jc.piglatin.core;
2
3 import java.util.function.Function;
4 import java.util.function.UnaryOperator;
5 import java.util.regex.MatchResult;
6 import java.util.regex.Matcher;
7
8 import org.springframework.beans.factory.annotation.Qualifier;
9 import org.springframework.stereotype.Component;
10 import org.springframework.validation.annotation.Validated;
11
12
13
14
15
16 @Component("phraseTranslator")
17 public class PhraseTranslator implements UnaryOperator<String> {
18 private final Function<String, Matcher> wordMatcher;
19 private final Function<MatchResult, String> pigLatinTranslator;
20
21
22
23
24
25
26
27 public PhraseTranslator(
28 Function<String, Matcher> wordMatcher,
29 @Validated @Qualifier("wordTranslator")
30 UnaryOperator<String> wordTranslator
31 ) {
32 this.wordMatcher = wordMatcher;
33 this.pigLatinTranslator = wordTranslator.compose(MatchResult::group);
34 }
35
36
37
38
39
40
41
42 @Override
43 public String apply(String phrase) {
44 return wordMatcher.apply(phrase).replaceAll(pigLatinTranslator);
45 }
46 }