Skip to content

Commit 49cfaca

Browse files
committed
Avoid collection and additional calls to tail() & get() (#127)
1 parent 0cedb17 commit 49cfaca

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

metafix/src/main/java/org/metafacture/metafix/FixPath.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -278,18 +278,8 @@ else if (isReference(field)) {
278278
mode.apply(hash, field, newValue);
279279
}
280280
else {
281-
if (!hash.containsField(field)) {
282-
final Value value = Arrays.asList(ReservedField.$prepend.name(), ReservedField.$append.name())
283-
.contains(tail(path)[0]) ? Value.newArray() : Value.newHash();
284-
hash.put(field, value.withPathSet(newValue.getPath()));
285-
}
286-
else {
287-
final Value value = hash.get(field);
288-
if (value.isString()) {
289-
hash.put(field, Value.newArray(a -> a.add(value)));
290-
}
291-
}
292-
insertInto(hash.get(field), mode, newValue, field, tail(path));
281+
final String[] tail = tail(path);
282+
insertInto(getContainerValue(hash, field, newValue.getPath(), tail[0]), mode, newValue, field, tail);
293283
}
294284

295285
return new Value(hash);
@@ -309,6 +299,23 @@ private Value insertInto(final Value value, final InsertMode mode, final Value n
309299
}
310300
}
311301

302+
private Value getContainerValue(final Hash hash, final String field, final String newPath, final String nextField) {
303+
Value result = hash.get(field);
304+
if (result == null) {
305+
result = (nextField.equals(ReservedField.$prepend.name()) || nextField.equals(ReservedField.$append.name()) ?
306+
Value.newArray() : Value.newHash()).withPathSet(newPath);
307+
hash.put(field, result);
308+
}
309+
else {
310+
if (result.isString()) {
311+
final Value value = result;
312+
result = Value.newArray(a -> a.add(value));
313+
hash.put(field, result);
314+
}
315+
}
316+
return result;
317+
}
318+
312319
private String[] tail(final String[] fields) {
313320
return Arrays.copyOfRange(fields, 1, fields.length);
314321
}

0 commit comments

Comments
 (0)