au.id.pbw.hyfo.hyph
Class HyphenationTreeCache

java.lang.Object
  extended by au.id.pbw.hyfo.hyph.HyphenationTreeCache

public class HyphenationTreeCache
extends Object

A singleton class which caches instances of HyphenationTree.

Such instances are located using a Locale-style identifier. Such identifiers are made up of one or more components. The identifier is normalized before a search. The search proceeds by looking for an instance matching all components, in order, then stripping the last component and searching again, until a match is found, or the components are exhausted. The results, including failures, for all searches are cached to eliminate unnecessary searches.

Each of thee search components is a prefix of the original identifier.

Aliases may be added to the HyphenationTreeCache, but all cached information will be cleared when an alias is installed. If, for example, trees for en_US and en_GB are available, it may be desirable to add an alias for en, to enable a shortened Locale to be associated with a hyphenation tree. There is otherwise no reliable way to form such an association.

Author:
pbw

Field Summary
protected  Logger log
          The Logger instance for the cache.
 
Method Summary
 HyphenationTree add_alias(String alias, String locale)
          Adds the given alias for the given Locale-like string, and returns the HyphenationTree corresponding to the Locale, if one exists.
protected  HyphenationTree check_prefix(String locale, String prefix, List<String> not_found_yet)
          Gets the HyphenationTree matched by the the prefix of locale.
protected  HyphenationTree failure(String id, List<String> not_found_yet)
          Adds all of the elements of the argument list to the failed map, and returns null.
static HyphenationTreeCache get_cache_instance()
          Returns the singleton instance of the cache, creating an instance if one does not currently exist.
 HyphenationTree get_hyphenation_tree(String locale)
          Gets the HyphenationTree corresponding to the given locale argument, or null if none is found.
protected  String next_prefix(String super_prefix)
          Returns the next prefix of super_prefix, derived by stripping the last underscore-separated element off super_prefix.
protected  String normalized_locale(String locale)
          Normalizes the Locale-like identifier.
 void reset_aliases()
          Creates a new, empty instance of aliases.
 void reset_cache_searches()
          Creates new, empty instances of matches, superstring_matches and failed.
 void reset_cache()
          Creates new, empty instances of matches, superstring_matches, failed and aliases.
protected  HyphenationTree success(String locale, String prefix, HyphenationTree tree, List<String> not_found_yet)
          Adds the prefix, mapped to locale, to the matches map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected Logger log
The Logger instance for the cache.

Method Detail

get_cache_instance

public static HyphenationTreeCache get_cache_instance()
Returns the singleton instance of the cache, creating an instance if one does not currently exist.

Returns:
the cache singleton.

get_hyphenation_tree

public HyphenationTree get_hyphenation_tree(String locale)
                                     throws IOException
Gets the HyphenationTree corresponding to the given locale argument, or null if none is found.

Parameters:
locale - the Locale-like identifier to serach for.
Returns:
the corresponding HyphenationTree or null.
Throws:
IOException - if an IO error occurs durng the search for a tree.

check_prefix

protected HyphenationTree check_prefix(String locale,
                                       String prefix,
                                       List<String> not_found_yet)
                                throws IOException
Gets the HyphenationTree matched by the the prefix of locale. If such a match is found, adds the prefix, mapped to locale, to the matches map. Adds all elements of not_found_yet to superstring_matches, mapped to prefix. If no such match is found, returns null.

Parameters:
locale - the original Locale-like identifier.
prefix - the prefix of locale which is being checked.
not_found_yet - list of super-prefixes of prefix which have not been matched.
Returns:
the matched HyphenationTree or null, if no match for this prefix.
Throws:
IOException - if the attempt to deserialize a matched HyphenationTree fails.

next_prefix

protected String next_prefix(String super_prefix)
Returns the next prefix of super_prefix, derived by stripping the last underscore-separated element off super_prefix. If there is no such prefix, returns null.

Parameters:
super_prefix - the original prefix to be stripped fo its last element.
Returns:
the stripped prefix or null.

success

protected HyphenationTree success(String locale,
                                  String prefix,
                                  HyphenationTree tree,
                                  List<String> not_found_yet)
Adds the prefix, mapped to locale, to the matches map. Adds all elements of not_found_yet to superstring_matches, mapped to prefix. Returns tree.

Parameters:
locale - the original Locale-like identifier.
prefix - the matched prefix of locale.
tree - the HyphenationTree matched by prefix.
not_found_yet - the list of super-prefixes of prefix for which no match was found.
Returns:
tree.

failure

protected HyphenationTree failure(String id,
                                  List<String> not_found_yet)
Adds all of the elements of the argument list to the failed map, and returns null.

Parameters:
id - the original search Locale-like string.
not_found_yet - the list of prefixes of id for which a match wsa not found; that is, all preixes.
Returns:
Null.

add_alias

public HyphenationTree add_alias(String alias,
                                 String locale)
                          throws IOException
Adds the given alias for the given Locale-like string, and returns the HyphenationTree corresponding to the Locale, if one exists. If not, the alias is not added, and null is returned.

Parameters:
alias - the alias to add.
locale - the string to map alias to.
Returns:
the HyphenationTree corresponding to the Locale, or null.
Throws:
IOException - if an IO error occurs while trying to locate the aliased tree.

reset_cache_searches

public void reset_cache_searches()
Creates new, empty instances of matches, superstring_matches and failed.


reset_aliases

public void reset_aliases()
Creates a new, empty instance of aliases.


reset_cache

public void reset_cache()
Creates new, empty instances of matches, superstring_matches, failed and aliases.


normalized_locale

protected String normalized_locale(String locale)
Normalizes the Locale-like identifier. All instances of '-' are replaced with an underscore. Note that no case changing occurs.

Parameters:
locale - the string to normalize.
Returns:
the normalized string.


Copyright © 2005-2006 Peter B. West.