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

org.simpleflatmapper.map.mapper.UnorderedJoinMapperEnumerable 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.mapper;

import org.simpleflatmapper.map.MappingContext;
import org.simpleflatmapper.map.SourceFieldMapper;
import org.simpleflatmapper.map.context.impl.BreakDetector;
import org.simpleflatmapper.map.context.impl.BreakDetectorMappingContext;
import org.simpleflatmapper.util.ArrayListEnumerable;
import org.simpleflatmapper.util.Enumerable;
import org.simpleflatmapper.util.ErrorHelper;

import java.util.ArrayList;

public class UnorderedJoinMapperEnumerable implements Enumerable {

    private final SourceFieldMapper mapper;
    private final MappingContext mappingContext;


    private final Enumerable sourceEnumerable;
    private final BreakDetector breakDetector;

    private Enumerable objectsEnumerable;
    
    public UnorderedJoinMapperEnumerable(SourceFieldMapper mapper,
                                         MappingContext mappingContext,
                                         Enumerable sourceEnumerable) {
        this.mapper = mapper;
        this.mappingContext = mappingContext;
        this.sourceEnumerable = sourceEnumerable;
        this.breakDetector = getRootDetector(mappingContext);
    }

    private BreakDetector getRootDetector(MappingContext mappingContext) {
        BreakDetectorMappingContext mp = (BreakDetectorMappingContext) mappingContext;
        BreakDetector rootDetector = mp.getRootDetector();
        if (!rootDetector.hasKeyDefinition()) {
            throw new IllegalStateException("No key definitions");
        }
        return rootDetector;
    }

    @Override
    public boolean next() {
        try {
            if (objectsEnumerable == null) objectsEnumerable = fetchAll();
            return objectsEnumerable.next();
        } catch (Exception e) {
            ErrorHelper.rethrow(e);
            return false;
        }

    }

    private Enumerable fetchAll() throws Exception {
        ArrayList objects = new ArrayList();
        while (sourceEnumerable.next()) {

            S source = sourceEnumerable.currentValue();

            mappingContext.handleSource(source); // set current key
            
            T currentValue = (T) breakDetector.getValue();
            
            if (currentValue == null) {
                currentValue = mapper.map(source, mappingContext);
                breakDetector.setValue(currentValue);
                objects.add(currentValue);
            } else {
                mapper.mapTo(source, currentValue, mappingContext);
            }
        }
        
        return new ArrayListEnumerable(objects);
    }

    @Override
    public T currentValue() {
        return objectsEnumerable.currentValue();
    }

    @Override
    public String toString() {
        return "UnorderedJoinMapperEnumerable{" +
                "jdbcMapper=" + mapper +
                '}';
    }
}