Add options for partial loading

This commit is contained in:
dotasek 2024-03-20 13:39:12 -04:00
parent 77203326db
commit 90947c9f15
2 changed files with 91 additions and 10 deletions

View File

@ -1,5 +1,8 @@
package org.hl7.fhir.utilities.i18n.subtag; package org.hl7.fhir.utilities.i18n.subtag;
import lombok.Getter;
import lombok.With;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
@ -7,6 +10,24 @@ import java.util.*;
public class LanguageSubtagRegistryLoader { 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){ public static boolean isMultiField(String field){
return DESCRIPTION.equals(field) return DESCRIPTION.equals(field)
|| COMMENTS.equals(field) || COMMENTS.equals(field)
@ -58,12 +79,19 @@ public class LanguageSubtagRegistryLoader {
public static final String GRANDFATHERED = "grandfathered"; public static final String GRANDFATHERED = "grandfathered";
private final LanguageSubtagRegistry registry;
public LanguageSubtagRegistryLoader(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 { public void loadFromDefaultResource() throws IOException {
loadFromResource("lang.dat.txt"); loadFromResource("lang.dat.txt");
} }
@ -140,15 +168,15 @@ public class LanguageSubtagRegistryLoader {
protected void addSubtag(Subtag subtag) { protected void addSubtag(Subtag subtag) {
assert subtag.getSubtag() != null; assert subtag.getSubtag() != null;
if (subtag instanceof LanguageSubtag) if (subtag instanceof LanguageSubtag && loadLanguages)
registry.addLanguage(subtag.getSubtag(), (LanguageSubtag) subtag); registry.addLanguage(subtag.getSubtag(), (LanguageSubtag) subtag);
else if (subtag instanceof ExtLangSubtag) else if (subtag instanceof ExtLangSubtag && loadExtLangs )
registry.addExtLang(subtag.getSubtag(), (ExtLangSubtag) subtag); registry.addExtLang(subtag.getSubtag(), (ExtLangSubtag) subtag);
else if (subtag instanceof ScriptSubtag) else if (subtag instanceof ScriptSubtag && loadScripts)
registry.addScript(subtag.getSubtag(), (ScriptSubtag) subtag); registry.addScript(subtag.getSubtag(), (ScriptSubtag) subtag);
else if (subtag instanceof RegionSubtag) else if (subtag instanceof RegionSubtag && loadRegions)
registry.addRegion(subtag.getSubtag(), (RegionSubtag) subtag); registry.addRegion(subtag.getSubtag(), (RegionSubtag) subtag);
else if (subtag instanceof VariantSubtag) else if (subtag instanceof VariantSubtag && loadVariants)
registry.addVariant(subtag.getSubtag(), (VariantSubtag) subtag); registry.addVariant(subtag.getSubtag(), (VariantSubtag) subtag);
} }
protected Subtag processVariantRecord(Record record) { protected Subtag processVariantRecord(Record record) {

View File

@ -1,15 +1,19 @@
package org.hl7.fhir.utilities.i18n.subtag; package org.hl7.fhir.utilities.i18n.subtag;
import org.junit.jupiter.api.Test; 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.io.IOException;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
public class LanguageSubtagRegistryTest { public class LanguageSubtagRegistryLoaderTest {
@Test @Test
public void initializationTest() throws IOException { public void defaultLoaderTest() throws IOException {
/* /*
languages.size(): 8259 languages.size(): 8259
extLangs.size(): 253 extLangs.size(): 253
@ -21,7 +25,7 @@ public class LanguageSubtagRegistryTest {
LanguageSubtagRegistry registry = new LanguageSubtagRegistry(); LanguageSubtagRegistry registry = new LanguageSubtagRegistry();
LanguageSubtagRegistryLoader loader = new LanguageSubtagRegistryLoader(registry); LanguageSubtagRegistryLoader loader = new LanguageSubtagRegistryLoader(registry);
loader.loadFromDefaultResource(); loader.loadFromDefaultResource();
/* Test entries of every subtag type (language, script, variant, extLang, region) /* Test entries of every subtag type (language, script, variant, extLang, region)
These should cover both simple, and more complex entries with a larger number These should cover both simple, and more complex entries with a larger number
of fields. of fields.
@ -186,4 +190,53 @@ public class LanguageSubtagRegistryTest {
assertEquals("Honduras", hn.getDescriptions().get(0)); assertEquals("Honduras", hn.getDescriptions().get(0));
assertEquals("2005-10-16", hn.getAdded()); 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<Arguments> 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());
}
} }