MCPcopy Create free account
hub / github.com/DiUS/java-faker / FakeValues

Class FakeValues

src/main/java/com/github/javafaker/service/FakeValues.java:11–92  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9import java.util.Map;
10
11public class FakeValues implements FakeValuesInterface {
12 private final Locale locale;
13 private final String filename;
14 private final String path;
15 private Map values;
16
17 FakeValues(Locale locale) {
18 this(locale, getFilename(locale), getFilename(locale));
19 }
20
21 private static String getFilename(Locale locale) {
22 final StringBuilder filename = new StringBuilder(language(locale));
23 if (!"".equals(locale.getCountry())) {
24 filename.append("-").append(locale.getCountry());
25 }
26 return filename.toString();
27 }
28
29 /**
30 * If you new up a locale with "he", it gets converted to "iw" which is old.
31 * This addresses that unfortunate condition.
32 */
33 private static String language(Locale l) {
34 if (l.getLanguage().equals("iw")) {
35 return "he";
36 }
37 return l.getLanguage();
38 }
39
40 FakeValues(Locale locale, String filename, String path) {
41 this.locale = locale;
42 this.filename = filename;
43 this.path = path;
44 }
45
46 @Override
47 public Map get(String key) {
48 if (values == null) {
49 values = loadValues();
50 }
51
52 return values == null ? null : (Map) values.get(key);
53 }
54
55 private Map loadValues() {
56 String pathWithLocaleAndFilename = "/" + locale.getLanguage() + "/" + this.filename;
57 String pathWithFilename = "/" + filename + ".yml";
58 String pathWithLocale = "/" + locale.getLanguage() + ".yml";
59
60 List<String> paths = Arrays.asList(pathWithLocaleAndFilename, pathWithFilename, pathWithLocale);
61 InputStream stream = null;
62 for (String path : paths) {
63 stream = findStream(path);
64 if (stream != null) {
65 break;
66 }
67 }
68

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected