@Test public void uses_two_type_hints_to_resolve_anonymous_parameter_type() { List<?> match = match(compile("a (.*) and a (.*)"), "a 22 and a 33.5", Float.class, Double.class); assertEquals(Float.class, match.get(0).getClass()); assertEquals(22f, (Float) match.get(0), 0.00001); assertEquals(Double.class, match.get(1).getClass()); assertEquals(33.5d, (Double) match.get(1), 0.00001); }
@Override public List<Argument<?>> match(String text, Type... typeHints) { final Group group = treeRegexp.match(text); if (group == null) { return null; } final ParameterByTypeTransformer defaultTransformer = parameterTypeRegistry.getDefaultParameterTransformer(); final List<ParameterType<?>> parameterTypes = new ArrayList<>(); int typeHintIndex = 0; for (GroupBuilder groupBuilder : treeRegexp.getGroupBuilder().getChildren()) { final String parameterTypeRegexp = groupBuilder.getSource(); boolean hasTypeHint = typeHintIndex < typeHints.length; final Type typeHint = hasTypeHint ? typeHints[typeHintIndex++] : String.class; ParameterType<?> parameterType = parameterTypeRegistry.lookupByRegexp(parameterTypeRegexp, expressionRegexp, text); if (parameterType != null && hasTypeHint && !parameterType.useRegexpMatchAsStrongTypeHint()) { if (!parameterType.getType().equals(typeHint)) { parameterType = null; } } if (parameterType == null) { parameterType = createAnonymousParameterType(parameterTypeRegexp); } if (parameterType.isAnonymous()) { parameterType = parameterType.deAnonymize(typeHint, arg -> defaultTransformer.transform(arg, typeHint)); } parameterTypes.add(parameterType); } return Argument.build(group, treeRegexp, parameterTypes); }
RegularExpression implements Expression { @Override public List<Argument<?>> match(String text, Type... typeHints) { final Group group = treeRegexp.match(text); if (group == null) { return null; } final ParameterByTypeTransformer defaultTransformer = parameterTypeRegistry.getDefaultParameterTransformer(); final List<ParameterType<?>> parameterTypes = new ArrayList<>(); int typeHintIndex = 0; for (GroupBuilder groupBuilder : treeRegexp.getGroupBuilder().getChildren()) { final String parameterTypeRegexp = groupBuilder.getSource(); boolean hasTypeHint = typeHintIndex < typeHints.length; final Type typeHint = hasTypeHint ? typeHints[typeHintIndex++] : String.class; ParameterType<?> parameterType = parameterTypeRegistry.lookupByRegexp(parameterTypeRegexp, expressionRegexp, text); if (parameterType != null && hasTypeHint && !parameterType.useRegexpMatchAsStrongTypeHint()) { if (!parameterType.getType().equals(typeHint)) { parameterType = null; } } if (parameterType == null) { parameterType = createAnonymousParameterType(parameterTypeRegexp); } if (parameterType.isAnonymous()) { parameterType = parameterType.deAnonymize(typeHint, arg -> defaultTransformer.transform(arg, typeHint)); } parameterTypes.add(parameterType); } return Argument.build(group, treeRegexp, parameterTypes); } }
RegularExpression implements Expression { @Override public List<Argument<?>> match(String text, Type... typeHints) { final Group group = treeRegexp.match(text); if (group == null) { return null; } final ParameterByTypeTransformer defaultTransformer = parameterTypeRegistry.getDefaultParameterTransformer(); final List<ParameterType<?>> parameterTypes = new ArrayList<>(); int typeHintIndex = 0; for (GroupBuilder groupBuilder : treeRegexp.getGroupBuilder().getChildren()) { final String parameterTypeRegexp = groupBuilder.getSource(); boolean hasTypeHint = typeHintIndex < typeHints.length; final Type typeHint = hasTypeHint ? typeHints[typeHintIndex++] : String.class; ParameterType<?> parameterType = parameterTypeRegistry.lookupByRegexp(parameterTypeRegexp, expressionRegexp, text); if (parameterType != null && hasTypeHint && !parameterType.useRegexpMatchAsStrongTypeHint()) { if (!parameterType.getType().equals(typeHint)) { parameterType = null; } } if (parameterType == null) { parameterType = createAnonymousParameterType(parameterTypeRegexp); } if (parameterType.isAnonymous()) { parameterType = parameterType.deAnonymize(typeHint, arg -> defaultTransformer.transform(arg, typeHint)); } parameterTypes.add(parameterType); } return Argument.build(group, treeRegexp, parameterTypes); } RegularExpression(Pattern expressionRegexp, ParameterTypeRegistry parameterTypeRegistry); }
RegularExpression implements Expression { @Override public List<Argument<?>> match(String text, Type... typeHints) { final Group group = treeRegexp.match(text); if (group == null) { return null; } final ParameterByTypeTransformer defaultTransformer = parameterTypeRegistry.getDefaultParameterTransformer(); final List<ParameterType<?>> parameterTypes = new ArrayList<>(); int typeHintIndex = 0; for (GroupBuilder groupBuilder : treeRegexp.getGroupBuilder().getChildren()) { final String parameterTypeRegexp = groupBuilder.getSource(); boolean hasTypeHint = typeHintIndex < typeHints.length; final Type typeHint = hasTypeHint ? typeHints[typeHintIndex++] : String.class; ParameterType<?> parameterType = parameterTypeRegistry.lookupByRegexp(parameterTypeRegexp, expressionRegexp, text); if (parameterType != null && hasTypeHint && !parameterType.useRegexpMatchAsStrongTypeHint()) { if (!parameterType.getType().equals(typeHint)) { parameterType = null; } } if (parameterType == null) { parameterType = createAnonymousParameterType(parameterTypeRegexp); } if (parameterType.isAnonymous()) { parameterType = parameterType.deAnonymize(typeHint, arg -> defaultTransformer.transform(arg, typeHint)); } parameterTypes.add(parameterType); } return Argument.build(group, treeRegexp, parameterTypes); } RegularExpression(Pattern expressionRegexp, ParameterTypeRegistry parameterTypeRegistry); @Override List<Argument<?>> match(String text, Type... typeHints); @Override Pattern getRegexp(); @Override String getSource(); }
RegularExpression implements Expression { @Override public List<Argument<?>> match(String text, Type... typeHints) { final Group group = treeRegexp.match(text); if (group == null) { return null; } final ParameterByTypeTransformer defaultTransformer = parameterTypeRegistry.getDefaultParameterTransformer(); final List<ParameterType<?>> parameterTypes = new ArrayList<>(); int typeHintIndex = 0; for (GroupBuilder groupBuilder : treeRegexp.getGroupBuilder().getChildren()) { final String parameterTypeRegexp = groupBuilder.getSource(); boolean hasTypeHint = typeHintIndex < typeHints.length; final Type typeHint = hasTypeHint ? typeHints[typeHintIndex++] : String.class; ParameterType<?> parameterType = parameterTypeRegistry.lookupByRegexp(parameterTypeRegexp, expressionRegexp, text); if (parameterType != null && hasTypeHint && !parameterType.useRegexpMatchAsStrongTypeHint()) { if (!parameterType.getType().equals(typeHint)) { parameterType = null; } } if (parameterType == null) { parameterType = createAnonymousParameterType(parameterTypeRegexp); } if (parameterType.isAnonymous()) { parameterType = parameterType.deAnonymize(typeHint, arg -> defaultTransformer.transform(arg, typeHint)); } parameterTypes.add(parameterType); } return Argument.build(group, treeRegexp, parameterTypes); } RegularExpression(Pattern expressionRegexp, ParameterTypeRegistry parameterTypeRegistry); @Override List<Argument<?>> match(String text, Type... typeHints); @Override Pattern getRegexp(); @Override String getSource();  }