Package com.ibm.icu.util
Class LocalePriorityList
- java.lang.Object
-
- com.ibm.icu.util.LocalePriorityList
-
- All Implemented Interfaces:
java.lang.Iterable<ULocale>
public class LocalePriorityList extends java.lang.Object implements java.lang.Iterable<ULocale>
Provides an immutable list of languages (locales) in priority order. The string format is based on the Accept-Language format http://www.ietf.org/rfc/rfc2616.txt, such as "af, en, fr;q=0.9". Syntactically it is slightly more lenient, in allowing extra whitespace between elements, extra commas, and more than 3 decimals (on input), and pins between 0 and 1.In theory, Accept-Language indicates the relative 'quality' of each item, but in practice, all of the browsers just take an ordered list, like "en, fr, de", and synthesize arbitrary quality values that put these in the right order, like: "en, fr;q=0.7, de;q=0.3". The quality values in these de facto semantics thus have nothing to do with the relative qualities of the original. Accept-Language also doesn't specify the interpretation of multiple instances, eg what "en, fr, en;q=.5" means.
There are various ways to build a LanguagePriorityList, such as using the following equivalent patterns:
list = LanguagePriorityList.add("af, en, fr;q=0.9").build(); list2 = LanguagePriorityList .add(ULocale.forString("af")) .add(ULocale.ENGLISH) .add(ULocale.FRENCH, 0.9d) .build();When the list is built, the internal values are sorted in descending order by weight, and then by input order. That is, if two languages have the same weight, the first one in the original order comes first. If exactly the same language tag appears multiple times, the last one wins. There are two options when building. If preserveWeights are on, then "de;q=0.3, ja;q=0.3, en, fr;q=0.7, de " would result in the following:en;q=1.0 de;q=1.0 fr;q=0.7 ja;q=0.3
If it is off (the default), then all weights are reset to 1.0 after reordering. This is to match the effect of the Accept-Language semantics as used in browsers, and results in the following: *en;q=1.0 de;q=1.0 fr;q=1.0 ja;q=1.0
- Author:
- markdavis@google.com
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classLocalePriorityList.BuilderClass used for building LanguagePriorityLists
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static LocalePriorityList.Builderadd(LocalePriorityList languagePriorityList)Add a language priority list.static LocalePriorityList.Builderadd(ULocale... languageCode)Add a language code to the list being built, with weight 1.0.static LocalePriorityList.Builderadd(ULocale languageCode, double weight)Add a language code to the list being built, with specified weight.static LocalePriorityList.Builderadd(java.lang.String acceptLanguageString)Add language codes to the list being built, using a string in rfc2616 (lenient) format, where each language is a validULocale.booleanequals(java.lang.Object o)java.lang.DoublegetWeight(ULocale language)Return the weight for a given language, or null if there is none.inthashCode()java.util.Iterator<ULocale>iterator()java.lang.StringtoString()
-
-
-
Method Detail
-
add
public static LocalePriorityList.Builder add(ULocale... languageCode)
Add a language code to the list being built, with weight 1.0.- Parameters:
languageCode- locale/language to be added- Returns:
- internal builder, for chaining
-
add
public static LocalePriorityList.Builder add(ULocale languageCode, double weight)
Add a language code to the list being built, with specified weight.- Parameters:
languageCode- locale/language to be addedweight- value from 0.0 to 1.0- Returns:
- internal builder, for chaining
-
add
public static LocalePriorityList.Builder add(LocalePriorityList languagePriorityList)
Add a language priority list.- Parameters:
languagePriorityList- list to add all the members of- Returns:
- internal builder, for chaining
-
add
public static LocalePriorityList.Builder add(java.lang.String acceptLanguageString)
Add language codes to the list being built, using a string in rfc2616 (lenient) format, where each language is a validULocale.- Parameters:
acceptLanguageString- String in rfc2616 format (but leniently parsed)- Returns:
- internal builder, for chaining
-
getWeight
public java.lang.Double getWeight(ULocale language)
Return the weight for a given language, or null if there is none. Note that the weights may be adjusted from those used to build the list.- Parameters:
language- to get weight of- Returns:
- weight
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
iterator
public java.util.Iterator<ULocale> iterator()
- Specified by:
iteratorin interfacejava.lang.Iterable<ULocale>
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
-