diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/subtag/LanguageSubtagRegistryLoader.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/subtag/LanguageSubtagRegistryLoader.java index 17ed2b876..9ee3cc8f4 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/subtag/LanguageSubtagRegistryLoader.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/subtag/LanguageSubtagRegistryLoader.java @@ -1,5 +1,8 @@ package org.hl7.fhir.utilities.i18n.subtag; +import lombok.Getter; +import lombok.With; + import java.io.IOException; import java.io.InputStream; import java.net.URL; @@ -7,6 +10,24 @@ import java.util.*; public class LanguageSubtagRegistryLoader { + @Getter + private final LanguageSubtagRegistry registry; + + @With + private final boolean loadLanguages; + + @With + private final boolean loadScripts; + + @With + private final boolean loadExtLangs; + + @With + private final boolean loadRegions; + + @With + private final boolean loadVariants; + public static boolean isMultiField(String field){ return DESCRIPTION.equals(field) || COMMENTS.equals(field) @@ -58,12 +79,19 @@ public class LanguageSubtagRegistryLoader { public static final String GRANDFATHERED = "grandfathered"; - private final LanguageSubtagRegistry registry; public LanguageSubtagRegistryLoader(LanguageSubtagRegistry registry) { - this.registry = registry; + this(registry, true, true, true, true, true); } + private LanguageSubtagRegistryLoader(LanguageSubtagRegistry registry, boolean loadLanguages, boolean loadScripts, boolean loadExtLangs, boolean loadRegions, boolean loadVariants) { + this.registry = registry; + this.loadLanguages = loadLanguages; + this.loadScripts = loadScripts; + this.loadExtLangs = loadExtLangs; + this.loadRegions = loadRegions; + this.loadVariants = loadVariants; + } public void loadFromDefaultResource() throws IOException { loadFromResource("lang.dat.txt"); } @@ -140,15 +168,15 @@ public class LanguageSubtagRegistryLoader { protected void addSubtag(Subtag subtag) { assert subtag.getSubtag() != null; - if (subtag instanceof LanguageSubtag) + if (subtag instanceof LanguageSubtag && loadLanguages) registry.addLanguage(subtag.getSubtag(), (LanguageSubtag) subtag); - else if (subtag instanceof ExtLangSubtag) + else if (subtag instanceof ExtLangSubtag && loadExtLangs ) registry.addExtLang(subtag.getSubtag(), (ExtLangSubtag) subtag); - else if (subtag instanceof ScriptSubtag) + else if (subtag instanceof ScriptSubtag && loadScripts) registry.addScript(subtag.getSubtag(), (ScriptSubtag) subtag); - else if (subtag instanceof RegionSubtag) + else if (subtag instanceof RegionSubtag && loadRegions) registry.addRegion(subtag.getSubtag(), (RegionSubtag) subtag); - else if (subtag instanceof VariantSubtag) + else if (subtag instanceof VariantSubtag && loadVariants) registry.addVariant(subtag.getSubtag(), (VariantSubtag) subtag); } protected Subtag processVariantRecord(Record record) { diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/subtag/LanguageSubtagRegistryTest.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/subtag/LanguageSubtagRegistryLoaderTest.java similarity index 68% rename from org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/subtag/LanguageSubtagRegistryTest.java rename to org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/subtag/LanguageSubtagRegistryLoaderTest.java index a6c8c9467..f55242f10 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/subtag/LanguageSubtagRegistryTest.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/subtag/LanguageSubtagRegistryLoaderTest.java @@ -1,15 +1,19 @@ package org.hl7.fhir.utilities.i18n.subtag; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import java.io.IOException; +import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.*; -public class LanguageSubtagRegistryTest { +public class LanguageSubtagRegistryLoaderTest { @Test - public void initializationTest() throws IOException { + public void defaultLoaderTest() throws IOException { /* languages.size(): 8259 extLangs.size(): 253 @@ -21,7 +25,7 @@ public class LanguageSubtagRegistryTest { LanguageSubtagRegistry registry = new LanguageSubtagRegistry(); LanguageSubtagRegistryLoader loader = new LanguageSubtagRegistryLoader(registry); loader.loadFromDefaultResource(); - + /* Test entries of every subtag type (language, script, variant, extLang, region) These should cover both simple, and more complex entries with a larger number of fields. @@ -186,4 +190,53 @@ public class LanguageSubtagRegistryTest { assertEquals("Honduras", hn.getDescriptions().get(0)); assertEquals("2005-10-16", hn.getAdded()); } + + @Test + public void testNoLanguagesLoading() throws IOException { + LanguageSubtagRegistry registry = new LanguageSubtagRegistry(); + LanguageSubtagRegistryLoader loader = new LanguageSubtagRegistryLoader(registry).withLoadLanguages(false); + + loader.loadFromDefaultResource(); + + assertTrue(registry.getLanguageKeys().isEmpty()); + assertFalse(registry.getExtLangKeys().isEmpty()); + assertFalse(registry.getRegionKeys().isEmpty()); + assertFalse(registry.getVariantKeys().isEmpty()); + assertFalse(registry.getScriptKeys().isEmpty()); + } + + @Test + public void testNoExtLangsLoading() throws IOException { + LanguageSubtagRegistry registry = new LanguageSubtagRegistry(); + LanguageSubtagRegistryLoader loader = new LanguageSubtagRegistryLoader(new LanguageSubtagRegistry()).withLoadExtLangs(false); + + + } + + private static Stream provideParamsForPartialLoad() { + return Stream.of( + Arguments.of(new LanguageSubtagRegistryLoader(new LanguageSubtagRegistry()).withLoadLanguages(false), true, false, false, false, false), + Arguments.of(new LanguageSubtagRegistryLoader(new LanguageSubtagRegistry()).withLoadExtLangs(false), false, true, false, false, false), + Arguments.of(new LanguageSubtagRegistryLoader(new LanguageSubtagRegistry()).withLoadRegions(false), false, false, true, false, false), + Arguments.of(new LanguageSubtagRegistryLoader(new LanguageSubtagRegistry()).withLoadVariants(false), false, false, false, true, false), + Arguments.of(new LanguageSubtagRegistryLoader(new LanguageSubtagRegistry()).withLoadScripts(false), false, false, false, false, true) + ); + } + + @ParameterizedTest + @MethodSource("provideParamsForPartialLoad") + public void testPartialLoad(LanguageSubtagRegistryLoader loader, + boolean languagesEmpty, + boolean extLangsEmpty, + boolean regionsEmpty, + boolean variantsEmpty, + boolean scriptsEmpty) throws IOException { + loader.loadFromDefaultResource(); + + assertEquals(languagesEmpty, loader.getRegistry().getLanguageKeys().isEmpty()); + assertEquals(extLangsEmpty, loader.getRegistry().getExtLangKeys().isEmpty()); + assertEquals(regionsEmpty, loader.getRegistry().getRegionKeys().isEmpty()); + assertEquals(variantsEmpty, loader.getRegistry().getVariantKeys().isEmpty()); + assertEquals(scriptsEmpty, loader.getRegistry().getScriptKeys().isEmpty()); + } }