dido.operators.Flatten Maven / Gradle / Ivy
The newest version!
package dido.operators;
import dido.data.*;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
public class Flatten {
public static List> flatten(F field, IndexedData data) {
return new DynamicFlatten<>(extractorForFieldOrIndex(field, 0)).apply(data);
}
public static List> flattenAt(int index, IndexedData data) {
return new DynamicFlatten<>(extractorForFieldOrIndex((F) null, 0)).apply(data);
}
public static Function, List>> field(F field) {
return new DynamicFlatten<>(extractorForFieldOrIndex(field, 0));
}
public static Function, List>> fields(F... fields) {
Collection> extractors = Arrays.stream(fields)
.map(f -> extractorForFieldOrIndex(f, 0))
.collect(Collectors.toList());
return new DynamicIterableFlatten<>(extractors);
}
public static Function, List>> indices(int... indices) {
Collection> extractors = Arrays.stream(indices)
.mapToObj(i -> (Extractor) extractorForFieldOrIndex(null, i))
.collect(Collectors.toList());
return new DynamicIterableFlatten<>(extractors);
}
public static Function, List>> fieldOfSchema(F field,
DataSchema schema) {
return fieldOrIndexOfSchema(field, 0, schema);
}
public static Function, List>> fieldOrIndexOfSchema(F field,
int index,
DataSchema schema) {
Extractor extractor = extractorForFieldOrIndex(field, index);
return extractorOfSchema(extractor, schema);
}
static Function, List>> extractorOfSchema(Extractor extractor,
DataSchema schema) {
DataSchema nestedSchema = Objects.requireNonNull(extractor.getSchema(schema),
"No Nested Schema for " + extractor );
Concatenator concatenator = extractor.bodgeFields(Concatenator.withSettings())
.makeFromSchemas(schema, nestedSchema);
return new KnownRepeatingFlatten<>(concatenator, extractor);
}
static class KnownRepeatingFlatten implements Function, List>> {
private final Concatenator concatenator;
private final Extractor extractor;
KnownRepeatingFlatten(Concatenator concatenator, Extractor extractor) {
this.concatenator = concatenator;
this.extractor = extractor;
}
@Override
public List> apply(IndexedData data) {
@SuppressWarnings("unchecked")
RepeatingData nested = (RepeatingData) extractor.extract(data);
List> flattened = new ArrayList<>(nested.size());
for (IndexedData element : nested) {
flattened.add(concatenator.concat(data, element));
}
return flattened;
}
}
static Function, List>> strategyFlatten(DataSchema schema,
Collection> extractors) {
Map> extractorMap = new HashMap<>();
SchemaBuilder schemaBuilder = SchemaBuilder.impliedType();
for (int index = schema.firstIndex(); index > 0; index = schema.nextIndex(index)) {
Extractor extractor = null;
for (Extractor e : extractors) {
if (e.isForIndexInSchema(index, schema)) {
extractor = e;
break;
}
}
SchemaField existingSchemaField = schema.getSchemaFieldAt(index);
if (extractor == null) {
schemaBuilder.addSchemaField(existingSchemaField);
}
else {
extractorMap.put(index, extractor);
Class> type = schema.getTypeAt(index);
Class> newType;
if (type.isArray()) {
newType = Primitives.wrap(type.getComponentType());
}
else {
newType = Object.class;
}
schemaBuilder.addSchemaField(
SchemaField.of(1, newType)
.mapTo(existingSchemaField.getIndex(), existingSchemaField.getField()));
}
}
return new KnownIterableFlatten<>(extractorMap, schema, schemaBuilder.build());
}
static class KnownIterableFlatten implements Function, List>> {
private final Map> extractors;
private final DataSchema schema;
private final DataSchema newSchema;
KnownIterableFlatten(Map> extractors, DataSchema schema, DataSchema newSchema) {
this.extractors = extractors;
this.schema = schema;
this.newSchema = newSchema;
}
@Override
public List> apply(IndexedData data) {
int maxSize = 1;
Map> lists = new HashMap<>(extractors.size());
for (Map.Entry> entry : extractors.entrySet()) {
int index = entry.getKey();
Extractor extractor = entry.getValue();
Object value = extractor.extract(data);
if (value == null) {
lists.put(index, Collections.emptyList());
continue;
}
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy