ã¹ãã©ãã¸ã¼ãã¿ã¼ã³ã¨ããGoFã®ãã¶ã¤ã³ãã¿ã¼ã³ãè¯ãã³ã¼ãæªãã³ã¼ãã§ãç´¹ä»ããã¦ããã®ã§ãç´ æ¯ããã¾ãã
èªåã®è¨èã§ä¸æãªè¨èªåãã§ãã¦ããªãã®ã§ãã¡ãªããã¯åèæ å ±ãè¯ãã³ã¼ãæªãã³ã¼ãã®è³¼å ¥ãã¾ãã¯å¥ã®æ¹ã®è¨äºãåèã«ãã¦ãã ããã
ç°å¢
- Java
- 17
ä»åã®å¦ç
è¤æ°ã®è¨èªã®æç« ããã²ã¨ã¤ã®è¨èªã«å¤æããã
â» å¤æã®å ·ä½çãªãã¸ãã¯ã¯å®è£ ããªãã
対å¿
æç« ã¨è¨èªåãè¨è¼ãããDTOãçæããã
@Data public class Question { private String sentence; private Locale locale; }
ã¹ãã©ãã¸ã¼ãã¿ã¼ã³ã使ç¨ããããããã«ãFCCã使ç¨ãã¦Questionã®ãªã¹ããç¨æããLocaleãKeyã¨ããMapåã«å¤æããã
@Data @Builder public class QuestionList { List<Question> value; public Map<Locale, List<Question>> groupByLocale() { return value.stream() .collect(Collectors.groupingBy( Question::getLocale )); } }
翻訳ç¨ã®interfaceãç¨æããã
public interface Translator { List<Question> translate(List<Question> question); }
interfaceãç¶æ¿ãã翻訳ã¯ã©ã¹ãç¨æããã
public class FromEnToJa implements Translator { @Override public List<Question> translate(List<Question> question) { return question.stream() .map(e -> e.toBuilder().locale(Locale.JAPANESE).build() ).collect(Collectors.toList()); } } public class FromFrToJa implements Translator { @Override public List<Question> translate(List<Question> question) { return question.stream() .map(e -> e.toBuilder().locale(Locale.JAPANESE).build() ).collect(Collectors.toList()); } } public class NoopTransrate implements Translator { @Override public List<Question> translate(List<Question> question) { return question; } }
LocaleãKeyã¨ããå®éã«å¦çããã¯ã©ã¹ãvalueã¨ããMapãç¨æããã
private final Map<Locale, Translator> toJaActionMap = Map.of( Locale.ENGLISH, new FromEnToJa(), Locale.JAPANESE, new NoopTransrate(), Locale.FRENCH, new FromFrToJa() );
æç« ã¨Localeãå ã«ããã¹ã¦ãæ¥æ¬èªã«å¤æããå¦çãããã
public QuestionList translate() { final List<Question> list = new ArrayList<>(); // å¦çã移è²ãã¦ããã®ã§ãä¸èº«ãéã翻訳å¦çãè¡ãã this.groupByLocale().forEach( (key, value) -> list.addAll(toJaActionMap.get(key).translate(value)) ); return new QuestionList(list); }
ã½ã¼ã¹ã³ã¼ã
- https://github.com/hirotoKirimaru/cucumber-sample/blob/a3e62238eea16ebbdfb8e9c4a1d9b4c24fd92c58/kirimaru-core/src/main/java/kirimaru/biz/domain/Question.java
- https://github.com/hirotoKirimaru/cucumber-sample/blob/a3e62238eea16ebbdfb8e9c4a1d9b4c24fd92c58/kirimaru-core/src/main/java/kirimaru/biz/domain/QuestionList.java
- https://github.com/hirotoKirimaru/cucumber-sample/blob/a3e62238eea16ebbdfb8e9c4a1d9b4c24fd92c58/kirimaru-core/src/test/java/kirimaru/biz/domain/StrategyPatternTest.java
çµããã«
æ£ç´ãä»åã®ä¾ç¤ºã¨ããå§è²å¦çã¯ã¡ãªããèãã§ãããã ãã¹ãã©ãã¸ã¼ãã¿ã¼ã³ãç´ æ¯ããã¦ãããã¨ã§ãè¦éãã®æªãå¦çãåãåºããã¨ããæèãæã£ã¦ããããã§ãã
å°å³ãªãããJavaGoldã§åå¼·ããBiConsumerã使ãã¿ã¤ãã³ã°ãåãã£ã¦ããªãã£ãã®ã§ãä»åã®ã¹ãã©ãã¸ã¼ãã¿ã¼ã³ãç´ æ¯ããããã¨ã§ä½¿ç¨ã§ãã¦ããã£ãã§ãã
// ãããå é¨çã«ã¯BiConsumerã§å¦çããã¦ãã¾ãã this.groupByLocale().forEach( (key, value) -> list.addAll(toJaActionMap.get(key).translate(value)) );