@Test public void does_not_allow_more_than_one_preferential_parameter_type_for_each_regexp() { registry.defineParameterType(new ParameterType<>("name", CAPITALISED_WORD, Name.class, Name::new, false, true)); registry.defineParameterType(new ParameterType<>("person", CAPITALISED_WORD, Person.class, Person::new, false, false)); final Executable testMethod = () -> registry.defineParameterType(new ParameterType<>( "place", CAPITALISED_WORD, Place.class, Place::new, false, true )); final CucumberExpressionException thrownException = assertThrows(CucumberExpressionException.class, testMethod); assertThat("Unexpected message", thrownException.getMessage(), is(equalTo("There can only be one preferential parameter type per regexp. The regexp /[A-Z]+\\w+/ is used for two preferential parameter types, {name} and {place}"))); }
public void defineParameterType(ParameterType<?> parameterType) { if (parameterType.getName() != null) { if (parameterTypeByName.containsKey(parameterType.getName())) { if (parameterType.getName().isEmpty()) { throw new DuplicateTypeNameException("The anonymous parameter type has already been defined"); } throw new DuplicateTypeNameException(String.format("There is already a parameter type with name %s", parameterType.getName())); } parameterTypeByName.put(parameterType.getName(), parameterType); } for (String parameterTypeRegexp : parameterType.getRegexps()) { if (!parameterTypesByRegexp.containsKey(parameterTypeRegexp)) { parameterTypesByRegexp.put(parameterTypeRegexp, new TreeSet<ParameterType<?>>()); } SortedSet<ParameterType<?>> parameterTypes = parameterTypesByRegexp.get(parameterTypeRegexp); if (!parameterTypes.isEmpty() && parameterTypes.first().preferForRegexpMatch() && parameterType.preferForRegexpMatch()) { throw new CucumberExpressionException(String.format( "There can only be one preferential parameter type per regexp. " + "The regexp /%s/ is used for two preferential parameter types, {%s} and {%s}", parameterTypeRegexp, parameterTypes.first().getName(), parameterType.getName() )); } parameterTypes.add(parameterType); } }
ParameterTypeRegistry { public void defineParameterType(ParameterType<?> parameterType) { if (parameterType.getName() != null) { if (parameterTypeByName.containsKey(parameterType.getName())) { if (parameterType.getName().isEmpty()) { throw new DuplicateTypeNameException("The anonymous parameter type has already been defined"); } throw new DuplicateTypeNameException(String.format("There is already a parameter type with name %s", parameterType.getName())); } parameterTypeByName.put(parameterType.getName(), parameterType); } for (String parameterTypeRegexp : parameterType.getRegexps()) { if (!parameterTypesByRegexp.containsKey(parameterTypeRegexp)) { parameterTypesByRegexp.put(parameterTypeRegexp, new TreeSet<ParameterType<?>>()); } SortedSet<ParameterType<?>> parameterTypes = parameterTypesByRegexp.get(parameterTypeRegexp); if (!parameterTypes.isEmpty() && parameterTypes.first().preferForRegexpMatch() && parameterType.preferForRegexpMatch()) { throw new CucumberExpressionException(String.format( "There can only be one preferential parameter type per regexp. " + "The regexp /%s/ is used for two preferential parameter types, {%s} and {%s}", parameterTypeRegexp, parameterTypes.first().getName(), parameterType.getName() )); } parameterTypes.add(parameterType); } } }
ParameterTypeRegistry { public void defineParameterType(ParameterType<?> parameterType) { if (parameterType.getName() != null) { if (parameterTypeByName.containsKey(parameterType.getName())) { if (parameterType.getName().isEmpty()) { throw new DuplicateTypeNameException("The anonymous parameter type has already been defined"); } throw new DuplicateTypeNameException(String.format("There is already a parameter type with name %s", parameterType.getName())); } parameterTypeByName.put(parameterType.getName(), parameterType); } for (String parameterTypeRegexp : parameterType.getRegexps()) { if (!parameterTypesByRegexp.containsKey(parameterTypeRegexp)) { parameterTypesByRegexp.put(parameterTypeRegexp, new TreeSet<ParameterType<?>>()); } SortedSet<ParameterType<?>> parameterTypes = parameterTypesByRegexp.get(parameterTypeRegexp); if (!parameterTypes.isEmpty() && parameterTypes.first().preferForRegexpMatch() && parameterType.preferForRegexpMatch()) { throw new CucumberExpressionException(String.format( "There can only be one preferential parameter type per regexp. " + "The regexp /%s/ is used for two preferential parameter types, {%s} and {%s}", parameterTypeRegexp, parameterTypes.first().getName(), parameterType.getName() )); } parameterTypes.add(parameterType); } } ParameterTypeRegistry(Locale locale); private  ParameterTypeRegistry(ParameterByTypeTransformer defaultParameterTransformer, Locale locale); }
ParameterTypeRegistry { public void defineParameterType(ParameterType<?> parameterType) { if (parameterType.getName() != null) { if (parameterTypeByName.containsKey(parameterType.getName())) { if (parameterType.getName().isEmpty()) { throw new DuplicateTypeNameException("The anonymous parameter type has already been defined"); } throw new DuplicateTypeNameException(String.format("There is already a parameter type with name %s", parameterType.getName())); } parameterTypeByName.put(parameterType.getName(), parameterType); } for (String parameterTypeRegexp : parameterType.getRegexps()) { if (!parameterTypesByRegexp.containsKey(parameterTypeRegexp)) { parameterTypesByRegexp.put(parameterTypeRegexp, new TreeSet<ParameterType<?>>()); } SortedSet<ParameterType<?>> parameterTypes = parameterTypesByRegexp.get(parameterTypeRegexp); if (!parameterTypes.isEmpty() && parameterTypes.first().preferForRegexpMatch() && parameterType.preferForRegexpMatch()) { throw new CucumberExpressionException(String.format( "There can only be one preferential parameter type per regexp. " + "The regexp /%s/ is used for two preferential parameter types, {%s} and {%s}", parameterTypeRegexp, parameterTypes.first().getName(), parameterType.getName() )); } parameterTypes.add(parameterType); } } ParameterTypeRegistry(Locale locale); private  ParameterTypeRegistry(ParameterByTypeTransformer defaultParameterTransformer, Locale locale); void defineParameterType(ParameterType<?> parameterType); void setDefaultParameterTransformer(ParameterByTypeTransformer defaultParameterTransformer); }
ParameterTypeRegistry { public void defineParameterType(ParameterType<?> parameterType) { if (parameterType.getName() != null) { if (parameterTypeByName.containsKey(parameterType.getName())) { if (parameterType.getName().isEmpty()) { throw new DuplicateTypeNameException("The anonymous parameter type has already been defined"); } throw new DuplicateTypeNameException(String.format("There is already a parameter type with name %s", parameterType.getName())); } parameterTypeByName.put(parameterType.getName(), parameterType); } for (String parameterTypeRegexp : parameterType.getRegexps()) { if (!parameterTypesByRegexp.containsKey(parameterTypeRegexp)) { parameterTypesByRegexp.put(parameterTypeRegexp, new TreeSet<ParameterType<?>>()); } SortedSet<ParameterType<?>> parameterTypes = parameterTypesByRegexp.get(parameterTypeRegexp); if (!parameterTypes.isEmpty() && parameterTypes.first().preferForRegexpMatch() && parameterType.preferForRegexpMatch()) { throw new CucumberExpressionException(String.format( "There can only be one preferential parameter type per regexp. " + "The regexp /%s/ is used for two preferential parameter types, {%s} and {%s}", parameterTypeRegexp, parameterTypes.first().getName(), parameterType.getName() )); } parameterTypes.add(parameterType); } } ParameterTypeRegistry(Locale locale); private  ParameterTypeRegistry(ParameterByTypeTransformer defaultParameterTransformer, Locale locale); void defineParameterType(ParameterType<?> parameterType); void setDefaultParameterTransformer(ParameterByTypeTransformer defaultParameterTransformer);  }