RegexContainer { static <T> Optional<RegexContainer<T>> checkAndCreate(String maybeRegex, T content) { if (maybeRegex.startsWith("/") && maybeRegex.endsWith("/")) { try { Pattern regex = Pattern.compile(maybeRegex.substring(1, maybeRegex.length() - 1)); return Optional.of(new RegexContainer<>(regex, content)); } catch (PatternSyntaxException e) { return Optional.empty(); } } return Optional.empty(); } RegexContainer(Pattern regex, T content);   }
@Test public void shouldCheckAndNotCreate() throws Exception { Optional<RegexContainer<Map<String, String>>> result = RegexContainer .checkAndCreate("/{}won't compile/", Collections.singletonMap("k", "v")); assertThat(result).isEmpty(); }