All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dido.operators.AlternativeView Maven / Gradle / Ivy

The newest version!
package dido.operators;

import dido.data.*;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * Provide an alternative view of data by changing fields.
 *
 * @param 
 */
public class AlternativeView implements Function, GenericData> {

    private final Map fromToFields;

    private final Map toFromFields;

    private DataSchema lastSchema;

    private DataSchema newSchema;

    private AlternativeView(Map fromToFields, Map toFromFields) {
        this.fromToFields = fromToFields;
        this.toFromFields = toFromFields;
    }

    public static class With {

        private final Map fieldMap = new HashMap<>();

        public With fieldChange(F original, F toField) {
            fieldMap.put(original, toField);
            return this;
        }

        public Function, GenericData> make() {

            final Map fromToFields = new HashMap<>();

            final Map toFromFields = new HashMap<>();

            for (Map.Entry entry : this.fieldMap.entrySet()) {
                fromToFields.put(entry.getKey(), entry.getValue());
                toFromFields.put(entry.getValue(), entry.getKey());
            }

            return new AlternativeView<>(fromToFields, toFromFields);
        }
    }

    public static  With with() {
        return new With<>();
    }

    @Override
    public GenericData apply(IndexedData originalData) {

        GenericData inData = GenericData.from(originalData);

        DataSchema originalSchema = inData.getSchema();
        if (lastSchema == null || !lastSchema.equals(originalSchema)) {
            newSchema = new Schema(originalSchema);
            lastSchema = originalSchema;
        }

        return new Data(inData, newSchema);
    }

    class Schema extends AbstractDataSchema {

        private final DataSchema original;

        Schema(DataSchema original) {
            this.original = original;
        }

        @Override
        public SchemaField getSchemaFieldAt(int index) {
            SchemaField original = this.original.getSchemaFieldAt(index);
            F now = fromToFields.get(original.getField());
            if (now == null) {
                return original;
            }
            else {
                return original.mapToField(now);
            }
        }

        @Override
        public int getIndex(F field) {
            F original = toFromFields.get(field);
            if (original == null) {
                return this.original.getIndex(field);
            }
            else {
                return this.original.getIndex(original);
            }
        }

        @Override
        public int firstIndex() {
            return original.firstIndex();
        }

        @Override
        public int nextIndex(int index) {
            return original.nextIndex(index);
        }

        @Override
        public int lastIndex() {
            return original.lastIndex();
        }
    }


    class Data extends AbstractGenericData {

        private final GenericData originalData;

        private final DataSchema schema;

        Data(GenericData originalData, DataSchema schema) {
            this.originalData = originalData;
            this.schema = schema;
        }


        @Override
        public DataSchema getSchema() {
            return schema;
        }

        @Override
        public Object getAt(int index) {
            return originalData.getAt(index);
        }

        @Override
        public boolean hasIndex(int index) {
            return originalData.hasIndex(index);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy