|
45 | 45 |
|
46 | 46 | import java.util.ArrayList;
|
47 | 47 | import java.util.Arrays;
|
| 48 | +import java.util.Collections; |
48 | 49 | import java.util.HashMap;
|
| 50 | +import java.util.HashSet; |
49 | 51 | import java.util.List;
|
50 | 52 | import java.util.Map;
|
| 53 | +import java.util.Set; |
51 | 54 | import java.util.function.Function;
|
52 | 55 |
|
53 | 56 | /**
|
@@ -216,6 +219,54 @@ public static Map<String, Object> filter(Map<String, ?> map, String[] includes,
|
216 | 219 | * @see #filter(Map, String[], String[]) for details
|
217 | 220 | */
|
218 | 221 | public static Function<Map<String, ?>, Map<String, Object>> filter(String[] includes, String[] excludes) {
|
| 222 | + if (hasNoWildcardsOrDots(includes) && hasNoWildcardsOrDots(excludes)) { |
| 223 | + return createSetBasedFilter(includes, excludes); |
| 224 | + } |
| 225 | + return createAutomatonFilter(includes, excludes); |
| 226 | + } |
| 227 | + |
| 228 | + private static boolean hasNoWildcardsOrDots(String[] fields) { |
| 229 | + if (fields == null || fields.length == 0) { |
| 230 | + return true; |
| 231 | + } |
| 232 | + |
| 233 | + for (String field : fields) { |
| 234 | + if (field.indexOf('*') != -1 || field.indexOf('.') != -1) { |
| 235 | + return false; |
| 236 | + } |
| 237 | + } |
| 238 | + return true; |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * Creates a simple HashSet-based filter for exact field name matching |
| 243 | + */ |
| 244 | + private static Function<Map<String, ?>, Map<String, Object>> createSetBasedFilter(String[] includes, String[] excludes) { |
| 245 | + Set<String> includeSet = (includes == null || includes.length == 0) ? null : new HashSet<>(Arrays.asList(includes)); |
| 246 | + Set<String> excludeSet = (excludes == null || excludes.length == 0) |
| 247 | + ? Collections.emptySet() |
| 248 | + : new HashSet<>(Arrays.asList(excludes)); |
| 249 | + |
| 250 | + return (map) -> { |
| 251 | + Map<String, Object> filtered = new HashMap<>(); |
| 252 | + for (Map.Entry<String, ?> entry : map.entrySet()) { |
| 253 | + String key = entry.getKey(); |
| 254 | + int dotPos = key.indexOf('.'); |
| 255 | + if (dotPos > 0) { |
| 256 | + key = key.substring(0, dotPos); |
| 257 | + } |
| 258 | + if ((includeSet == null || includeSet.contains(key)) && !excludeSet.contains(key)) { |
| 259 | + filtered.put(entry.getKey(), entry.getValue()); |
| 260 | + } |
| 261 | + } |
| 262 | + return filtered; |
| 263 | + }; |
| 264 | + } |
| 265 | + |
| 266 | + /** |
| 267 | + * Creates an automaton-based filter for complex pattern matching |
| 268 | + */ |
| 269 | + public static Function<Map<String, ?>, Map<String, Object>> createAutomatonFilter(String[] includes, String[] excludes) { |
219 | 270 | CharacterRunAutomaton matchAllAutomaton = new CharacterRunAutomaton(Automata.makeAnyString());
|
220 | 271 |
|
221 | 272 | CharacterRunAutomaton include;
|
|
0 commit comments