File tree Expand file tree Collapse file tree
main/java/org/apache/commons/io/function
test/java/org/apache/commons/io/function Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1818package org .apache .commons .io .function ;
1919
2020import java .io .IOException ;
21+ import java .io .UncheckedIOException ;
2122
2223/**
2324 * Like {@link Runnable} but throws {@link IOException}.
2728@ FunctionalInterface
2829public interface IORunnable {
2930
31+ /**
32+ * Creates a {@link Runnable} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}.
33+ *
34+ * @return an unchecked Predicate.
35+ */
36+ default Runnable asRunnable () {
37+ return () -> Uncheck .run (this );
38+ }
39+
3040 /**
3141 * Like {@link Runnable#run()} but throws {@link IOException}.
3242 *
Original file line number Diff line number Diff line change 1818package org .apache .commons .io .function ;
1919
2020import static org .junit .jupiter .api .Assertions .assertEquals ;
21+ import static org .junit .jupiter .api .Assertions .assertNull ;
22+ import static org .junit .jupiter .api .Assertions .assertThrows ;
2123
2224import java .io .IOException ;
25+ import java .io .UncheckedIOException ;
26+ import java .nio .file .Files ;
27+ import java .util .concurrent .Executors ;
2328import java .util .concurrent .atomic .AtomicReference ;
2429
30+ import org .apache .commons .io .file .PathUtils ;
2531import org .junit .jupiter .api .Test ;
2632
2733/**
@@ -42,4 +48,11 @@ public void testAccept() throws IOException {
4248 assertEquals ("A1" , ref .get ());
4349 }
4450
51+ @ Test
52+ public void testAsRunnable () throws Exception {
53+ assertThrows (UncheckedIOException .class , () -> Executors .callable (TestConstants .THROWING_IO_RUNNABLE .asRunnable ()).call ());
54+ final IORunnable runnable = () -> Files .size (PathUtils .current ());
55+ assertNull (Executors .callable (runnable .asRunnable ()).call ());
56+ }
57+
4558}
You can’t perform that action at this time.
0 commit comments