String clause; final ExampleMatcher.PropertySpecifier specifier = example.getMatcher().getPropertySpecifiers() .getForPath(fullPath); if (specifier != null && value != null) { value = specifier.transformValue(Optional.of(value)).orElse(null);
private <T> void validateExample(Example<T> example) { Assert.notNull(example, "A non-null example is expected"); ExampleMatcher matcher = example.getMatcher(); if (!matcher.isAllMatching()) { throw new DatastoreDataException("Unsupported MatchMode. Only MatchMode.ALL is supported"); } if (matcher.isIgnoreCaseEnabled()) { throw new DatastoreDataException("Ignore case matching is not supported"); } if (!(matcher.getDefaultStringMatcher() == ExampleMatcher.StringMatcher.EXACT || matcher.getDefaultStringMatcher() == ExampleMatcher.StringMatcher.DEFAULT)) { throw new DatastoreDataException("Unsupported StringMatcher. Only EXACT and DEFAULT are supported"); } Optional<String> path = example.getMatcher().getIgnoredPaths().stream().filter((s) -> s.contains(".")).findFirst(); if (path.isPresent()) { throw new DatastoreDataException("Ignored paths deeper than 1 are not supported"); } if (matcher.getPropertySpecifiers().hasValues()) { throw new DatastoreDataException("Property matchers are not supported"); } } }
@Override public ExampleMatcher withMatcher(String propertyPath, GenericPropertyMatcher genericPropertyMatcher) { Assert.hasText(propertyPath, "PropertyPath must not be empty!"); Assert.notNull(genericPropertyMatcher, "GenericPropertyMatcher must not be empty!"); PropertySpecifiers propertySpecifiers = new PropertySpecifiers(this.propertySpecifiers); PropertySpecifier propertySpecifier = new PropertySpecifier(propertyPath); if (genericPropertyMatcher.ignoreCase != null) { propertySpecifier = propertySpecifier.withIgnoreCase(genericPropertyMatcher.ignoreCase); } if (genericPropertyMatcher.stringMatcher != null) { propertySpecifier = propertySpecifier.withStringMatcher(genericPropertyMatcher.stringMatcher); } propertySpecifier = propertySpecifier.withValueTransformer(genericPropertyMatcher.valueTransformer); propertySpecifiers.add(propertySpecifier); return new TypedExampleMatcher(nullHandler, defaultStringMatcher, propertySpecifiers, ignoredPaths, defaultIgnoreCase, mode); }
String clause; final ExampleMatcher.PropertySpecifier specifier = example.getMatcher().getPropertySpecifiers() .getForPath(fullPath); if (specifier != null && value != null) { value = specifier.transformValue(Optional.of(value)).orElse(null);
/** * This method will create an invocation that had it occurred on a query method would provide sufficient * data for a parsed query method expression to be evaluated * @param descriptor the query descriptor * @param example the example that is used for evaluation * @return the fake method invocation corresponding to the example probe */ private Invocation createInvocation(QueryDescriptor descriptor, Example example) { final List<Object> values = new ArrayList<>(); //since according to http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example //the matcher only supports AND condition, so, we expect to see only one branch final List<List<Parameter>> branches = descriptor.getBranches(); final List<Parameter> parameters = branches.get(0); for (Parameter parameter : parameters) { final String propertyPath = parameter.getPath(); final Object propertyValue = PropertyUtils.getPropertyValue(example.getProbe(), propertyPath); final ExampleMatcher.PropertySpecifier specifier = example.getMatcher().getPropertySpecifiers().getForPath(propertyPath); values.add(specifier == null ? propertyValue : specifier.getPropertyValueTransformer().convert(propertyValue)); } return new ImmutableInvocation(null, values.toArray()); }
/** * This method will create an invocation that had it occurred on a query method would provide sufficient * data for a parsed query method expression to be evaluated * @param descriptor the query descriptor * @param example the example that is used for evaluation * @return the fake method invocation corresponding to the example probe */ private Invocation createInvocation(QueryDescriptor descriptor, Example example) { final List<Object> values = new ArrayList<>(); //since according to http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example //the matcher only supports AND condition, so, we expect to see only one branch final List<List<Parameter>> branches = descriptor.getBranches(); final List<Parameter> parameters = branches.get(0); for (Parameter parameter : parameters) { final String propertyPath = parameter.getPath(); final Object propertyValue = PropertyUtils.getPropertyValue(example.getProbe(), propertyPath); final ExampleMatcher.PropertySpecifier specifier = example.getMatcher().getPropertySpecifiers().getForPath(propertyPath); values.add(specifier == null ? propertyValue : specifier.getPropertyValueTransformer().convert(propertyValue)); } return new ImmutableInvocation(null, values.toArray()); }
private boolean ignoreCase(ExampleMatcher matcher, String path) { final ExampleMatcher.PropertySpecifier specifier = matcher.getPropertySpecifiers().getForPath(path); return matcher.isIgnoreCaseEnabled() || specifier != null && Boolean.TRUE.equals(specifier.getIgnoreCase()); }
private ExampleMatcher.StringMatcher stringMatcher(ExampleMatcher matcher, String path) { final ExampleMatcher.PropertySpecifier specifier = matcher.getPropertySpecifiers().getForPath(path); return specifier != null ? specifier.getStringMatcher() : matcher.getDefaultStringMatcher(); }
TypedExampleMatcher() { this(NullHandler.IGNORE, StringMatcher.DEFAULT, new PropertySpecifiers(), Collections.emptySet(), false, MatchMode.ALL); }
private ExampleMatcher.StringMatcher stringMatcher(ExampleMatcher matcher, String path) { final ExampleMatcher.PropertySpecifier specifier = matcher.getPropertySpecifiers().getForPath(path); return specifier != null ? specifier.getStringMatcher() : matcher.getDefaultStringMatcher(); }
/** * Get the {@link ExampleMatcher.PropertySpecifier} for given path. <br /> * Please check if {@link #hasPropertySpecifier(String)} to avoid running into {@literal null} values. * * @param path Dot-Path to property. * @return {@literal null} when no {@link ExampleMatcher.PropertySpecifier} defined for path. */ public ExampleMatcher.PropertySpecifier getPropertySpecifier(String path) { return matcher.getPropertySpecifiers().getForPath(path); }
private boolean ignoreCase(ExampleMatcher matcher, String path) { final ExampleMatcher.PropertySpecifier specifier = matcher.getPropertySpecifiers().getForPath(path); return matcher.isIgnoreCaseEnabled() || specifier != null && Boolean.TRUE.equals(specifier.getIgnoreCase()); }
/** * Returns the {@link PropertySpecifier}s of the underlying {@link ExampleMatcher}. * * @return unmodifiable {@link Collection} of {@link ExampleMatcher.PropertySpecifier}s. */ public Collection<ExampleMatcher.PropertySpecifier> getPropertySpecifiers() { return matcher.getPropertySpecifiers().getSpecifiers(); }
/** * @return true if at least one {@link ExampleMatcher.PropertySpecifier} defined. */ public boolean hasPropertySpecifiers() { return matcher.getPropertySpecifiers().hasValues(); }
/** * Returns whether the underlying {@link ExampleMatcher} contains a {@link PropertySpecifier} for the given path. * * @param path the dot-path identifying a property. * @return {@literal true} in case {@link ExampleMatcher.PropertySpecifier} defined for given path. */ public boolean hasPropertySpecifier(String path) { return matcher.getPropertySpecifiers().hasSpecifierForPath(path); }
@Override public ExampleMatcher withIgnoreCase(String... propertyPaths) { Assert.notEmpty(propertyPaths, "PropertyPaths must not be empty!"); Assert.noNullElements(propertyPaths, "PropertyPaths must not contain null elements!"); PropertySpecifiers propertySpecifiers = new PropertySpecifiers(this.propertySpecifiers); for (String propertyPath : propertyPaths) { PropertySpecifier propertySpecifier = getOrCreatePropertySpecifier(propertyPath, propertySpecifiers); propertySpecifiers.add(propertySpecifier.withIgnoreCase(true)); } return new TypedExampleMatcher(nullHandler, defaultStringMatcher, propertySpecifiers, ignoredPaths, defaultIgnoreCase, mode); }
@Override public ExampleMatcher withTransformer(String propertyPath, PropertyValueTransformer propertyValueTransformer) { Assert.hasText(propertyPath, "PropertyPath must not be empty!"); Assert.notNull(propertyValueTransformer, "PropertyValueTransformer must not be empty!"); PropertySpecifiers propertySpecifiers = new PropertySpecifiers(this.propertySpecifiers); PropertySpecifier propertySpecifier = getOrCreatePropertySpecifier(propertyPath, propertySpecifiers); propertySpecifiers.add(propertySpecifier.withValueTransformer(propertyValueTransformer)); return new TypedExampleMatcher(nullHandler, defaultStringMatcher, propertySpecifiers, ignoredPaths, defaultIgnoreCase, mode); }
private PropertySpecifier getOrCreatePropertySpecifier(String propertyPath, PropertySpecifiers propertySpecifiers) { if (propertySpecifiers.hasSpecifierForPath(propertyPath)) { return propertySpecifiers.getForPath(propertyPath); } return new PropertySpecifier(propertyPath); } }