in runtime/src/main/java/com/google/cloud/verticals/foundations/dataharmonization/data/path/Path.java [74:123]
public static Path parse(CharSequence path) {
if (path == null || path.length() == 0) {
return empty();
}
// Estimate number of segments to avoid overgrowing these array lists.
// Note: below code will likely trigger Mutants but has no effect on functionality, only
// performance, so it should be ok.
int approxNumSegs = 0;
for (int i = 0; i < path.length(); i++) {
if (path.charAt(i) == SEGMENT_DELIM_CHAR || path.charAt(i) == INDEX_OPEN_CHAR) {
approxNumSegs++;
}
}
List<PathSegment> segments = new ArrayList<>(approxNumSegs);
int lastWildcardIndex = -1;
// Reuse the same result throughout to avoid a bunch of extra allocations.
ConsumeResult result = new ConsumeResult();
for (int i = 0; i < path.length(); ) {
if (path.charAt(i) == SEGMENT_DELIM_CHAR) {
i++;
continue;
}
if (path.charAt(i) == INDEX_OPEN_CHAR
&& i < path.length() - 1
&& path.charAt(i + 1) == WILDCARD_CHAR) {
consumeWildcard(result, i);
lastWildcardIndex = segments.size();
} else if (path.charAt(i) == INDEX_OPEN_CHAR) {
consumeBracketted(result, path, i);
} else {
consumeField(result, path, i);
}
segments.add(result.seg);
i = result.nextI;
}
// Only flatten arrays in a wildcard if they are produced by further wildcards. The last
// wildcard needs to not flatten.
if (lastWildcardIndex != -1) {
segments.set(lastWildcardIndex, new Wildcard(/* flatten */ false));
}
return new Path(segments);
}