Skip to content
This repository has been archived by the owner on Jan 27, 2025. It is now read-only.

Commit

Permalink
Move Hash#insertArray to Array#insert (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Nov 30, 2021
1 parent 77dcd2b commit 4d956a7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private Map<String, String> fileMap(final String location, final String separato
private static final Pattern NAMED_GROUP_PATTERN = Pattern.compile("\\(\\?<(.+?)>");

private static final String EMPTY = "";
private static final String DOT_APPEND = "." + Value.Hash.APPEND_FIELD;
private static final String DOT_APPEND = "." + Value.APPEND_FIELD;

abstract void apply(Record record, List<String> params, Map<String, String> options);

Expand Down
112 changes: 55 additions & 57 deletions metafix/src/main/java/org/metafacture/metafix/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
*/
public class Value {

/*package-private*/ static final String APPEND_FIELD = "$append";
private static final String LAST_FIELD = "$last";
private static final String ASTERISK = "*";

private final Array array;
Expand Down Expand Up @@ -361,16 +363,48 @@ private Value findInValue(final String[] path, final Value value) {
}
return result;
}

private void insert(final InsertMode mode, final String[] fields, final String newValue) {
switch (fields[0]) {
case ASTERISK:
// TODO: WDCD? descend into the array?
break;
case APPEND_FIELD:
add(newHash(h -> h.insert(mode, tail(fields), newValue)));
break;
case LAST_FIELD:
if (size() > 0) {
final Value last = get(size() - 1);
if (last.isHash()) {
last.asHash().insert(mode, tail(fields), newValue);
}
}
break;
default:
if (isNumber(fields[0])) {
// TODO: WDCD? insert at the given index? also descend into the array?
if (fields.length == 1) {
add(new Value(newValue));
}
if (fields.length > 1) {
final Value newHash = Value.newHash();
mode.apply(newHash.asHash(), fields[1], newValue);
add(newHash);
}
}
else {
add(newHash(h -> h.insert(mode, fields, newValue)));
}
break;
}
}
}

/**
* Represents a hash of metadata fields and values.
*/
public static class Hash extends AbstractValueType {

/*package-private*/ static final String APPEND_FIELD = "$append";
private static final String LAST_FIELD = "$last";

private static final String FIELD_PATH_SEPARATOR = "\\.";

private static final String UNEXPECTED = "expected array or hash, got ";
Expand Down Expand Up @@ -542,7 +576,7 @@ private Value insert(final InsertMode mode, final String[] fields, final String
value.asHash().insert(mode, tail(fields), newValue);
break;
case Array:
insertArray(mode, newValue, tail(fields), value.asArray());
value.asArray().insert(mode, tail(fields), newValue);
break;
default:
throw new IllegalStateException(UNEXPECTED + value.type);
Expand All @@ -553,42 +587,6 @@ private Value insert(final InsertMode mode, final String[] fields, final String
return new Value(this);
}

private void insertArray(final InsertMode mode, final String newValue, final String[] fields,
final Array array) {
switch (fields[0]) {
case ASTERISK:
// TODO: WDCD? descend into the array?
break;
case APPEND_FIELD:
array.add(newHash(h -> h.insert(mode, tail(fields), newValue)));
break;
case LAST_FIELD:
if (size() > 0) {
final Value last = array.get(array.size() - 1);
if (last.isHash()) {
last.asHash().insert(mode, tail(fields), newValue);
}
}
break;
default:
if (isNumber(fields[0])) {
// TODO: WDCD? insert at the given index? also descend into the array?
if (fields.length == 1) {
array.add(new Value(newValue));
}
if (fields.length > 1) {
final Value newHash = Value.newHash();
mode.apply(newHash.asHash(), fields[1], newValue);
array.add(newHash);
}
}
else {
array.add(newHash(h -> h.insert(mode, fields, newValue)));
}
break;
}
}

/**
* Removes the given field/value pair from this hash.
*
Expand Down Expand Up @@ -700,26 +698,26 @@ public String asString() {
return map.toString();
}

private enum InsertMode {
}

REPLACE {
@Override
void apply(final Hash hash, final String field, final String value) {
hash.put(field, new Value(value));
}
},
APPEND {
@Override
void apply(final Hash hash, final String field, final String value) {
final Value oldValue = hash.get(field);
final Value newValue = new Value(value);
hash.put(field, oldValue == null ? newValue : oldValue.merge(newValue));
}
};
private enum InsertMode {

abstract void apply(Hash hash, String field, String value);
REPLACE {
@Override
void apply(final Hash h, final String field, final String value) {
h.put(field, new Value(value));
}
},
APPEND {
@Override
void apply(final Hash h, final String field, final String value) {
final Value oldValue = h.get(field);
final Value newValue = new Value(value);
h.put(field, oldValue == null ? newValue : oldValue.merge(newValue));
}
};

}
abstract void apply(Hash h, String field, String value);

}
}

0 comments on commit 4d956a7

Please sign in to comment.