diff --git a/src/main/java/io/github/trackerforce/DotPathQL.java b/src/main/java/io/github/trackerforce/DotPathQL.java index 12f10ea..411090e 100644 --- a/src/main/java/io/github/trackerforce/DotPathQL.java +++ b/src/main/java/io/github/trackerforce/DotPathQL.java @@ -1,5 +1,9 @@ package io.github.trackerforce; +import io.github.trackerforce.path.*; +import io.github.trackerforce.path.api.DotPath; +import io.github.trackerforce.path.api.DotPrinter; + import java.util.Collections; import java.util.List; import java.util.Map; @@ -13,17 +17,17 @@ @SuppressWarnings("unchecked") public class DotPathQL { - private final PathCommon pathFilter; - private final PathCommon pathExclude; - private final PathPrinter pathPrinter; + 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 = new PathFilter(); - pathExclude = new PathExclude(); - pathPrinter = new PathPrinter(2); + pathFilter = DotPathFactory.buildFilter(); + pathExclude = DotPathFactory.buildExclude(); + pathPrinter = DotPathFactory.buildPrinter(2); } /** diff --git a/src/main/java/io/github/trackerforce/path/DotPathFactory.java b/src/main/java/io/github/trackerforce/path/DotPathFactory.java new file mode 100644 index 0000000..2ebcd41 --- /dev/null +++ b/src/main/java/io/github/trackerforce/path/DotPathFactory.java @@ -0,0 +1,40 @@ +package io.github.trackerforce.path; + +import io.github.trackerforce.path.api.DotPath; +import io.github.trackerforce.path.api.DotPrinter; + +/** + * Factory class for creating instances of DotPath and DotPrinter implementations. + */ +public class DotPathFactory { + + private DotPathFactory() { } + + /** + * Builds and returns a new instance of PathFilter. + * + * @return a new PathFilter instance + */ + public static DotPath buildFilter() { + return new PathFilter(); + } + + /** + * Builds and returns a new instance of PathExclude. + * + * @return a new PathExclude instance + */ + public static DotPath buildExclude() { + return new PathExclude(); + } + + /** + * Builds and returns a new instance of PathPrinter with the specified indentation size. + * + * @param indentSize the number of spaces to use for indentation + * @return a new PathPrinter instance + */ + public static DotPrinter buildPrinter(int indentSize) { + return new PathPrinter(indentSize); + } +} diff --git a/src/main/java/io/github/trackerforce/ExclusionNode.java b/src/main/java/io/github/trackerforce/path/ExclusionNode.java similarity index 93% rename from src/main/java/io/github/trackerforce/ExclusionNode.java rename to src/main/java/io/github/trackerforce/path/ExclusionNode.java index 467cefd..2ff5046 100644 --- a/src/main/java/io/github/trackerforce/ExclusionNode.java +++ b/src/main/java/io/github/trackerforce/path/ExclusionNode.java @@ -1,4 +1,4 @@ -package io.github.trackerforce; +package io.github.trackerforce.path; import java.util.HashMap; import java.util.Map; @@ -7,6 +7,7 @@ class ExclusionNode { private boolean excludeSelf; // If true, this exact path is excluded + private final Map children; public ExclusionNode() { diff --git a/src/main/java/io/github/trackerforce/PathCommon.java b/src/main/java/io/github/trackerforce/path/PathCommon.java similarity index 93% rename from src/main/java/io/github/trackerforce/PathCommon.java rename to src/main/java/io/github/trackerforce/path/PathCommon.java index 2782118..58fd626 100644 --- a/src/main/java/io/github/trackerforce/PathCommon.java +++ b/src/main/java/io/github/trackerforce/path/PathCommon.java @@ -1,4 +1,6 @@ -package io.github.trackerforce; +package io.github.trackerforce.path; + +import io.github.trackerforce.path.api.DotPath; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; @@ -13,7 +15,7 @@ * Provides methods to expand grouped paths, retrieve property values, * and manage default filter paths. */ -abstract class PathCommon { +abstract class PathCommon implements DotPath { /** * Default paths that can be used across different implementations. @@ -21,48 +23,36 @@ abstract class PathCommon { protected final List defaultPaths; /** - * Executes the path processing logic for the given source object. - * - * @param the type of the source object - * @param source the source object to process - * @param filterPaths the list of paths to filter or exclude - * @return a map containing the processed properties + * Constructor to initialize the PathCommon with an empty list of default paths. + * This allows subclasses to add their own default paths as needed. */ - abstract Map execute(T source, List filterPaths); + protected PathCommon() { + this.defaultPaths = new ArrayList<>(); + } - /** - * Runs the path processing logic for the given source object with the specified paths. - * - * @param the type of the source object - * @param source the source object to process - * @param excludePaths the list of paths to exclude - * @return a map containing the processed properties - */ - Map run(T source, List excludePaths) { + @Override + public Map run(T source, List paths) { if (source == null) { return Collections.emptyMap(); } - return execute(source, expandGroupedPaths(excludePaths)); + return execute(source, expandGroupedPaths(paths)); } - /** - * Constructor to initialize the PathCommon with an empty list of default paths. - * This allows subclasses to add their own default paths as needed. - */ - protected PathCommon() { - this.defaultPaths = new ArrayList<>(); + @Override + public void addDefaultPaths(List paths) { + defaultPaths.addAll(paths); } /** - * Adds default paths to the list of paths that can be used - * when processing objects. + * Executes the path processing logic for the given source object. * - * @param paths the list of paths to add as default paths + * @param the type of the source object + * @param source the source object to process + * @param filterPaths the list of paths to filter or exclude + * @return a map containing the processed properties */ - public void addDefaultPaths(List paths) { - defaultPaths.addAll(paths); - } + abstract Map execute(T source, List filterPaths); /** * Expands grouped paths like "parent[child1.prop,child2.prop]" into individual paths. diff --git a/src/main/java/io/github/trackerforce/PathExclude.java b/src/main/java/io/github/trackerforce/path/PathExclude.java similarity index 99% rename from src/main/java/io/github/trackerforce/PathExclude.java rename to src/main/java/io/github/trackerforce/path/PathExclude.java index 438fe9d..2e2a357 100644 --- a/src/main/java/io/github/trackerforce/PathExclude.java +++ b/src/main/java/io/github/trackerforce/path/PathExclude.java @@ -1,4 +1,4 @@ -package io.github.trackerforce; +package io.github.trackerforce.path; import java.lang.reflect.Field; import java.lang.reflect.Method; diff --git a/src/main/java/io/github/trackerforce/PathFilter.java b/src/main/java/io/github/trackerforce/path/PathFilter.java similarity index 98% rename from src/main/java/io/github/trackerforce/PathFilter.java rename to src/main/java/io/github/trackerforce/path/PathFilter.java index 7e18313..4e12586 100644 --- a/src/main/java/io/github/trackerforce/PathFilter.java +++ b/src/main/java/io/github/trackerforce/path/PathFilter.java @@ -1,4 +1,4 @@ -package io.github.trackerforce; +package io.github.trackerforce.path; import java.util.*; diff --git a/src/main/java/io/github/trackerforce/PathPrinter.java b/src/main/java/io/github/trackerforce/path/PathPrinter.java similarity index 96% rename from src/main/java/io/github/trackerforce/PathPrinter.java rename to src/main/java/io/github/trackerforce/path/PathPrinter.java index 96dc0b3..560d946 100644 --- a/src/main/java/io/github/trackerforce/PathPrinter.java +++ b/src/main/java/io/github/trackerforce/path/PathPrinter.java @@ -1,11 +1,13 @@ -package io.github.trackerforce; +package io.github.trackerforce.path; + +import io.github.trackerforce.path.api.DotPrinter; import java.lang.reflect.Array; import java.util.List; import java.util.Map; import java.util.StringJoiner; -class PathPrinter { +class PathPrinter implements DotPrinter { private String indent; @@ -15,11 +17,13 @@ class PathPrinter { setIndentSize(indentSize); } + @Override public String toJson(Object obj, boolean prettier) { this.prettier = prettier; return toJsonInternal(obj, 0); } + @Override public void setIndentSize(int indentSize) { indent = " ".repeat(indentSize); } diff --git a/src/main/java/io/github/trackerforce/path/api/DotPath.java b/src/main/java/io/github/trackerforce/path/api/DotPath.java new file mode 100644 index 0000000..2c22a5e --- /dev/null +++ b/src/main/java/io/github/trackerforce/path/api/DotPath.java @@ -0,0 +1,28 @@ +package io.github.trackerforce.path.api; + +import java.util.List; +import java.util.Map; + +/** + * Defines common APIs for path processing + */ +public interface DotPath { + + /** + * Runs the path processing logic for the given source object with the specified paths. + * + * @param the type of the source object + * @param source the source object to process + * @param paths the list of paths to exclude + * @return a map containing the processed properties + */ + Map run(T source, List paths); + + /** + * Adds default paths to the list of paths that can be used + * when processing objects. + * + * @param paths the list of paths to add as default paths + */ + void addDefaultPaths(List paths); +} diff --git a/src/main/java/io/github/trackerforce/path/api/DotPrinter.java b/src/main/java/io/github/trackerforce/path/api/DotPrinter.java new file mode 100644 index 0000000..9b314f2 --- /dev/null +++ b/src/main/java/io/github/trackerforce/path/api/DotPrinter.java @@ -0,0 +1,23 @@ +package io.github.trackerforce.path.api; + +/** + * Defines common APIs for printing objects to JSON format. + */ +public interface DotPrinter { + + /** + * Converts the given object to its JSON representation. + * + * @param obj the object to convert to JSON + * @param prettier if true, the JSON output will be formatted with indentation and line breaks + * @return a JSON string representation of the object + */ + String toJson(Object obj, boolean prettier); + + /** + * Sets the number of spaces to use for indentation in the JSON output. + * + * @param indentSize the number of spaces for indentation + */ + void setIndentSize(int indentSize); +}