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

org.simpleflatmapper.map.context.impl.BreakDetectorMappingContext Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

There is a newer version: 9.0.2
Show newest version
package org.simpleflatmapper.map.context.impl;

import org.simpleflatmapper.map.MappingContext;
import org.simpleflatmapper.map.context.KeyDefinition;

public class BreakDetectorMappingContext extends MappingContext {

    private final BreakDetector rootDetector;
    private final MappingContext delegateContext;
    private final BreakDetector[] breakDetectors;

    public BreakDetectorMappingContext(KeyDefinition rootKeyDefinition,
                                       MappingContext delegateContext,
                                       KeyDefinition[] keyDefinitions) {
        this.delegateContext = delegateContext;
        this.breakDetectors = toBreakDetectors(keyDefinitions);
        this.rootDetector = breakDetectors[rootKeyDefinition.getIndex()];
    }


    @SuppressWarnings("unchecked")
    private static  BreakDetector[] toBreakDetectors(KeyDefinition[] definitions) {
        BreakDetector[] breakDetectors = new BreakDetector[definitions.length];
        for (int i = 0; i < definitions.length; i++) {
            KeyDefinition definition = definitions[i];
            breakDetectors[i] = new BreakDetector(definition);
        }
        return breakDetectors;
    }

    @Override
    public boolean broke(S source) {
        boolean b = rootDetector.broke(source);

        if (b) {
            for(BreakDetector breakDetector : breakDetectors) {
                if (breakDetector != rootDetector) {
                    breakDetector.markRootAsBroken();
                }
            }
        }

        for(BreakDetector breakDetector : breakDetectors) {
            if (breakDetector != rootDetector) {
                breakDetector.handleSource(source);
            }
        }

        return b;
    }

    @Override
    public void handleSource(S source) {
        for(BreakDetector breakDetector : breakDetectors) {
            breakDetector.handleSource(source);
        }
    }

    @Override
    public void markAsBroken() {
        for(BreakDetector breakDetector : breakDetectors) {
            breakDetector.markRootAsBroken();
        }
    }

    @Override
    public  T context(int i) {
        return delegateContext.context(i);
    }

    @Override
    public void setCurrentValue(int i,  Object value) {
        this.breakDetectors[i].setValue(value);
    }

    @Override
    public Object getCurrentValue(int i) {
        return breakDetectors[i].getValue();
    }

    public BreakDetector getRootDetector() {
        return rootDetector;
    }
}