From f0e85a3bda91ccff4ecc15735ad269310b1b29f9 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Tue, 4 May 2021 15:34:49 +0200 Subject: Make immutable --- .../prelude/query/parser/SpecialTokenRegistry.java | 98 ++++++---------------- 1 file changed, 24 insertions(+), 74 deletions(-) (limited to 'container-search/src/main/java/com/yahoo/prelude/query/parser/SpecialTokenRegistry.java') diff --git a/container-search/src/main/java/com/yahoo/prelude/query/parser/SpecialTokenRegistry.java b/container-search/src/main/java/com/yahoo/prelude/query/parser/SpecialTokenRegistry.java index f1798948361..9c735a031d7 100644 --- a/container-search/src/main/java/com/yahoo/prelude/query/parser/SpecialTokenRegistry.java +++ b/container-search/src/main/java/com/yahoo/prelude/query/parser/SpecialTokenRegistry.java @@ -1,7 +1,6 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.prelude.query.parser; -import com.yahoo.config.subscription.ConfigGetter; import com.yahoo.vespa.configdefinition.SpecialtokensConfig; import com.yahoo.vespa.configdefinition.SpecialtokensConfig.Tokenlist; import com.yahoo.vespa.configdefinition.SpecialtokensConfig.Tokenlist.Tokens; @@ -11,55 +10,36 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.logging.Logger; +import java.util.stream.Collectors; /** - * A registry which is responsible for knowing the current - * set of special tokens. The default registry returns empty token lists - * for all names. Usage of this registry is multithread safe. + * A registry which is responsible for knowing the current + * set of special tokens.Usage of this registry is multithread safe. * * @author bratseth */ public class SpecialTokenRegistry { - /** The log of this */ - private static final Logger log = Logger.getLogger(SpecialTokenRegistry.class.getName()); - - private static final SpecialTokens nullSpecialTokens = new SpecialTokens(); - /** * The current special token lists, indexed on name. * These lists are unmodifiable and used directly by clients of this */ - private Map specialTokenMap = new HashMap<>(); + private Map specialTokenMap; private boolean frozen = false; - /** - * Creates an empty special token registry which - * does not subscribe to any configuration - */ - public SpecialTokenRegistry() {} - - /** - * Create a special token registry which subscribes to the specialtokens - * configuration. Only used for testing. - */ - public SpecialTokenRegistry(String configId) { - try { - build(new ConfigGetter<>(SpecialtokensConfig.class).getConfig(configId)); - } catch (Exception e) { - log.config("No special tokens are configured (" + e.getMessage() + ")"); - } + /** Creates an empty special token registry */ + public SpecialTokenRegistry() { + this(List.of()); } - /** - * Create a special token registry from a configuration object. This is the production code path. - */ + /** Create a special token registry from a configuration object. */ public SpecialTokenRegistry(SpecialtokensConfig config) { - if (config != null) { - build(config); - } + this(specialTokensFrom(config)); + } + + public SpecialTokenRegistry(List specialTokensList) { + specialTokenMap = specialTokensList.stream().collect(Collectors.toMap(t -> t.name(), t -> t)); freeze(); } @@ -67,35 +47,19 @@ public class SpecialTokenRegistry { frozen = true; } - private void build(SpecialtokensConfig config) { - List list = new ArrayList<>(); + private static List specialTokensFrom(SpecialtokensConfig config) { + List specialTokensList = new ArrayList<>(); for (Iterator i = config.tokenlist().iterator(); i.hasNext();) { - Tokenlist tokenList = i.next(); - SpecialTokens tokens = new SpecialTokens(tokenList.name()); + Tokenlist tokenListConfig = i.next(); - for (Iterator j = tokenList.tokens().iterator(); j.hasNext();) { - Tokens token = j.next(); - tokens.addSpecialToken(token.token(), token.replace()); + List tokenList = new ArrayList<>(); + for (Iterator j = tokenListConfig.tokens().iterator(); j.hasNext();) { + Tokens tokenConfig = j.next(); + tokenList.add(new SpecialTokens.Token(tokenConfig.token(), tokenConfig.replace())); } - tokens.freeze(); - list.add(tokens); + specialTokensList.add(new SpecialTokens(tokenListConfig.name(), tokenList)); } - addSpecialTokens(list); - } - - /** - * Adds a SpecialTokens instance to the registry. That is, add the - * tokens contained for the name of the SpecialTokens instance - * given. - * - * @param specialTokens the SpecialTokens object to add - */ - public void addSpecialTokens(SpecialTokens specialTokens) { - ensureNotFrozen(); - List list = new ArrayList<>(); - list.add(specialTokens); - addSpecialTokens(list); - + return specialTokensList; } private void ensureNotFrozen() { @@ -104,14 +68,6 @@ public class SpecialTokenRegistry { } } - private void addSpecialTokens(List list) { - HashMap tokens = new HashMap<>(specialTokenMap); - for(SpecialTokens t: list) { - tokens.put(t.getName(),t); - } - specialTokenMap = tokens; - } - /** * Returns the list of special tokens for a given name. * @@ -122,15 +78,9 @@ public class SpecialTokenRegistry { * has no special tokens */ public SpecialTokens getSpecialTokens(String name) { - if (name == null || name.trim().equals("")) { + if (name == null || name.trim().equals("")) name = "default"; - } - SpecialTokens specialTokens = specialTokenMap.get(name); - - if (specialTokens == null) { - return nullSpecialTokens; - } - return specialTokens; + return specialTokenMap.getOrDefault(name, SpecialTokens.empty()); } } -- cgit v1.2.3