Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/main/java/io/github/trackerforce/DotPathQL.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}

/**
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/io/github/trackerforce/path/DotPathFactory.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.trackerforce;
package io.github.trackerforce.path;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -7,6 +7,7 @@
class ExclusionNode {

private boolean excludeSelf; // If true, this exact path is excluded

private final Map<String, ExclusionNode> children;

public ExclusionNode() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,56 +15,44 @@
* 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.
*/
protected final List<String> defaultPaths;

/**
* Executes the path processing logic for the given source object.
*
* @param <T> 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 <T> Map<String, Object> execute(T source, List<String> filterPaths);
protected PathCommon() {
this.defaultPaths = new ArrayList<>();
}

/**
* Runs the path processing logic for the given source object with the specified paths.
*
* @param <T> 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
*/
<T> Map<String, Object> run(T source, List<String> excludePaths) {
@Override
public <T> Map<String, Object> run(T source, List<String> 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<String> 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 <T> 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<String> paths) {
defaultPaths.addAll(paths);
}
abstract <T> Map<String, Object> execute(T source, List<String> filterPaths);

/**
* Expands grouped paths like "parent[child1.prop,child2.prop]" into individual paths.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.trackerforce;
package io.github.trackerforce.path;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.trackerforce;
package io.github.trackerforce.path;

import java.util.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
}
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/io/github/trackerforce/path/api/DotPath.java
Original file line number Diff line number Diff line change
@@ -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 <T> 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
*/
<T> Map<String, Object> run(T source, List<String> 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<String> paths);
}
23 changes: 23 additions & 0 deletions src/main/java/io/github/trackerforce/path/api/DotPrinter.java
Original file line number Diff line number Diff line change
@@ -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);
}
Loading