From 578c6638badb1c427f2335432cb93d9a1a1eddce Mon Sep 17 00:00:00 2001 From: leventov Date: Sun, 30 Oct 2016 13:47:34 -0600 Subject: [PATCH] Add Throwables utilities, inspired by Guava --- .../chronicle/hash/impl/util/Throwables.java | 48 ++++++++++++ .../hash/impl/util/ThrowablesTest.java | 76 +++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/main/java/net/openhft/chronicle/hash/impl/util/Throwables.java create mode 100644 src/test/java/net/openhft/chronicle/hash/impl/util/ThrowablesTest.java diff --git a/src/main/java/net/openhft/chronicle/hash/impl/util/Throwables.java b/src/main/java/net/openhft/chronicle/hash/impl/util/Throwables.java new file mode 100644 index 000000000..733ccb44b --- /dev/null +++ b/src/main/java/net/openhft/chronicle/hash/impl/util/Throwables.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2012, 2016 higherfrequencytrading.com + * Copyright (C) 2016 Roman Leventov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package net.openhft.chronicle.hash.impl.util; + +/** + * Inspired by Guava's Throwables + */ +public final class Throwables { + + public static RuntimeException propagate(Throwable t) { + Objects.requireNonNull(t); + if (t instanceof Error) + throw (Error) t; + if (t instanceof RuntimeException) + throw (RuntimeException) t; + throw new RuntimeException(t); + } + + public static T propagateNotWrapping( + Throwable t, Class notWrappingThrowableType) throws T { + Objects.requireNonNull(t); + Objects.requireNonNull(notWrappingThrowableType); + if (t instanceof Error) + throw (Error) t; + if (t instanceof RuntimeException) + throw (RuntimeException) t; + if (notWrappingThrowableType.isInstance(t)) + throw notWrappingThrowableType.cast(t); + throw new RuntimeException(t); + } + + private Throwables() {} +} diff --git a/src/test/java/net/openhft/chronicle/hash/impl/util/ThrowablesTest.java b/src/test/java/net/openhft/chronicle/hash/impl/util/ThrowablesTest.java new file mode 100644 index 000000000..63b713f98 --- /dev/null +++ b/src/test/java/net/openhft/chronicle/hash/impl/util/ThrowablesTest.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2012, 2016 higherfrequencytrading.com + * Copyright (C) 2016 Roman Leventov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package net.openhft.chronicle.hash.impl.util; + +import org.junit.Test; + +import java.io.IOException; + +@SuppressWarnings("ThrowableResultOfMethodCallIgnored") +public class ThrowablesTest { + + @Test(expected = NullPointerException.class) + public void testPropagateNull() { + Throwables.propagate(null); + } + + @Test(expected = AssertionError.class) + public void testPropagateError() { + Throwables.propagate(new AssertionError()); + } + + @Test(expected = IllegalStateException.class) + public void testPropagateUncheckedException() { + Throwables.propagate(new IllegalStateException()); + } + + @Test(expected = RuntimeException.class) + public void testPropagateCheckedException() { + Throwables.propagate(new IOException()); + } + + @Test(expected = NullPointerException.class) + public void testPropagateNotWrappingNull() throws IOException { + Throwables.propagateNotWrapping(null, IOException.class); + } + + @Test(expected = NullPointerException.class) + public void testPropagateNotWrappingNullWrappingType() { + Throwables.propagateNotWrapping(new IOException(), null); + } + + @Test(expected = AssertionError.class) + public void testPropagateNotWrappingError() throws IOException { + Throwables.propagateNotWrapping(new AssertionError(), IOException.class); + } + + @Test(expected = IllegalStateException.class) + public void testPropagateNotWrappingUncheckedException() throws IOException { + Throwables.propagateNotWrapping(new IllegalStateException(), IOException.class); + } + + @Test(expected = IOException.class) + public void testPropagateNotWrappingNotWrappingEException() throws IOException { + Throwables.propagateNotWrapping(new IOException(), IOException.class); + } + + @Test(expected = RuntimeException.class) + public void testPropagateNotWrappingCheckedException() throws IOException { + Throwables.propagateNotWrapping(new Exception(), IOException.class); + } +}