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

org.simpleflatmapper.map.mapper.AbstractEnumarableMapper Maven / Gradle / Ivy

package org.simpleflatmapper.map.mapper;

import org.simpleflatmapper.map.EnumarableMapper;
import org.simpleflatmapper.map.MappingException;
import org.simpleflatmapper.map.ConsumerErrorHandler;
import org.simpleflatmapper.util.Enumarable;
import org.simpleflatmapper.util.EnumarableIterator;
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.EnumarableSpliterator;

//IFJAVA8_END


public abstract class AbstractEnumarableMapper implements EnumarableMapper {

    protected final ConsumerErrorHandler errorHandler;

    public AbstractEnumarableMapper(ConsumerErrorHandler errorHandler) {
        this.errorHandler = errorHandler;
    }

    @Override
	public final > H forEach(final S source, final H handler)
			throws E, MappingException {
        final Enumarable enumarable = newEnumarableOfT(source);
        while(enumarable.next()) {
            final T t = enumarable.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(S source) throws MappingException, E {
		return new EnumarableIterator(newEnumarableOfT(source));
	}

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

    protected abstract Enumarable newEnumarableOfT(S source) throws E;


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy