From 0164e9b88dbf111c6e851de7fd0a1ae7fa569b02 Mon Sep 17 00:00:00 2001 From: Brian Clements Date: Wed, 2 Apr 2014 11:09:42 -0500 Subject: [PATCH] Issue #2906 change util.mapping to return empty sets instead of null Former-commit-id: c150d74ce77018813227ce991341e7b00d3442b8 [formerly 2d6be840c82c5a47811b2bb318651bde75597540] [formerly c150d74ce77018813227ce991341e7b00d3442b8 [formerly 2d6be840c82c5a47811b2bb318651bde75597540] [formerly 29ace2a204f22d3d99b5c98fa4aa8b7a67f0859d [formerly 9cf62e8ac95ef7cc1010d436eb0e7ed990d1e3d3]]] Former-commit-id: 29ace2a204f22d3d99b5c98fa4aa8b7a67f0859d Former-commit-id: 2942e86e420abf9075c312402d99b0eefd7a0e73 [formerly a42417c17345e02b5a27635766f09827842cc1ff] Former-commit-id: c47ff60f9615a05dea60c7120be74d1bf1eb42bf --- .../common/util/mapping/AliasNamespace.java | 21 +++++-- .../uf/common/util/mapping/Mapper.java | 61 +++++++++++++------ 2 files changed, 57 insertions(+), 25 deletions(-) diff --git a/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/mapping/AliasNamespace.java b/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/mapping/AliasNamespace.java index b94b0853bc..e8a907d2b8 100644 --- a/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/mapping/AliasNamespace.java +++ b/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/mapping/AliasNamespace.java @@ -28,10 +28,10 @@ import java.util.Set; /** * * Represents a bidirectional map for going between base names and aliases. - * Allows for caseInsensitive aliases since some naming conventions are ambigous - * on case. The base names cannot be treated case insensitive because this would - * cause ambiguity and require case insensitive handling of base names in all - * namespaces. + * Allows for caseInsensitive aliases since some naming conventions are + * ambiguous on case. The base names cannot be treated case insensitive because + * this would cause ambiguity and require case insensitive handling of base + * names in all namespaces. * *
  * 
@@ -40,6 +40,7 @@ import java.util.Set;
  * Date         Ticket#    Engineer    Description
  * ------------ ---------- ----------- --------------------------
  * Mar 22, 2012            bsteffen     Initial creation
+ * Apr 02, 2014 2906       bclement     changed to return empty set instead of null for lookup methods
  * 
  * 
* @@ -86,21 +87,29 @@ public class AliasNamespace { } } + /** + * @param alias + * @return empty set if no mapping from alias to base is found + */ public Set lookupBaseNames(String alias) { if (!caseSensitive) { alias = alias.toLowerCase(); } Set base = alias2base.get(alias); if (base == null) { - return base; + return Collections.emptySet(); } return Collections.unmodifiableSet(base); } + /** + * @param base + * @return empty set if no mapping from base to aliases is found + */ public Set lookupAliases(String base) { Set alias = base2alias.get(base); if (alias == null) { - return alias; + return Collections.emptySet(); } return Collections.unmodifiableSet(alias); } diff --git a/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/mapping/Mapper.java b/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/mapping/Mapper.java index 6dfd6b8232..14924a5235 100644 --- a/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/mapping/Mapper.java +++ b/edexOsgi/com.raytheon.uf.common.util/src/com/raytheon/uf/common/util/mapping/Mapper.java @@ -53,6 +53,7 @@ import javax.xml.bind.Unmarshaller; * Date Ticket# Engineer Description * ------------ ---------- ----------- -------------------------- * Mar 22, 2012 bsteffen Initial creation + * Apr 02, 2014 2906 bclement changed to return empty set instead of null for lookup methods * * * @@ -97,6 +98,13 @@ public abstract class Mapper { Mapper.unmarshaller = unmarshaller; } + /** + * @param alias + * @param namespace + * @param defaultUseAlias + * default to populating return with alias if no base names found + * @return empty set if no base names found and defaultUseAlias is false + */ protected Set lookupBaseNames(String alias, String namespace, boolean defaultUseAlias) { AliasNamespace list = namespaceMap.get(namespace); @@ -108,7 +116,7 @@ public abstract class Mapper { if (defaultUseAlias) { baseNames = new HashSet(Arrays.asList(alias)); } else { - return null; + return Collections.emptySet(); } } AliasNamespace deprecated = namespaceMap.get(DEPRECATED); @@ -148,18 +156,27 @@ public abstract class Mapper { /** * Lookup all the baseNames associated with the given alias in a namespace. - * If no baseNames are defined null is returned. + * If no baseNames are defined an empty set is returned. * * @param namespace * - the defined alias namespace to look for the name * @param alias * - the name of an alias defined in the namespace - * @return the base names or null if the namespace or alias is undefined + * @return the base names or an empty set if the namespace or alias is + * undefined */ - public Set lookupBaseNamesOrNull(String alias, String namespace) { + public Set lookupBaseNamesOrEmpty(String alias, String namespace) { return lookupBaseNames(alias, namespace, false); } + /** + * @param base + * @param namespace + * @param defaultUseBase + * default to populating return with base if no aliases found + * @return empty set if no aliases found and defaultUseBase is false + * @return + */ protected Set lookupAliases(String base, String namespace, boolean defaultUseBase) { AliasNamespace ns = namespaceMap.get(namespace); @@ -167,7 +184,7 @@ public abstract class Mapper { if (ns != null) { aliases = ns.lookupAliases(base); } - if (aliases == null) { + if (aliases == null || aliases.isEmpty()) { AliasNamespace deprecated = namespaceMap.get(DEPRECATED); if (deprecated != null) { Set depNames = deprecated.lookupAliases(base); @@ -186,8 +203,12 @@ public abstract class Mapper { } } } - if (aliases == null && defaultUseBase) { - aliases = new HashSet(Arrays.asList(base)); + if (aliases == null || aliases.isEmpty()) { + if (defaultUseBase) { + aliases = new HashSet(Arrays.asList(base)); + } else { + aliases = Collections.emptySet(); + } } } return aliases; @@ -209,15 +230,15 @@ public abstract class Mapper { /** * Lookup an alias name within a given namespace for a base name. If no - * alias is defined then null is returned + * alias is defined then an empty set is returned * * @param parameter * - The base name to find an alias for * @param namespace * - The namespace in which to look for an alias. - * @return an alias abbreviation or null if none is found. + * @return an alias abbreviation or an empty set if none is found. */ - public Set lookupAliasesOrNull(String base, String namespace) { + public Set lookupAliasesOrEmpty(String base, String namespace) { return lookupAliases(base, namespace, false); } @@ -255,7 +276,7 @@ public abstract class Mapper { public String lookupAlias(String base, String namespace) throws MultipleMappingException { Set aliases = lookupAliases(base, namespace); - if (aliases == null) { + if (aliases == null || aliases.isEmpty()) { return null; } else if (aliases.size() == 1) { return aliases.iterator().next(); @@ -265,18 +286,19 @@ public abstract class Mapper { } /** - * Provides same functionality as lookupBaseNamesOrNull but is more + * Provides same functionality as lookupBaseNamesOrEmpty but is more * convenient when only alias is expected. * * @param alias * @param namespace - * @return + * @return null if no mapping from alias to base name is found * @throws MultipleMappingException + * if more than one base name is found for alias */ public String lookupBaseNameOrNull(String alias, String namespace) throws MultipleMappingException { - Set baseNames = lookupBaseNamesOrNull(alias, namespace); - if (baseNames == null || baseNames.isEmpty()) { + Set baseNames = lookupBaseNamesOrEmpty(alias, namespace); + if (baseNames.isEmpty()) { return null; } else if (baseNames.size() == 1) { return baseNames.iterator().next(); @@ -287,17 +309,18 @@ public abstract class Mapper { } /** - * Provides same functionality as lookupAliasesOrNull but is more convenient - * when only one base name is expected. + * Provides same functionality as lookupAliasesOrEmpty but is more + * convenient when only one base name is expected. * * @param base * @param namespace - * @return + * @return null if no mapping from base to alias is found * @throws MultipleMappingException + * if more than one alias is found for base */ public String lookupAliasOrNull(String base, String namespace) throws MultipleMappingException { - Set aliases = lookupAliasesOrNull(base, namespace); + Set aliases = lookupAliasesOrEmpty(base, namespace); if (aliases == null) { return null; } else if (aliases.size() == 1) {