Skip to content

Commit

Permalink
add toRuntime
Browse files Browse the repository at this point in the history
  • Loading branch information
cgardens committed Oct 8, 2020
1 parent 6afcb67 commit 020e1f8
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package io.airbyte.commons.concurrency;

import io.airbyte.commons.functional.CheckedConsumer;
import io.airbyte.commons.functional.VoidCallable;
import java.util.concurrent.Callable;

public class LifecycledCallable<T> implements Callable<T> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* MIT License
*
* Copyright (c) 2020 Airbyte
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.airbyte.commons.exception;

import io.airbyte.commons.functional.VoidCallable;
import java.util.concurrent.Callable;

public class Exceptions {

public static <T> T toRuntime(Callable<T> callable) {
try {
return callable.call();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public static void toRuntimeVoid(VoidCallable callable) {
try {
callable.call();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
* SOFTWARE.
*/

package io.airbyte.commons.concurrency;
package io.airbyte.commons.functional;

import java.util.concurrent.Callable;

@FunctionalInterface
public interface VoidCallable extends Callable<Void> {

default @Override Void call() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

package io.airbyte.commons.io;

import io.airbyte.commons.concurrency.VoidCallable;
import io.airbyte.commons.functional.VoidCallable;
import java.io.BufferedReader;
import java.io.InputStream;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static org.mockito.Mockito.when;

import io.airbyte.commons.functional.CheckedConsumer;
import io.airbyte.commons.functional.VoidCallable;
import java.util.concurrent.Callable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* MIT License
*
* Copyright (c) 2020 Airbyte
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.airbyte.commons.exception;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;

class ExceptionsTest {

@Test
void testToRuntime() {
assertEquals("hello", Exceptions.toRuntime(() -> callable("hello", false)));
assertThrows(RuntimeException.class, () -> Exceptions.toRuntime(() -> callable("goodbye", true)));
}

@Test
void testToRuntimeVoid() {
List<String> list = new ArrayList<>();
assertThrows(RuntimeException.class, () -> Exceptions.toRuntimeVoid(() -> voidCallable(list, "hello", true)));
assertEquals(0, list.size());

Exceptions.toRuntimeVoid(() -> voidCallable(list, "goodbye", false));
assertEquals(1, list.size());
assertEquals("goodbye", list.get(0));
}

private String callable(String input, boolean shouldThrow) throws IOException {
if (shouldThrow) {
throw new IOException();
} else {
return input;
}
}

private void voidCallable(List<String> list, String input, boolean shouldThrow) throws IOException {
if (shouldThrow) {
throw new IOException();
} else {
list.add(input);
}
}

}

0 comments on commit 020e1f8

Please sign in to comment.