Skip to content

Commit c9f9785

Browse files
authored
chore: refactored APIs to reduce coupling (#6)
1 parent 94c1ca2 commit c9f9785

File tree

9 files changed

+132
-42
lines changed

9 files changed

+132
-42
lines changed

src/main/java/io/github/trackerforce/DotPathQL.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package io.github.trackerforce;
22

3+
import io.github.trackerforce.path.*;
4+
import io.github.trackerforce.path.api.DotPath;
5+
import io.github.trackerforce.path.api.DotPrinter;
6+
37
import java.util.Collections;
48
import java.util.List;
59
import java.util.Map;
@@ -13,17 +17,17 @@
1317
@SuppressWarnings("unchecked")
1418
public class DotPathQL {
1519

16-
private final PathCommon pathFilter;
17-
private final PathCommon pathExclude;
18-
private final PathPrinter pathPrinter;
20+
private final DotPath pathFilter;
21+
private final DotPath pathExclude;
22+
private final DotPrinter pathPrinter;
1923

2024
/**
2125
* Constructs a DotPathQL instance with an empty list of default filter paths.
2226
*/
2327
public DotPathQL() {
24-
pathFilter = new PathFilter();
25-
pathExclude = new PathExclude();
26-
pathPrinter = new PathPrinter(2);
28+
pathFilter = DotPathFactory.buildFilter();
29+
pathExclude = DotPathFactory.buildExclude();
30+
pathPrinter = DotPathFactory.buildPrinter(2);
2731
}
2832

2933
/**
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.github.trackerforce.path;
2+
3+
import io.github.trackerforce.path.api.DotPath;
4+
import io.github.trackerforce.path.api.DotPrinter;
5+
6+
/**
7+
* Factory class for creating instances of DotPath and DotPrinter implementations.
8+
*/
9+
public class DotPathFactory {
10+
11+
private DotPathFactory() { }
12+
13+
/**
14+
* Builds and returns a new instance of PathFilter.
15+
*
16+
* @return a new PathFilter instance
17+
*/
18+
public static DotPath buildFilter() {
19+
return new PathFilter();
20+
}
21+
22+
/**
23+
* Builds and returns a new instance of PathExclude.
24+
*
25+
* @return a new PathExclude instance
26+
*/
27+
public static DotPath buildExclude() {
28+
return new PathExclude();
29+
}
30+
31+
/**
32+
* Builds and returns a new instance of PathPrinter with the specified indentation size.
33+
*
34+
* @param indentSize the number of spaces to use for indentation
35+
* @return a new PathPrinter instance
36+
*/
37+
public static DotPrinter buildPrinter(int indentSize) {
38+
return new PathPrinter(indentSize);
39+
}
40+
}

src/main/java/io/github/trackerforce/ExclusionNode.java renamed to src/main/java/io/github/trackerforce/path/ExclusionNode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.trackerforce;
1+
package io.github.trackerforce.path;
22

33
import java.util.HashMap;
44
import java.util.Map;
@@ -7,6 +7,7 @@
77
class ExclusionNode {
88

99
private boolean excludeSelf; // If true, this exact path is excluded
10+
1011
private final Map<String, ExclusionNode> children;
1112

1213
public ExclusionNode() {

src/main/java/io/github/trackerforce/PathCommon.java renamed to src/main/java/io/github/trackerforce/path/PathCommon.java

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package io.github.trackerforce;
1+
package io.github.trackerforce.path;
2+
3+
import io.github.trackerforce.path.api.DotPath;
24

35
import java.lang.reflect.Field;
46
import java.lang.reflect.InvocationTargetException;
@@ -13,56 +15,44 @@
1315
* Provides methods to expand grouped paths, retrieve property values,
1416
* and manage default filter paths.
1517
*/
16-
abstract class PathCommon {
18+
abstract class PathCommon implements DotPath {
1719

1820
/**
1921
* Default paths that can be used across different implementations.
2022
*/
2123
protected final List<String> defaultPaths;
2224

2325
/**
24-
* Executes the path processing logic for the given source object.
25-
*
26-
* @param <T> the type of the source object
27-
* @param source the source object to process
28-
* @param filterPaths the list of paths to filter or exclude
29-
* @return a map containing the processed properties
26+
* Constructor to initialize the PathCommon with an empty list of default paths.
27+
* This allows subclasses to add their own default paths as needed.
3028
*/
31-
abstract <T> Map<String, Object> execute(T source, List<String> filterPaths);
29+
protected PathCommon() {
30+
this.defaultPaths = new ArrayList<>();
31+
}
3232

33-
/**
34-
* Runs the path processing logic for the given source object with the specified paths.
35-
*
36-
* @param <T> the type of the source object
37-
* @param source the source object to process
38-
* @param excludePaths the list of paths to exclude
39-
* @return a map containing the processed properties
40-
*/
41-
<T> Map<String, Object> run(T source, List<String> excludePaths) {
33+
@Override
34+
public <T> Map<String, Object> run(T source, List<String> paths) {
4235
if (source == null) {
4336
return Collections.emptyMap();
4437
}
4538

46-
return execute(source, expandGroupedPaths(excludePaths));
39+
return execute(source, expandGroupedPaths(paths));
4740
}
4841

49-
/**
50-
* Constructor to initialize the PathCommon with an empty list of default paths.
51-
* This allows subclasses to add their own default paths as needed.
52-
*/
53-
protected PathCommon() {
54-
this.defaultPaths = new ArrayList<>();
42+
@Override
43+
public void addDefaultPaths(List<String> paths) {
44+
defaultPaths.addAll(paths);
5545
}
5646

5747
/**
58-
* Adds default paths to the list of paths that can be used
59-
* when processing objects.
48+
* Executes the path processing logic for the given source object.
6049
*
61-
* @param paths the list of paths to add as default paths
50+
* @param <T> the type of the source object
51+
* @param source the source object to process
52+
* @param filterPaths the list of paths to filter or exclude
53+
* @return a map containing the processed properties
6254
*/
63-
public void addDefaultPaths(List<String> paths) {
64-
defaultPaths.addAll(paths);
65-
}
55+
abstract <T> Map<String, Object> execute(T source, List<String> filterPaths);
6656

6757
/**
6858
* Expands grouped paths like "parent[child1.prop,child2.prop]" into individual paths.

src/main/java/io/github/trackerforce/PathExclude.java renamed to src/main/java/io/github/trackerforce/path/PathExclude.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.trackerforce;
1+
package io.github.trackerforce.path;
22

33
import java.lang.reflect.Field;
44
import java.lang.reflect.Method;

src/main/java/io/github/trackerforce/PathFilter.java renamed to src/main/java/io/github/trackerforce/path/PathFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.trackerforce;
1+
package io.github.trackerforce.path;
22

33
import java.util.*;
44

src/main/java/io/github/trackerforce/PathPrinter.java renamed to src/main/java/io/github/trackerforce/path/PathPrinter.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
package io.github.trackerforce;
1+
package io.github.trackerforce.path;
2+
3+
import io.github.trackerforce.path.api.DotPrinter;
24

35
import java.lang.reflect.Array;
46
import java.util.List;
57
import java.util.Map;
68
import java.util.StringJoiner;
79

8-
class PathPrinter {
10+
class PathPrinter implements DotPrinter {
911

1012
private String indent;
1113

@@ -15,11 +17,13 @@ class PathPrinter {
1517
setIndentSize(indentSize);
1618
}
1719

20+
@Override
1821
public String toJson(Object obj, boolean prettier) {
1922
this.prettier = prettier;
2023
return toJsonInternal(obj, 0);
2124
}
2225

26+
@Override
2327
public void setIndentSize(int indentSize) {
2428
indent = " ".repeat(indentSize);
2529
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.github.trackerforce.path.api;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
/**
7+
* Defines common APIs for path processing
8+
*/
9+
public interface DotPath {
10+
11+
/**
12+
* Runs the path processing logic for the given source object with the specified paths.
13+
*
14+
* @param <T> the type of the source object
15+
* @param source the source object to process
16+
* @param paths the list of paths to exclude
17+
* @return a map containing the processed properties
18+
*/
19+
<T> Map<String, Object> run(T source, List<String> paths);
20+
21+
/**
22+
* Adds default paths to the list of paths that can be used
23+
* when processing objects.
24+
*
25+
* @param paths the list of paths to add as default paths
26+
*/
27+
void addDefaultPaths(List<String> paths);
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.github.trackerforce.path.api;
2+
3+
/**
4+
* Defines common APIs for printing objects to JSON format.
5+
*/
6+
public interface DotPrinter {
7+
8+
/**
9+
* Converts the given object to its JSON representation.
10+
*
11+
* @param obj the object to convert to JSON
12+
* @param prettier if true, the JSON output will be formatted with indentation and line breaks
13+
* @return a JSON string representation of the object
14+
*/
15+
String toJson(Object obj, boolean prettier);
16+
17+
/**
18+
* Sets the number of spaces to use for indentation in the JSON output.
19+
*
20+
* @param indentSize the number of spaces for indentation
21+
*/
22+
void setIndentSize(int indentSize);
23+
}

0 commit comments

Comments
 (0)