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

org.simpleflatmapper.map.mapper.AbstractEnumerableMapper 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.EnumerableMapper;
import org.simpleflatmapper.map.MappingException;
import org.simpleflatmapper.map.ConsumerErrorHandler;
import org.simpleflatmapper.util.Enumerable;
import org.simpleflatmapper.util.EnumerableIterator;
import org.simpleflatmapper.util.CheckedConsumer;

import java.util.Iterator;

//IFJAVA8_START
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.simpleflatmapper.util.EnumerableSpliterator;

//IFJAVA8_END


public abstract class AbstractEnumerableMapper implements EnumerableMapper {

    protected final ConsumerErrorHandler errorHandler;

    public AbstractEnumerableMapper(ConsumerErrorHandler errorHandler) {
        this.errorHandler = errorHandler;
    }
    
    
    @Override
	public final > H forEach(final SET source, final H handler)
			throws E, MappingException {
        final Enumerable enumerable = enumerate(source);
        while(enumerable.next()) {
            final T t = enumerable.currentValue();
            handleT(handler, t);
        }
        return handler;
	}
	
    private > void handleT(H handler, T t) {
        try {
            handler.accept(t);
        } catch(Throwable e) {
            errorHandler.handlerError(e, t);
        }
    }

    @Override
	public final Iterator iterator(SET source) throws MappingException, E {
		return new EnumerableIterator(enumerate(source));
	}

    //IFJAVA8_START
	@Override
	public final Stream stream(SET source) throws MappingException, E {
		return StreamSupport.stream(new EnumerableSpliterator(enumerate(source)), false);
	}
    //IFJAVA8_END



}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy