-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDotPathQL.java
More file actions
113 lines (100 loc) · 3.61 KB
/
DotPathQL.java
File metadata and controls
113 lines (100 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package ca.trackerforce;
import ca.trackerforce.path.DotPathFactory;
import ca.trackerforce.path.api.DotPath;
import ca.trackerforce.path.api.DotPrinter;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* API for filtering and excluding properties from objects using dot paths.
*
* @author petruki
* @since 2025-08-02
*/
public class DotPathQL {
private final DotPath pathFilter;
private final DotPath pathExclude;
private final DotPrinter pathPrinter;
/**
* Constructs a DotPathQL instance with an empty list of default filter paths.
*/
public DotPathQL() {
pathFilter = DotPathFactory.buildFilter();
pathExclude = DotPathFactory.buildExclude();
pathPrinter = DotPathFactory.buildPrinter(2);
}
/**
* Filters the given source object based on the specified paths.
* The paths can include nested properties, collections, and arrays.
* Also supports grouped paths syntax like "parent[child1.prop,child2.prop]"
*
* @param <T> the type of the source object
* @param source the source object to filter
* @param filterPaths the list of paths to filter
* @return a map containing the filtered properties
*/
public <T> Map<String, Object> filter(T source, List<String> filterPaths) {
return pathFilter.run(source, filterPaths);
}
/**
* Excludes the given paths from the source object and returns the remaining structure.
* Works as the inverse of {@link #filter(Object, List)} – instead of selecting only
* specific paths, it returns all properties except the excluded ones.
* Supports the same grouped path syntax (e.g. "locations[home.street,work.city]").
*
* @param <T> the type of the source object
* @param source the source object to extract from
* @param excludePaths list of dot paths to exclude
* @return a map containing all properties except the excluded ones
*/
public <T> Map<String, Object> exclude(T source, List<String> excludePaths) {
return pathExclude.run(source, excludePaths);
}
/**
* Adds default filter paths that will be included in every filtering operation.
*
* @param paths the list of default filter paths to add
*/
public void addDefaultFilterPaths(List<String> paths) {
pathFilter.addDefaultPaths(paths);
}
/**
* Adds default exclude paths that will be included in every exclusion operation.
*
* @param paths the list of default exclude paths to add
*/
public void addDefaultExcludePaths(List<String> paths) {
pathExclude.addDefaultPaths(paths);
}
/**
* Converts the source object to a map representation.
*
* @param <T> the type of the source object
* @param source the source object to convert
* @return a map containing all properties of the source object
*/
public <T> Map<String, Object> toMap(T source) {
return exclude(source, Collections.emptyList());
}
/**
* Converts the given sourceMap to a JSON string representation with optional formatting.
*
* @param sourceMap the source map to convert to JSON
* @param indentSize the number of spaces to use for indentation
* @return a JSON string representation of the object
*/
public String toJson(Map<String, Object> sourceMap, int indentSize) {
pathPrinter.setIndentSize(indentSize);
return toJson(sourceMap, true);
}
/**
* Converts the given sourceMap to a JSON string representation.
*
* @param sourceMap the source map to convert to JSON
* @param prettier if true, formats with proper indentation; if false, compact single-line format
* @return a JSON string representation of the object
*/
public String toJson(Map<String, Object> sourceMap, boolean prettier) {
return pathPrinter.toJson(sourceMap, prettier);
}
}