1   package org.daisy.pipeline.braille.tex.impl;
2   
3   import java.net.URI;
4   import java.util.Locale;
5   
6   import org.daisy.pipeline.braille.common.Provider;
7   import static org.daisy.pipeline.braille.common.Provider.util.dispatch;
8   import static org.daisy.pipeline.braille.common.Provider.util.memoize;
9   import static org.daisy.pipeline.braille.common.Provider.util.varyLocale;
10  import org.daisy.pipeline.braille.common.ResourceRegistry;
11  
12  import org.daisy.pipeline.braille.tex.TexHyphenatorTablePath;
13  
14  import org.osgi.service.component.annotations.Component;
15  import org.osgi.service.component.annotations.Reference;
16  import org.osgi.service.component.annotations.ReferenceCardinality;
17  import org.osgi.service.component.annotations.ReferencePolicy;
18  
19  @Component(
20  	name = "org.daisy.pipeline.braille.tex.impl.TexHyphenatorTableRegistry",
21  	service = {
22  		TexHyphenatorTableRegistry.class
23  	}
24  )
25  public class TexHyphenatorTableRegistry extends ResourceRegistry<TexHyphenatorTablePath> implements Provider<Locale,URI> {
26  	
27  	@Reference(
28  		name = "TexHyphenatorTablePath",
29  		unbind = "-",
30  		service = TexHyphenatorTablePath.class,
31  		cardinality = ReferenceCardinality.MULTIPLE,
32  		policy = ReferencePolicy.STATIC
33  	)
34  	protected void _register(TexHyphenatorTablePath path) {
35  		register(path);
36  		provider.invalidateCache();
37  	}
38  	
39  	protected void _unregister (TexHyphenatorTablePath path) {
40  		unregister(path);
41  		provider.invalidateCache();
42  	}
43  	
44  	/**
45  	 * Try to find a table based on the given locale.
46  	 * An automatic fallback mechanism is used: if nothing is found for
47  	 * language-COUNTRY-variant, then language-COUNTRY is searched, then language.
48  	 */
49  	public Iterable<URI> get(Locale locale) {
50  		return provider.get(locale);
51  	}
52  	
53  	private final Provider.util.MemoizingProvider<Locale,URI> provider
54  		= memoize(
55  			varyLocale(
56  				dispatch(paths.values())));
57  	
58  }