RegexCache { public RegexParseResult getRegexForLiterals(FlagSet initialFlags, LiteralTree... stringLiterals) { return cache.computeIfAbsent( Arrays.asList(stringLiterals), k -> new RegexParser(new RegexSource(k), initialFlags).parse()); }  RegexParseResult getRegexForLiterals(FlagSet initialFlags, LiteralTree... stringLiterals);  }
@Test void same_result_if_same_tree_is_provided() { CompilationUnitTree cut = JParserTestUtils.parse( "class A {\n" + " String s0 = \"abc\";\n" + " String s1 = \"abc\";\n" + "}"); ClassTree a = (ClassTree) cut.types().get(0); List<Tree> fields = a.members(); LiteralTree s0 = (LiteralTree) ((VariableTree) fields.get(0)).initializer(); LiteralTree s1 = (LiteralTree) ((VariableTree) fields.get(1)).initializer(); RegexCache cache = new RegexCache(); RegexParseResult resultForS0 = cache.getRegexForLiterals(new FlagSet(), s0); RegexParseResult resultForS1 = cache.getRegexForLiterals(new FlagSet(), s1); assertThat(s0.value()).isEqualTo(s1.value()); assertThat(resultForS0) .isNotEqualTo(resultForS1) .isSameAs(cache.getRegexForLiterals(new FlagSet(), s0)); assertThat(resultForS1).isSameAs(cache.getRegexForLiterals(new FlagSet(), s1)); }
@Test void same_result_if_same_trees_are_provided() { CompilationUnitTree cut = JParserTestUtils.parse( "class A {\n" + " String s0 = \"abc\";\n" + " String s1 = \"abc\";\n" + "}"); ClassTree a = (ClassTree) cut.types().get(0); List<Tree> fields = a.members(); LiteralTree s0 = (LiteralTree) ((VariableTree) fields.get(0)).initializer(); LiteralTree s1 = (LiteralTree) ((VariableTree) fields.get(1)).initializer(); RegexCache cache = new RegexCache(); RegexParseResult resultForS0S1 = cache.getRegexForLiterals(new FlagSet(), s0, s1); RegexParseResult resultForS1S0 = cache.getRegexForLiterals(new FlagSet(), s1, s0); assertThat(s0.value() + s1.value()).isEqualTo(s1.value() + s0.value()); assertThat(resultForS0S1) .isNotEqualTo(resultForS1S0) .isSameAs(cache.getRegexForLiterals(new FlagSet(), s0, s1)); assertThat(resultForS1S0).isSameAs(cache.getRegexForLiterals(new FlagSet(), s1, s0)); }