Skip to content

Commit

Permalink
Avoid collection and additional calls to tail() & get() (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Sep 26, 2024
1 parent 0cedb17 commit 49cfaca
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions metafix/src/main/java/org/metafacture/metafix/FixPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,18 +278,8 @@ else if (isReference(field)) {
mode.apply(hash, field, newValue);
}
else {
if (!hash.containsField(field)) {
final Value value = Arrays.asList(ReservedField.$prepend.name(), ReservedField.$append.name())
.contains(tail(path)[0]) ? Value.newArray() : Value.newHash();
hash.put(field, value.withPathSet(newValue.getPath()));
}
else {
final Value value = hash.get(field);
if (value.isString()) {
hash.put(field, Value.newArray(a -> a.add(value)));
}
}
insertInto(hash.get(field), mode, newValue, field, tail(path));
final String[] tail = tail(path);
insertInto(getContainerValue(hash, field, newValue.getPath(), tail[0]), mode, newValue, field, tail);
}

return new Value(hash);
Expand All @@ -309,6 +299,23 @@ private Value insertInto(final Value value, final InsertMode mode, final Value n
}
}

private Value getContainerValue(final Hash hash, final String field, final String newPath, final String nextField) {
Value result = hash.get(field);
if (result == null) {
result = (nextField.equals(ReservedField.$prepend.name()) || nextField.equals(ReservedField.$append.name()) ?
Value.newArray() : Value.newHash()).withPathSet(newPath);
hash.put(field, result);
}
else {
if (result.isString()) {
final Value value = result;
result = Value.newArray(a -> a.add(value));
hash.put(field, result);
}
}
return result;
}

private String[] tail(final String[] fields) {
return Arrays.copyOfRange(fields, 1, fields.length);
}
Expand Down

0 comments on commit 49cfaca

Please sign in to comment.