|
| 1 | +/* |
| 2 | + * Copyright 2016-2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.support; |
| 17 | + |
| 18 | +import java.util.Collection; |
| 19 | + |
| 20 | +import org.springframework.data.domain.ExampleMatcher; |
| 21 | +import org.springframework.data.domain.ExampleMatcher.PropertySpecifier; |
| 22 | +import org.springframework.data.util.StringMatcher; |
| 23 | +import org.springframework.util.Assert; |
| 24 | + |
| 25 | +/** |
| 26 | + * Accessor for the {@link ExampleMatcher} to use in modules that support query by example (QBE) querying. |
| 27 | + * |
| 28 | + * @author Mark Paluch |
| 29 | + * @author Oliver Gierke |
| 30 | + * @author Christoph Strobl |
| 31 | + * @author Jens Schauder |
| 32 | + * @since 1.12 |
| 33 | + */ |
| 34 | +public class ExampleMatcherAccessor { |
| 35 | + |
| 36 | + private final ExampleMatcher matcher; |
| 37 | + |
| 38 | + /** |
| 39 | + * Creates a new {@link ExampleMatcherAccessor} for the given {@link ExampleMatcher}. |
| 40 | + * |
| 41 | + * @param matcher must not be {@literal null}. |
| 42 | + */ |
| 43 | + public ExampleMatcherAccessor(ExampleMatcher matcher) { |
| 44 | + |
| 45 | + Assert.notNull(matcher, "ExampleMatcher must not be null!"); |
| 46 | + |
| 47 | + this.matcher = matcher; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Returns the {@link PropertySpecifier}s of the underlying {@link ExampleMatcher}. |
| 52 | + * |
| 53 | + * @return unmodifiable {@link Collection} of {@link ExampleMatcher.PropertySpecifier}s. |
| 54 | + */ |
| 55 | + public Collection<ExampleMatcher.PropertySpecifier> getPropertySpecifiers() { |
| 56 | + return matcher.getPropertySpecifiers().getSpecifiers(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns whether the underlying {@link ExampleMatcher} contains a {@link PropertySpecifier} for the given path. |
| 61 | + * |
| 62 | + * @param path the dot-path identifying a property. |
| 63 | + * @return {@literal true} in case {@link ExampleMatcher.PropertySpecifier} defined for given path. |
| 64 | + */ |
| 65 | + public boolean hasPropertySpecifier(String path) { |
| 66 | + return matcher.getPropertySpecifiers().hasSpecifierForPath(path); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Get the {@link ExampleMatcher.PropertySpecifier} for given path. <br /> |
| 71 | + * Please check if {@link #hasPropertySpecifier(String)} to avoid running into {@literal null} values. |
| 72 | + * |
| 73 | + * @param path Dot-Path to property. |
| 74 | + * @return {@literal null} when no {@link ExampleMatcher.PropertySpecifier} defined for path. |
| 75 | + */ |
| 76 | + public ExampleMatcher.PropertySpecifier getPropertySpecifier(String path) { |
| 77 | + return matcher.getPropertySpecifiers().getForPath(path); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @return true if at least one {@link ExampleMatcher.PropertySpecifier} defined. |
| 82 | + */ |
| 83 | + public boolean hasPropertySpecifiers() { |
| 84 | + return matcher.getPropertySpecifiers().hasValues(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Get the {@link ExampleMatcher.StringMatcher} for a given path or return the default one if none defined. |
| 89 | + * |
| 90 | + * @param path |
| 91 | + * @return never {@literal null}. |
| 92 | + */ |
| 93 | + public ExampleMatcher.StringMatcher getStringMatcherForPath(String path) { |
| 94 | + |
| 95 | + if (!hasPropertySpecifier(path)) { |
| 96 | + return matcher.getDefaultStringMatcher(); |
| 97 | + } |
| 98 | + |
| 99 | + ExampleMatcher.PropertySpecifier specifier = getPropertySpecifier(path); |
| 100 | + return specifier.getStringMatcher() != null ? specifier.getStringMatcher() : matcher.getDefaultStringMatcher(); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Get defined null handling. |
| 105 | + * |
| 106 | + * @return never {@literal null} |
| 107 | + */ |
| 108 | + public ExampleMatcher.NullHandler getNullHandler() { |
| 109 | + return matcher.getNullHandler(); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Get defined {@link ExampleMatcher.StringMatcher}. |
| 114 | + * |
| 115 | + * @return never {@literal null}. |
| 116 | + */ |
| 117 | + public ExampleMatcher.StringMatcher getDefaultStringMatcher() { |
| 118 | + return matcher.getDefaultStringMatcher(); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @return {@literal true} if {@link String} should be matched with ignore case option. |
| 123 | + */ |
| 124 | + public boolean isIgnoreCaseEnabled() { |
| 125 | + return matcher.isIgnoreCaseEnabled(); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @param path |
| 130 | + * @return return {@literal true} if path was set to be ignored. |
| 131 | + */ |
| 132 | + public boolean isIgnoredPath(String path) { |
| 133 | + return matcher.isIgnoredPath(path); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Get the ignore case flag for a given path or return the default one if none defined. |
| 138 | + * |
| 139 | + * @param path |
| 140 | + * @return never {@literal null}. |
| 141 | + */ |
| 142 | + public boolean isIgnoreCaseForPath(String path) { |
| 143 | + |
| 144 | + if (!hasPropertySpecifier(path)) { |
| 145 | + return matcher.isIgnoreCaseEnabled(); |
| 146 | + } |
| 147 | + |
| 148 | + ExampleMatcher.PropertySpecifier specifier = getPropertySpecifier(path); |
| 149 | + return specifier.getIgnoreCase() != null ? specifier.getIgnoreCase() : matcher.isIgnoreCaseEnabled(); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Get the ignore case flag for a given path or return {@link ExampleMatcher.NoOpPropertyValueTransformer} if none |
| 154 | + * defined. |
| 155 | + * |
| 156 | + * @param path |
| 157 | + * @return never {@literal null}. |
| 158 | + */ |
| 159 | + public ExampleMatcher.PropertyValueTransformer getValueTransformerForPath(String path) { |
| 160 | + |
| 161 | + if (!hasPropertySpecifier(path)) { |
| 162 | + return ExampleMatcher.NoOpPropertyValueTransformer.INSTANCE; |
| 163 | + } |
| 164 | + |
| 165 | + return getPropertySpecifier(path).getPropertyValueTransformer(); |
| 166 | + } |
| 167 | +} |
0 commit comments