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 LocalePriorityList, such as using the following equivalent patterns:
list = LocalePriorityList.add("af, en, fr;q=0.9").build();
list2 = LocalePriorityList
.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/locales 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.3If 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
| Modifier and Type | Class and Description |
|---|---|
| public static class | LocalePriorityList.Builder
Class used for building LocalePriorityLists. |
| Modifier and Type | Field and Description |
|---|---|
| private static final Double | |
| private final Map | |
| private static final Pattern | |
| private static Comparator | |
| private static final Pattern |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static LocalePriorityList. | |
| public static LocalePriorityList. | |
| public static LocalePriorityList. | Returns: a new builder with these locales, for chaininglist of locales with weights list)Creates a Builder and adds locales with weights. |
| public static LocalePriorityList. | |
| public boolean | equals(final Object
the reference object with which to compare. o)Overrides java. |
| public Set | |
| public Double | |
| public int | |
| public Iterator | |
| public String |
| D1 | back to summary |
|---|---|
| private static final Double D1 | |
| languagesAndWeights | back to summary |
|---|---|
| private final Map<ULocale, Double> languagesAndWeights | |
| languageSplitter | back to summary |
|---|---|
| private static final Pattern languageSplitter | |
| myDescendingDouble | back to summary |
|---|---|
| private static Comparator<Double> myDescendingDouble | |
| weightSplitter | back to summary |
|---|---|
| private static final Pattern weightSplitter | |
| LocalePriorityList | back to summary |
|---|---|
| private LocalePriorityList(final Map<ULocale, Double> languageToWeight) | |
| add | back to summary |
|---|---|
| public static LocalePriorityList. Creates a Builder and adds locales, each with weight 1.0.
| |
| add | back to summary |
|---|---|
| public static LocalePriorityList. Creates a Builder and adds a locale with a specified weight. A zero or negative weight leads to removing the locale. A weight greater than 1 is pinned to 1.
| |
| add | back to summary |
|---|---|
| public static LocalePriorityList. Creates a Builder and adds locales with weights.
| |
| add | back to summary |
|---|---|
| public static LocalePriorityList. Creates a Builder, parses the RFC 2616 string, and adds locales with weights accordingly.
| |
| equals | back to summary |
|---|---|
| public boolean equals(final Object o) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes. | |
| getULocales | back to summary |
|---|---|
| public Set Returns the locales as an immutable Set view. The set has the same iteration order as this object itself. | |
| getWeight | back to summary |
|---|---|
| public Double getWeight(ULocale locale) Returns the weight for a given language/locale, or null if there is none. Note that the weights may be adjusted from those used to build the list. | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for the object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| iterator | back to summary |
|---|---|
| public Iterator Implements java. Doc from java. Returns an iterator over elements of type | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| Modifier and Type | Field and Description |
|---|---|
| private LocalePriorityList | built
The builder is reusable but rarely reused. |
| private boolean | |
| private Map | languageToWeight
These store the input languages and weights, in chronological order, where later additions override previous ones. |
| Access | Constructor and Description |
|---|---|
| private | |
| private |
| Modifier and Type | Method and Description |
|---|---|
| public LocalePriorityList. | Returns: this, for chaininglist of locales with weights list)Adds locales with weights. |
| public LocalePriorityList. | |
| public LocalePriorityList. | |
| public LocalePriorityList. | |
| public LocalePriorityList. | |
| public LocalePriorityList | |
| public LocalePriorityList | Returns: A LocalePriorityListwhen true, each locale's given weight is preserved. preserveWeights)Creates a LocalePriorityList. |
| built | back to summary |
|---|---|
| private LocalePriorityList built The builder is reusable but rarely reused. Avoid cloning the map when not needed. Exactly one of languageToWeight and built is null. | |
| hasWeights | back to summary |
|---|---|
| private boolean hasWeights | |
| languageToWeight | back to summary |
|---|---|
| private Map<ULocale, Double> languageToWeight These store the input languages and weights, in chronological order, where later additions override previous ones. | |
| Builder | back to summary |
|---|---|
| private Builder() Private constructor, only used by LocalePriorityList | |
| Builder | back to summary |
|---|---|
| private Builder(LocalePriorityList list) | |
| add | back to summary |
|---|---|
| public LocalePriorityList. Adds locales with weights.
| |
| add | back to summary |
|---|---|
| public LocalePriorityList. Adds a locale with weight 1.0.
| |
| add | back to summary |
|---|---|
| public LocalePriorityList. Adds locales, each with weight 1.0.
| |
| add | back to summary |
|---|---|
| public LocalePriorityList. Adds a locale with a specified weight. Overrides any previous weight for the locale. A zero or negative weight leads to removing the locale. A weight greater than 1 is pinned to 1.
| |
| add | back to summary |
|---|---|
| public LocalePriorityList. Parses the RFC 2616 string, and adds locales with weights accordingly.
| |
| build | back to summary |
|---|---|
| public LocalePriorityList build() Creates a LocalePriorityList. This is equivalent to
| |
| build | back to summary |
|---|---|
| public LocalePriorityList build(boolean preserveWeights) Creates a LocalePriorityList.
| |