@Test public void testGetLocaleList() throws Exception { Locale[] locales = RequestHeader.getLocaleList("da, en-gb;q=0.8, en;q=0.7"); assertThat(locales[0]).isEqualTo(Locale.forLanguageTag("da")); assertThat(locales[1]).isEqualTo(Locale.forLanguageTag("en-gb")); assertThat(locales[2]).isEqualTo(Locale.ENGLISH); locales = RequestHeader.getLocaleList("fr"); assertThat(locales[0]).isEqualTo(Locale.FRENCH); locales = RequestHeader.getLocaleList("en-ca,en;q=0.8,en-us;q=0.6,de-de;q=0.4,de;q=0.2"); assertThat(locales[0]).isEqualTo(Locale.CANADA); assertThat(locales[1]).isEqualTo(Locale.ENGLISH); assertThat(locales[2]).isEqualTo(Locale.US); assertThat(locales[3]).isEqualTo(Locale.GERMANY); assertThat(locales[4]).isEqualTo(Locale.GERMAN); locales = RequestHeader.getLocaleList("en-ca,en-us;q=0.6 ,de;q=0.2, en;q=0.8, ,de-de;q=0.4"); assertThat(locales[0]).isEqualTo(Locale.CANADA); assertThat(locales[1]).isEqualTo(Locale.ENGLISH); assertThat(locales[2]).isEqualTo(Locale.US); assertThat(locales[3]).isEqualTo(Locale.GERMANY); assertThat(locales[4]).isEqualTo(Locale.GERMAN); locales = RequestHeader.getLocaleList(null); assertThat(locales).isEmpty(); locales = RequestHeader.getLocaleList(""); assertThat(locales).isEmpty(); locales = RequestHeader.getLocaleList("abc"); assertThat(locales[0]).isEqualTo(new Locale("abc")); locales = RequestHeader.getLocaleList("123$4567"); assertThat(locales).isEmpty(); locales = RequestHeader.getLocaleList("en, en-US,en-cockney,i-cherokee,x-pig-latin"); assertThat(locales[0]).isEqualTo(Locale.ENGLISH); assertThat(locales[1]).isEqualTo(Locale.US); assertThat(locales[2]).isEqualTo(new Locale("en", "", "cockney")); assertThat(locales[3]).isEqualTo(Locale.forLanguageTag("i-cherokee")); assertThat(locales[4]).isEqualTo(Locale.forLanguageTag("x-pig-latin")); locales = RequestHeader.getLocaleList("en-US,en;q=0.8,de;q=0.6,fr;q=0.4"); assertThat(locales[0]).isEqualTo(Locale.US); assertThat(locales[1]).isEqualTo(Locale.ENGLISH); assertThat(locales[2]).isEqualTo(Locale.GERMAN); assertThat(locales[3]).isEqualTo(Locale.FRENCH); }
public static Locale[] getLocaleList(String accept) { if (accept == null || accept.length() == 0) { return new Locale[0]; } Map<Float, List<Locale>> locales = new TreeMap<>(new Comparator<Float>() { @Override public int compare(Float o1, Float o2) { return o2.compareTo(o1); } }); String[] segments = accept.split(","); for (String segment : segments) { Matcher matcher = LANGUAGE_SEGMENT_PATTERN.matcher(segment.trim()); if (!matcher.matches()) { continue; } float q = 1; if (matcher.group(5) != null) { q = Float.valueOf(matcher.group(5)); } List<Locale> l = locales.get(q); if (l == null) { l = new ArrayList<>(); locales.put(q, l); } l.add(Locale.forLanguageTag(matcher.group(1))); } List<Locale> list = new ArrayList<>(); for (Map.Entry<Float, List<Locale>> entry : locales.entrySet()) { list.addAll(entry.getValue()); } return list.toArray(new Locale[list.size()]); }
RequestHeader { public static Locale[] getLocaleList(String accept) { if (accept == null || accept.length() == 0) { return new Locale[0]; } Map<Float, List<Locale>> locales = new TreeMap<>(new Comparator<Float>() { @Override public int compare(Float o1, Float o2) { return o2.compareTo(o1); } }); String[] segments = accept.split(","); for (String segment : segments) { Matcher matcher = LANGUAGE_SEGMENT_PATTERN.matcher(segment.trim()); if (!matcher.matches()) { continue; } float q = 1; if (matcher.group(5) != null) { q = Float.valueOf(matcher.group(5)); } List<Locale> l = locales.get(q); if (l == null) { l = new ArrayList<>(); locales.put(q, l); } l.add(Locale.forLanguageTag(matcher.group(1))); } List<Locale> list = new ArrayList<>(); for (Map.Entry<Float, List<Locale>> entry : locales.entrySet()) { list.addAll(entry.getValue()); } return list.toArray(new Locale[list.size()]); } }
RequestHeader { public static Locale[] getLocaleList(String accept) { if (accept == null || accept.length() == 0) { return new Locale[0]; } Map<Float, List<Locale>> locales = new TreeMap<>(new Comparator<Float>() { @Override public int compare(Float o1, Float o2) { return o2.compareTo(o1); } }); String[] segments = accept.split(","); for (String segment : segments) { Matcher matcher = LANGUAGE_SEGMENT_PATTERN.matcher(segment.trim()); if (!matcher.matches()) { continue; } float q = 1; if (matcher.group(5) != null) { q = Float.valueOf(matcher.group(5)); } List<Locale> l = locales.get(q); if (l == null) { l = new ArrayList<>(); locales.put(q, l); } l.add(Locale.forLanguageTag(matcher.group(1))); } List<Locale> list = new ArrayList<>(); for (Map.Entry<Float, List<Locale>> entry : locales.entrySet()) { list.addAll(entry.getValue()); } return list.toArray(new Locale[list.size()]); }  }
RequestHeader { public static Locale[] getLocaleList(String accept) { if (accept == null || accept.length() == 0) { return new Locale[0]; } Map<Float, List<Locale>> locales = new TreeMap<>(new Comparator<Float>() { @Override public int compare(Float o1, Float o2) { return o2.compareTo(o1); } }); String[] segments = accept.split(","); for (String segment : segments) { Matcher matcher = LANGUAGE_SEGMENT_PATTERN.matcher(segment.trim()); if (!matcher.matches()) { continue; } float q = 1; if (matcher.group(5) != null) { q = Float.valueOf(matcher.group(5)); } List<Locale> l = locales.get(q); if (l == null) { l = new ArrayList<>(); locales.put(q, l); } l.add(Locale.forLanguageTag(matcher.group(1))); } List<Locale> list = new ArrayList<>(); for (Map.Entry<Float, List<Locale>> entry : locales.entrySet()) { list.addAll(entry.getValue()); } return list.toArray(new Locale[list.size()]); }  abstract String uri(); abstract String remoteAddress(); abstract String host(); abstract String path(); abstract MediaType mediaType(); abstract Collection<MediaType> mediaTypes(); abstract boolean accepts(String mimeType); abstract Cookies cookies(); Cookie cookie(String name); abstract java.util.Map<String, List<String>> headers(); String getHeader(String headerName); abstract String encoding(); abstract String language(); Locale[] languages(); abstract String charset(); static Locale[] getLocaleList(String accept); }
RequestHeader { public static Locale[] getLocaleList(String accept) { if (accept == null || accept.length() == 0) { return new Locale[0]; } Map<Float, List<Locale>> locales = new TreeMap<>(new Comparator<Float>() { @Override public int compare(Float o1, Float o2) { return o2.compareTo(o1); } }); String[] segments = accept.split(","); for (String segment : segments) { Matcher matcher = LANGUAGE_SEGMENT_PATTERN.matcher(segment.trim()); if (!matcher.matches()) { continue; } float q = 1; if (matcher.group(5) != null) { q = Float.valueOf(matcher.group(5)); } List<Locale> l = locales.get(q); if (l == null) { l = new ArrayList<>(); locales.put(q, l); } l.add(Locale.forLanguageTag(matcher.group(1))); } List<Locale> list = new ArrayList<>(); for (Map.Entry<Float, List<Locale>> entry : locales.entrySet()) { list.addAll(entry.getValue()); } return list.toArray(new Locale[list.size()]); }  abstract String uri(); abstract String remoteAddress(); abstract String host(); abstract String path(); abstract MediaType mediaType(); abstract Collection<MediaType> mediaTypes(); abstract boolean accepts(String mimeType); abstract Cookies cookies(); Cookie cookie(String name); abstract java.util.Map<String, List<String>> headers(); String getHeader(String headerName); abstract String encoding(); abstract String language(); Locale[] languages(); abstract String charset(); static Locale[] getLocaleList(String accept);  }