org.simpleflatmapper.map.mapper.AbstractEnumerableMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sfm-map Show documentation
Show all versions of sfm-map Show documentation
Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.
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
}