From 27f8736d392d0c306ee61dc4d6684efbaa2b6a74 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Sat, 17 Feb 2018 15:21:04 +0100 Subject: Remove dead code --- .../com/yahoo/searchdefinition/FeatureNames.java | 86 ++-------------------- .../searchdefinition/FeatureNamesTestCase.java | 11 --- 2 files changed, 8 insertions(+), 89 deletions(-) (limited to 'config-model') diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/FeatureNames.java b/config-model/src/main/java/com/yahoo/searchdefinition/FeatureNames.java index 3db818c4a85..659e027586f 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/FeatureNames.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/FeatureNames.java @@ -22,75 +22,6 @@ public class FeatureNames { private static final Pattern identifierRegexp = Pattern.compile("[A-Za-z0-9_][A-Za-z0-9_-]*"); - /** - *

Returns the given query, document or constant feature in canonical form. - * A feature name consists of a feature type name (query, attribute or constant), - * followed by one argument enclosed in quotes. - * The argument may be an identifier or any string single or double quoted.

- * - *

Argument string values may not contain comma, single quote nor double quote characters.

- * - *

The canonical form use no quotes for arguments which are identifiers, and double quotes otherwise.

- * - *

Note that the above definition is not true for features in general, which accept any ranking expression - * as argument.

- * - * @throws IllegalArgumentException if the feature name is not valid - */ - // Note that this implementation is more general than what is described above: - // It accepts any number of arguments and an optional output - public static String canonicalize(String feature) { - return canonicalizeIfValid(feature).orElseThrow(() -> - new IllegalArgumentException("A feature name must be on the form query(name), attribute(name) or " + - "constant(name), but was '" + feature + "'" - )); - } - - /** - * Canonicalizes the given argument as in canonicalize, but returns empty instead of throwing an exception if - * the argument is not a valid feature - */ - public static Optional canonicalizeIfValid(String feature) { - int startParenthesis = feature.indexOf('('); - if (startParenthesis < 0) - return Optional.empty(); - int endParenthesis = feature.lastIndexOf(')'); - String featureType = feature.substring(0, startParenthesis); - if ( ! ( featureType.equals("query") || featureType.equals("attribute") || featureType.equals("constant"))) - return Optional.empty(); - if (startParenthesis < 1) return Optional.of(feature); // No arguments - if (endParenthesis < startParenthesis) - return Optional.empty(); - String argumentString = feature.substring(startParenthesis + 1, endParenthesis); - List canonicalizedArguments = - Arrays.stream(argumentString.split(",")) - .map(FeatureNames::canonicalizeArgument) - .collect(Collectors.toList()); - return Optional.of(featureType + "(" + - canonicalizedArguments.stream().collect(Collectors.joining(",")) + - feature.substring(endParenthesis)); - } - - /** Canonicalizes a single argument */ - private static String canonicalizeArgument(String argument) { - if (argument.startsWith("'")) { - if ( ! argument.endsWith("'")) - throw new IllegalArgumentException("Feature arguments starting by a single quote " + - "must end by a single quote, but was \"" + argument + "\""); - argument = argument.substring(1, argument.length() - 1); - } - if (argument.startsWith("\"")) { - if ( ! argument.endsWith("\"")) - throw new IllegalArgumentException("Feature arguments starting by a double quote " + - "must end by a double quote, but was '" + argument + "'"); - argument = argument.substring(1, argument.length() - 1); - } - if (identifierRegexp.matcher(argument).matches()) - return argument; - else - return "\"" + argument + "\""; - } - public static ReferenceNode.Reference asConstantFeature(String constantName) { return ReferenceNode.Reference.simple("constant", quoteIfNecessary(constantName)); } @@ -108,15 +39,14 @@ public class FeatureNames { * or empty if it is not a valid query, attribute or constant feature name */ public static Optional argumentOf(String feature) { - return canonicalizeIfValid(feature).map(f -> { - int startParenthesis = f.indexOf("("); - int endParenthesis = f.indexOf(")"); - String possiblyQuotedArgument = f.substring(startParenthesis + 1, endParenthesis); - if (possiblyQuotedArgument.startsWith("\"")) - return possiblyQuotedArgument.substring(1, possiblyQuotedArgument.length() - 1); - else - return possiblyQuotedArgument; - }); + Optional reference = ReferenceNode.Reference.simple(feature); + if ( ! reference.isPresent()) return Optional.empty(); + if ( ! ( reference.get().name().equals("attribute") || + reference.get().name().equals("constant") || + reference.get().name().equals("query"))) + return Optional.empty(); + + return Optional.of(reference.get().arguments().expressions().get(0).toString()); } private static String quoteIfNecessary(String s) { diff --git a/config-model/src/test/java/com/yahoo/searchdefinition/FeatureNamesTestCase.java b/config-model/src/test/java/com/yahoo/searchdefinition/FeatureNamesTestCase.java index 182aaba5be8..aa01070d296 100644 --- a/config-model/src/test/java/com/yahoo/searchdefinition/FeatureNamesTestCase.java +++ b/config-model/src/test/java/com/yahoo/searchdefinition/FeatureNamesTestCase.java @@ -17,17 +17,6 @@ import static org.junit.Assert.assertFalse; */ public class FeatureNamesTestCase { - @Test - public void testCanonicalization() { - assertFalse(FeatureNames.canonicalizeIfValid("foo").isPresent()); - assertEquals("query(bar)", FeatureNames.canonicalize("query(bar)")); - assertEquals("query(bar)", FeatureNames.canonicalize("query('bar')")); - assertEquals("constant(bar)", FeatureNames.canonicalize("constant(\"bar\")")); - assertEquals("query(\"ba.r\")", FeatureNames.canonicalize("query(ba.r)")); - assertEquals("query(\"ba.r\")", FeatureNames.canonicalize("query('ba.r')")); - assertEquals("attribute(\"ba.r\")", FeatureNames.canonicalize("attribute(\"ba.r\")")); - } - @Test public void testArgument() { assertFalse(FeatureNames.argumentOf("foo(bar)").isPresent()); -- cgit v1.2.3