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

Commit

Permalink
Move Mapping to Value.Hash. (#64)
Browse files Browse the repository at this point in the history
In preparation of introducing a shared type for record values.
  • Loading branch information
blackwinter committed Nov 15, 2021
1 parent 77bb545 commit c6e20f9
Show file tree
Hide file tree
Showing 7 changed files with 416 additions and 404 deletions.
52 changes: 26 additions & 26 deletions metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void apply(final Record record, final List<String> params, final Map<Stri
final String key = params.get(0);
final Object val = record.get(key.replace(DOT_APPEND, EMPTY));

final Mapping result = new Mapping();
final Value.Hash result = new Value.Hash();
options.forEach(result::put);

if (key.endsWith(DOT_APPEND) && val instanceof List) {
Expand All @@ -71,9 +71,9 @@ public void apply(final Record record, final List<String> params, final Map<Stri
public void apply(final Record record, final List<String> params, final Map<String, String> options) {
final String fieldName = params.get(0);
Metafix.asList(record.get(fieldName)).forEach(recordEntry -> {
if (recordEntry instanceof Mapping) {
if (recordEntry instanceof Value.Hash) {
record.remove(fieldName);
((Mapping) recordEntry).forEach((subFieldName, value) -> {
((Value.Hash) recordEntry).forEach((subFieldName, value) -> {
Metafix.add(record, fieldName, subFieldName);
Metafix.add(record, fieldName, value);
});
Expand All @@ -84,7 +84,7 @@ public void apply(final Record record, final List<String> params, final Map<Stri
hash { // hash-from-array
public void apply(final Record record, final List<String> params, final Map<String, String> options) {
final List<Object> values = Metafix.asList(record.get(params.get(0)));
final Mapping result = new Mapping();
final Value.Hash result = new Value.Hash();
for (int i = 1; i < values.size(); i = i + 2) {
result.put(values.get(i - 1).toString(), values.get(i));
}
Expand Down Expand Up @@ -135,7 +135,7 @@ public void apply(final Record record, final List<String> params, final Map<Stri
* 2. Named groups are not mixed with unnamed groups.
*/
final Matcher groupMatcher = NAMED_GROUP_PATTERN.matcher(p.pattern());
final Mapping result = new Mapping();
final Value.Hash result = new Value.Hash();

while (groupMatcher.find()) {
final String group = groupMatcher.group(1);
Expand Down Expand Up @@ -258,7 +258,7 @@ private static void applyToFields(final Record record, final List<String> params
}
}

private static Object insert(final InsertMode mode, final Mapping record, final String[] keys, final String value) {
private static Object insert(final InsertMode mode, final Value.Hash record, final String[] keys, final String value) {
final String currentKey = keys[0];
if (keys.length == 1) {
mode.apply(record, currentKey, value);
Expand All @@ -271,14 +271,14 @@ private static Object insert(final InsertMode mode, final Mapping record, final
}

@SuppressWarnings("unchecked")
private static Object insertNested(final InsertMode mode, final Mapping record, final String value, final String currentKey, final String[] remainingKeys) {
private static Object insertNested(final InsertMode mode, final Value.Hash record, final String value, final String currentKey, final String[] remainingKeys) {
if (!record.containsField(currentKey)) {
record.put(currentKey, new Mapping());
record.put(currentKey, new Value.Hash());
}
final Object nested = record.get(currentKey);
final Object result;
if (nested instanceof Mapping) {
result = insert(mode, (Mapping) nested, remainingKeys, value);
if (nested instanceof Value.Hash) {
result = insert(mode, (Value.Hash) nested, remainingKeys, value);
}
else if (nested instanceof List) {
processList(mode, value, remainingKeys, nested);
Expand All @@ -293,29 +293,29 @@ else if (nested instanceof List) {
@SuppressWarnings("unchecked")
private static void processList(final InsertMode mode, final String value, final String[] remainingKeys, final Object nested) {
final List<Object> nestedList = (List<Object>) nested;
final Mapping nestedMap;
final Value.Hash nestedMap;
switch (remainingKeys[0]) {
case APPEND:
nestedMap = new Mapping();
nestedMap = new Value.Hash();
nestedList.add(nestedMap);
insert(mode, nestedMap, Arrays.copyOfRange(remainingKeys, 1, remainingKeys.length), value);
break;
case LAST:
final Object last = nestedList.get(nestedList.size() - 1);
if (last instanceof Mapping) {
nestedMap = (Mapping) last;
if (last instanceof Value.Hash) {
nestedMap = (Value.Hash) last;
insert(mode, nestedMap, Arrays.copyOfRange(remainingKeys, 1, remainingKeys.length), value);
}
break;
default:
nestedMap = new Mapping();
nestedMap = new Value.Hash();
nestedList.add(nestedMap);
insert(mode, nestedMap, remainingKeys, value);
break;
}
}

static Object find(final Mapping record, final String[] keys) {
static Object find(final Value.Hash record, final String[] keys) {
final String currentKey = keys[0];
if (!record.containsField(currentKey) || keys.length == 1) {
return record.get(currentKey);
Expand All @@ -324,22 +324,22 @@ static Object find(final Mapping record, final String[] keys) {
return findNested(record, currentKey, remainingKeys);
}

private static Object findNested(final Mapping record, final String currentKey, final String[] remainingKeys) {
private static Object findNested(final Value.Hash record, final String currentKey, final String[] remainingKeys) {
final Object nested = record.get(currentKey);
// TODO: array of maps, like in insertNested
if (nested instanceof List) {
return ((List<?>) nested).stream().map(o -> findNested(record, currentKey, remainingKeys))
.collect(Collectors.toList());
}
if (nested instanceof Mapping) {
if (nested instanceof Value.Hash) {
@SuppressWarnings("unchecked")
final Object result = find((Mapping) nested, remainingKeys);
final Object result = find((Value.Hash) nested, remainingKeys);
return result;
}
throw new IllegalStateException(NESTED + nested);
}

private static Object remove(final Mapping record, final String[] keys) {
private static Object remove(final Value.Hash record, final String[] keys) {
final String currentKey = keys[0];
if (keys.length == 1) {
record.remove(currentKey);
Expand All @@ -351,11 +351,11 @@ private static Object remove(final Mapping record, final String[] keys) {
return removeNested(record, currentKey, remainingKeys);
}

private static Object removeNested(final Mapping record, final String currentKey, final String[] remainingKeys) {
private static Object removeNested(final Value.Hash record, final String currentKey, final String[] remainingKeys) {
final Object nested = record.get(currentKey);
if (nested instanceof Mapping) {
if (nested instanceof Value.Hash) {
@SuppressWarnings("unchecked")
final Object result = remove((Mapping) nested, remainingKeys);
final Object result = remove((Value.Hash) nested, remainingKeys);
return result;
}
throw new IllegalStateException(NESTED + nested);
Expand All @@ -380,18 +380,18 @@ static String[] split(final String s) {
private enum InsertMode {
REPLACE {
@Override
void apply(final Mapping record, final String key, final String value) {
void apply(final Value.Hash record, final String key, final String value) {
record.put(key, value);
}
},
APPEND {
@Override
void apply(final Mapping record, final String key, final String value) {
void apply(final Value.Hash record, final String key, final String value) {
final Object object = record.get(key);
record.put(key, object == null ? value : Metafix.merged(object, value));
}
};
abstract void apply(Mapping record, String key, String value);
abstract void apply(Value.Hash record, String key, String value);
}

abstract void apply(Record record, List<String> params, Map<String, String> options);
Expand Down
141 changes: 0 additions & 141 deletions metafix/src/main/java/org/metafacture/metafix/Mapping.java

This file was deleted.

32 changes: 16 additions & 16 deletions metafix/src/main/java/org/metafacture/metafix/Metafix.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class Metafix implements StreamPipe<StreamReceiver> {
private int entityCount;
private StreamReceiver outputStreamReceiver;
private String recordIdentifier;
private List<Mapping> entities = new ArrayList<>();
private List<Value.Hash> entities = new ArrayList<>();

public Metafix() {
init();
Expand Down Expand Up @@ -157,8 +157,8 @@ private void emit(final Object key, final Object val) {
}
for (int i = 0; i < vals.size(); ++i) {
final Object value = vals.get(i);
if (value instanceof Mapping) {
final Mapping nested = (Mapping) value;
if (value instanceof Value.Hash) {
final Value.Hash nested = (Value.Hash) value;
outputStreamReceiver.startEntity(isMulti ? "" : key.toString());
nested.forEach(this::emit);
outputStreamReceiver.endEntity();
Expand All @@ -179,23 +179,23 @@ public void startEntity(final String name) {
}
++entityCount;
final Integer currentEntityIndex = entityCountStack.peek() - 1;
final Mapping previousEntity = currentEntityIndex < 0 ||
final Value.Hash previousEntity = currentEntityIndex < 0 ||
entities.size() <= currentEntityIndex ? null : entities.get(currentEntityIndex);
entityCountStack.push(Integer.valueOf(entityCount));
flattener.startEntity(name);
entities.add(currentEntity(name, previousEntity != null ? previousEntity : currentRecord));
}

private Mapping currentEntity(final String name, final Mapping previousEntity) {
private Value.Hash currentEntity(final String name, final Value.Hash previousEntity) {
final Object existingValue = previousEntity != null ? previousEntity.get(name) : null;
final Mapping currentEntity;
if (existingValue instanceof Mapping) {
final Value.Hash currentEntity;
if (existingValue instanceof Value.Hash) {
@SuppressWarnings("unchecked")
final Mapping existingEntity = (Mapping) previousEntity.get(name);
final Value.Hash existingEntity = (Value.Hash) previousEntity.get(name);
currentEntity = existingEntity;
}
else {
currentEntity = new Mapping();
currentEntity = new Value.Hash();
add(previousEntity != null ? previousEntity : currentRecord, name, currentEntity);
}
return currentEntity;
Expand All @@ -211,7 +211,7 @@ public void endEntity() {
public void literal(final String name, final String value) {
LOG.debug("Putting '{}': '{}'", name, value);
final Integer currentEntityIndex = entityCountStack.peek() - 1;
final Mapping currentEntity = currentEntityIndex < 0 ||
final Value.Hash currentEntity = currentEntityIndex < 0 ||
entities.size() <= currentEntityIndex ? null : entities.get(currentEntityIndex);
add(currentEntity != null ? currentEntity : currentRecord, name, value);
// TODO: keep flattener as option?
Expand Down Expand Up @@ -252,24 +252,24 @@ public Record getCurrentRecord() {
return currentRecord;
}

static void addAll(final Mapping record, final String fieldName, final List<String> values) {
static void addAll(final Value.Hash record, final String fieldName, final List<String> values) {
values.forEach(value -> add(record, fieldName, value));
}

static void addAll(final Mapping record, final Mapping values) {
static void addAll(final Value.Hash record, final Value.Hash values) {
values.forEach((fieldName, value) -> add(record, fieldName, value));
}

static void add(final Mapping record, final String name, final Object newValue) {
static void add(final Value.Hash record, final String name, final Object newValue) {
final Object oldValue = record.get(name);
record.put(name, oldValue == null ? newValue : merged(oldValue, newValue));
}

@SuppressWarnings("unchecked")
static Object merged(final Object object1, final Object object2) {
if (object1 instanceof Mapping && object2 instanceof Mapping) {
final Mapping result = (Mapping) object1;
((Mapping) object2).forEach(result::put);
if (object1 instanceof Value.Hash && object2 instanceof Value.Hash) {
final Value.Hash result = (Value.Hash) object1;
((Value.Hash) object2).forEach(result::put);
return result;
}
final List<Object> list = asList(object1);
Expand Down
Loading

0 comments on commit c6e20f9

Please sign in to comment.