Skip to content

Instantly share code, notes, and snippets.

@SnuktheGreat
Created July 15, 2015 11:42
Show Gist options
  • Select an option

  • Save SnuktheGreat/108d4680ea1ec1007eb7 to your computer and use it in GitHub Desktop.

Select an option

Save SnuktheGreat/108d4680ea1ec1007eb7 to your computer and use it in GitHub Desktop.
Type safe work-around of throwing typed exceptions from within Streams. It builds upon the accepted answer of this question on stack overflow: http://stackoverflow.com/questions/27644361/how-can-i-throw-checked-exceptions-from-inside-java-8-streams
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
public class UtilException<E extends Exception> {
private static final UtilException INSTANCE = new UtilException();
@SuppressWarnings("unchecked")
public static <E extends Exception> UtilException<E> create() throws E {
return INSTANCE;
}
private UtilException() {
// Does nothing
}
@FunctionalInterface
public interface Consumer_WithExceptions<T, E extends Exception> {
void accept(T t) throws E;
}
@FunctionalInterface
public interface Function_WithExceptions<T, R, E extends Exception> {
R apply(T t) throws E;
}
@FunctionalInterface
public interface Supplier_WithExceptions<T, E extends Exception> {
T get() throws E;
}
/**
* .forEach(rethrowConsumer(name -> System.out.println(Class.forName(name)))); or .forEach(rethrowConsumer(ClassNameUtil::println));
*/
public <T> Consumer<T> rethrowConsumer(Consumer_WithExceptions<T, E> consumer) {
return t -> {
try {
consumer.accept(t);
} catch (Exception exception) {
throwAsUnchecked(exception);
}
};
}
/**
* .map(rethrowFunction(name -> Class.forName(name))) or .map(rethrowFunction(Class::forName))
*/
public <T, R> Function<T, R> rethrowFunction(Function_WithExceptions<T, R, E> function) {
return t -> {
try {
return function.apply(t);
} catch (Exception exception) {
throwAsUnchecked(exception);
return null;
}
};
}
/**
* rethrowSupplier(() -> new StringJoiner(new String(new byte[]{77, 97, 114, 107}, "UTF-8"))),
*/
public <T> Supplier<T> rethrowSupplier(Supplier_WithExceptions<T, E> function) {
return () -> {
try {
return function.get();
} catch (Exception exception) {
throwAsUnchecked(exception);
return null;
}
};
}
@SuppressWarnings("unchecked")
private static <E extends Exception> void throwAsUnchecked(Exception exception) throws E {
throw (E) exception;
}
}
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.io.IOException;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
public class UtilExceptionTest {
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void testRethrowConsumer() throws IOException {
UtilException<IOException> utilException = UtilException.create();
exception.expect(IOException.class);
Stream.of("Reason")
.forEach(utilException.rethrowConsumer(reason -> {throw new IOException(reason);}));
}
@Test
public void testRethrowFunction() throws IOException {
UtilException<IOException> utilException = UtilException.create();
exception.expect(IOException.class);
Stream.of("Reason")
.map(utilException.rethrowFunction(reason -> {throw new IOException(reason);}))
.collect(toList());
}
@Test
public void testRethrowSupplier() throws IOException {
UtilException<IOException> utilException = UtilException.create();
exception.expect(IOException.class);
Stream.generate(utilException.rethrowSupplier(() -> {throw new IOException();}))
.collect(toList());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment