This code should return the Optional String when given siteUrl contains key from the predefined list of pairs (or Optional.empty() otherwise).
Is there a better way than using new Pair(null, null) here? Or maybe change the whole expression?
static Optional<String> get(String siteUrl) {
return Optional.ofNullable(URL_TO_ENVIRONMENT
.stream()
.filter(p -> siteUrl.contains(p.getKey()))
.findFirst().orElse(new Pair(null, null)).getValue());
}
private static final List<Pair> URL_TO_ENVIRONMENT = buildList();
private static List<Pair> buildList() {
return Arrays.asList(
new Pair("aaa.mysite.com", "aaa_something"),
new Pair("bbb.mysite.com", "bbb_something"),
new Pair("ccc.mysite.com", "ccc_comething"));
}