LocaleUtil { public static Optional<Locale> getLocaleMatchByLanguage(VaadinRequest request, List<Locale> providedLocales) { Locale foundLocale = null; Enumeration<Locale> locales = request.getLocales(); while (locales.hasMoreElements()) { Locale locale = locales.nextElement(); Optional<Locale> matching = providedLocales .stream().filter(providedLocale -> providedLocale .getLanguage().equals(locale.getLanguage())) .findFirst(); if (matching.isPresent()) { foundLocale = matching.get(); break; } } return Optional.ofNullable(foundLocale); } private  LocaleUtil(); static Optional<Locale> getExactLocaleMatch(VaadinRequest request,
            List<Locale> providedLocales); static Optional<Locale> getLocaleMatchByLanguage(VaadinRequest request,
            List<Locale> providedLocales);  }
@Test public void language_match_gets_correct_target_by_request_priority() { Optional<Locale> exactLocaleMatch = LocaleUtil.getLocaleMatchByLanguage(request, Arrays.asList(Locale.US, LOCALE_FI)); Assert.assertEquals( "Found wrong locale event though an language match should have been available.", LOCALE_FI, exactLocaleMatch.get()); }
@Test public void language_match_returns_null_when_no_match() { Optional<Locale> exactLocaleMatch = LocaleUtil.getLocaleMatchByLanguage(request, Arrays.asList(Locale.FRENCH, Locale.KOREA)); Assert.assertFalse( "Found locale event though none should have been available.", exactLocaleMatch.isPresent()); }