-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLanguageMap.java
More file actions
107 lines (92 loc) · 2.85 KB
/
LanguageMap.java
File metadata and controls
107 lines (92 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* Copyright 2016-2025 Berry Cloud Ltd. All rights reserved.
*/
package dev.learning.xapi.model;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import dev.learning.xapi.jackson.LocaleSerializer;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Locale.LanguageRange;
import java.util.Map;
/**
* A language map is a dictionary where the key is a RFC 5646 Language Tag, and the value is a
* string in the language specified in the tag.
*
* @author Thomas Turrell-Croft
*
* @see <a href="https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#lang-maps">Language
* Maps</a>
*/
@JsonSerialize(keyUsing = LocaleSerializer.LocaleKeySerializer.class)
public class LanguageMap extends LinkedHashMap<Locale, String> {
private static final long serialVersionUID = 7375610804995032187L;
/**
* Cached undefined locale instance to avoid repeated object creation.
*/
private static final Locale UNDEFINED_LOCALE = new Locale("und");
/**
* Constructs an empty LanguageMap.
*/
public LanguageMap() {
super();
}
/**
* Constructs an new LanguageMap with the same mappings as the specified Map.
*
* @param languageMap the map whose mappings are to be placed in this LanguageMap
*/
public LanguageMap(Map<Locale, String> languageMap) {
super(languageMap);
}
/**
* Puts a value in the language map with the undefined locale as a key. If the language map
* previously contained a mapping for the undefined locale, the old value is replaced.
*
* @param value to be added with the undefined locale as a key
*/
public void put(String value) {
this.put(UNDEFINED_LOCALE, value);
}
/**
* <p>
* Returns the value which best matches one of the specified language ranges.
* </p>
* <p>
* The return value could be:
* </p>
* <ol>
* <li>Best matching locale key using the lookup mechanism defined in RFC 4647</li>
* <li>Locale with undefined language key</li>
* <li>First value in language map</li>
* <li>null</li>
* </ol>
* <p>
* <strong>Note</strong>: This helper method is not defined in the xAPI specification.
* </p>
*
* @param languageRanges used for matching a locale key
*
* @return the value which best matches one of the specified language ranges, or null if this map
* is empty
*/
public String get(List<LanguageRange> languageRanges) {
// Return best match
final var best = Locale.lookup(languageRanges, keySet());
if (best != null) {
return get(best);
}
// Otherwise return UND
final var und = get(UNDEFINED_LOCALE);
if (und != null) {
return und;
}
// Otherwise return first
final var iterator = keySet().iterator();
if (iterator.hasNext()) {
return get(iterator.next());
}
// Map must be empty
return null;
}
}