/** * Inspects the cause of the specified {@code ExecutionException} and * creates a {@code ConcurrentRuntimeException} with the checked cause if * necessary. This method works exactly like * {@link #extractCause(ExecutionException)}. The only difference is that * the cause of the specified {@code ExecutionException} is extracted as a * runtime exception. This is an alternative for client code that does not * want to deal with checked exceptions. * * @param ex the exception to be processed * @return a {@code ConcurrentRuntimeException} with the checked cause */ public static ConcurrentRuntimeException extractCauseUnchecked( final ExecutionException ex) { if (ex == null || ex.getCause() == null) { return null; } throwCause(ex); return new ConcurrentRuntimeException(ex.getMessage(), ex.getCause()); }
/** * Tests extractCauseUnchecked() if the cause is a checked exception. */ @Test public void testExtractCauseUncheckedChecked() { final Exception ex = new Exception("Test"); final ConcurrentRuntimeException cex = ConcurrentUtils .extractCauseUnchecked(new ExecutionException(ex)); assertSame("Wrong cause", ex, cex.getCause()); }
/** * Invokes the specified {@code ConcurrentInitializer} and transforms * occurring exceptions to runtime exceptions. This method works like * {@link #initialize(ConcurrentInitializer)}, but if the {@code * ConcurrentInitializer} throws a {@link ConcurrentException}, it is * caught, and the cause is wrapped in a {@link ConcurrentRuntimeException}. * So client code does not have to deal with checked exceptions. * * @param <T> the type of the object produced by the initializer * @param initializer the {@code ConcurrentInitializer} to be invoked * @return the object managed by the {@code ConcurrentInitializer} * @throws ConcurrentRuntimeException if the initializer throws an exception */ public static <T> T initializeUnchecked(final ConcurrentInitializer<T> initializer) { try { return initialize(initializer); } catch (final ConcurrentException cex) { throw new ConcurrentRuntimeException(cex.getCause()); } }
/** * Tests handleCauseUnchecked() if the cause is a checked exception. */ @Test public void testHandleCauseUncheckedChecked() { final Exception ex = new Exception("Test"); try { ConcurrentUtils.handleCauseUnchecked(new ExecutionException(ex)); fail("ConcurrentRuntimeException not thrown!"); } catch (final ConcurrentRuntimeException crex) { assertEquals("Wrong cause", ex, crex.getCause()); } }
/** * Checks if a concurrent map contains a key and creates a corresponding * value if not, suppressing checked exceptions. This method calls * {@code createIfAbsent()}. If a {@link ConcurrentException} is thrown, it * is caught and re-thrown as a {@link ConcurrentRuntimeException}. * * @param <K> the type of the keys of the map * @param <V> the type of the values of the map * @param map the map to be modified * @param key the key of the value to be added * @param init the {@link ConcurrentInitializer} for creating the value * @return the value stored in the map after this operation; this may or may * not be the object created by the {@link ConcurrentInitializer} * @throws ConcurrentRuntimeException if the initializer throws an exception */ public static <K, V> V createIfAbsentUnchecked(final ConcurrentMap<K, V> map, final K key, final ConcurrentInitializer<V> init) { try { return createIfAbsent(map, key, init); } catch (final ConcurrentException cex) { throw new ConcurrentRuntimeException(cex.getCause()); } }
/** * Tests whether exceptions are correctly handled by initializeUnchecked(). * * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it */ @Test public void testInitializeUncheckedEx() throws ConcurrentException { @SuppressWarnings("unchecked") final ConcurrentInitializer<Object> init = EasyMock .createMock(ConcurrentInitializer.class); final Exception cause = new Exception(); EasyMock.expect(init.get()).andThrow(new ConcurrentException(cause)); EasyMock.replay(init); try { ConcurrentUtils.initializeUnchecked(init); fail("Exception not thrown!"); } catch (final ConcurrentRuntimeException crex) { assertSame("Wrong cause", cause, crex.getCause()); } EasyMock.verify(init); }
/** * Tries to create a ConcurrentRuntimeException with a runtime as cause. */ @Test(expected = IllegalArgumentException.class) public void testConcurrentRuntimeExceptionCauseUnchecked() { new ConcurrentRuntimeException(new RuntimeException()); }
/** * Tests createIfAbsentUnchecked() if an exception is thrown. * * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it */ @Test public void testCreateIfAbsentUncheckedException() throws ConcurrentException { @SuppressWarnings("unchecked") final ConcurrentInitializer<Integer> init = EasyMock .createMock(ConcurrentInitializer.class); final Exception ex = new Exception(); EasyMock.expect(init.get()).andThrow(new ConcurrentException(ex)); EasyMock.replay(init); try { ConcurrentUtils.createIfAbsentUnchecked( new ConcurrentHashMap<String, Integer>(), "test", init); fail("Exception not thrown!"); } catch (final ConcurrentRuntimeException crex) { assertEquals("Wrong cause", ex, crex.getCause()); } EasyMock.verify(init); } }
/** * Tries to create a ConcurrentRuntimeException with an error as cause. */ @Test(expected = IllegalArgumentException.class) public void testConcurrentRuntimeExceptionCauseError() { new ConcurrentRuntimeException("An error", new Error()); }
/** * Tries to create a ConcurrentRuntimeException with null as cause. */ @Test(expected = IllegalArgumentException.class) public void testConcurrentRuntimeExceptionCauseNull() { new ConcurrentRuntimeException(null); }
/** * Tests creating ConcurrentRuntimeException with no arguments. */ @Test public void testUninitializedConcurrentRuntimeException() { assertNotNull("Error creating empty ConcurrentRuntimeException", new ConcurrentRuntimeException()); }
/** * Inspects the cause of the specified {@code ExecutionException} and * creates a {@code ConcurrentRuntimeException} with the checked cause if * necessary. This method works exactly like * {@link #extractCause(ExecutionException)}. The only difference is that * the cause of the specified {@code ExecutionException} is extracted as a * runtime exception. This is an alternative for client code that does not * want to deal with checked exceptions. * * @param ex the exception to be processed * @return a {@code ConcurrentRuntimeException} with the checked cause */ public static ConcurrentRuntimeException extractCauseUnchecked( final ExecutionException ex) { if (ex == null || ex.getCause() == null) { return null; } throwCause(ex); return new ConcurrentRuntimeException(ex.getMessage(), ex.getCause()); }
/** * Inspects the cause of the specified {@code ExecutionException} and * creates a {@code ConcurrentRuntimeException} with the checked cause if * necessary. This method works exactly like * {@link #extractCause(ExecutionException)}. The only difference is that * the cause of the specified {@code ExecutionException} is extracted as a * runtime exception. This is an alternative for client code that does not * want to deal with checked exceptions. * * @param ex the exception to be processed * @return a {@code ConcurrentRuntimeException} with the checked cause */ public static ConcurrentRuntimeException extractCauseUnchecked( final ExecutionException ex) { if (ex == null || ex.getCause() == null) { return null; } throwCause(ex); return new ConcurrentRuntimeException(ex.getMessage(), ex.getCause()); }
/** * Inspects the cause of the specified {@code ExecutionException} and * creates a {@code ConcurrentRuntimeException} with the checked cause if * necessary. This method works exactly like * {@link #extractCause(ExecutionException)}. The only difference is that * the cause of the specified {@code ExecutionException} is extracted as a * runtime exception. This is an alternative for client code that does not * want to deal with checked exceptions. * * @param ex the exception to be processed * @return a {@code ConcurrentRuntimeException} with the checked cause */ public static ConcurrentRuntimeException extractCauseUnchecked( final ExecutionException ex) { if (ex == null || ex.getCause() == null) { return null; } throwCause(ex); return new ConcurrentRuntimeException(ex.getMessage(), ex.getCause()); }
/** * Invokes the specified {@code ConcurrentInitializer} and transforms * occurring exceptions to runtime exceptions. This method works like * {@link #initialize(ConcurrentInitializer)}, but if the {@code * ConcurrentInitializer} throws a {@link ConcurrentException}, it is * caught, and the cause is wrapped in a {@link ConcurrentRuntimeException}. * So client code does not have to deal with checked exceptions. * * @param <T> the type of the object produced by the initializer * @param initializer the {@code ConcurrentInitializer} to be invoked * @return the object managed by the {@code ConcurrentInitializer} * @throws ConcurrentRuntimeException if the initializer throws an exception */ public static <T> T initializeUnchecked(final ConcurrentInitializer<T> initializer) { try { return initialize(initializer); } catch (final ConcurrentException cex) { throw new ConcurrentRuntimeException(cex.getCause()); } }
/** * Invokes the specified {@code ConcurrentInitializer} and transforms * occurring exceptions to runtime exceptions. This method works like * {@link #initialize(ConcurrentInitializer)}, but if the {@code * ConcurrentInitializer} throws a {@link ConcurrentException}, it is * caught, and the cause is wrapped in a {@link ConcurrentRuntimeException}. * So client code does not have to deal with checked exceptions. * * @param <T> the type of the object produced by the initializer * @param initializer the {@code ConcurrentInitializer} to be invoked * @return the object managed by the {@code ConcurrentInitializer} * @throws ConcurrentRuntimeException if the initializer throws an exception */ public static <T> T initializeUnchecked(final ConcurrentInitializer<T> initializer) { try { return initialize(initializer); } catch (final ConcurrentException cex) { throw new ConcurrentRuntimeException(cex.getCause()); } }
/** * Checks if a concurrent map contains a key and creates a corresponding * value if not, suppressing checked exceptions. This method calls * {@code createIfAbsent()}. If a {@link ConcurrentException} is thrown, it * is caught and re-thrown as a {@link ConcurrentRuntimeException}. * * @param <K> the type of the keys of the map * @param <V> the type of the values of the map * @param map the map to be modified * @param key the key of the value to be added * @param init the {@link ConcurrentInitializer} for creating the value * @return the value stored in the map after this operation; this may or may * not be the object created by the {@link ConcurrentInitializer} * @throws ConcurrentRuntimeException if the initializer throws an exception */ public static <K, V> V createIfAbsentUnchecked(final ConcurrentMap<K, V> map, final K key, final ConcurrentInitializer<V> init) { try { return createIfAbsent(map, key, init); } catch (final ConcurrentException cex) { throw new ConcurrentRuntimeException(cex.getCause()); } }
/** * Invokes the specified {@code ConcurrentInitializer} and transforms * occurring exceptions to runtime exceptions. This method works like * {@link #initialize(ConcurrentInitializer)}, but if the {@code * ConcurrentInitializer} throws a {@link ConcurrentException}, it is * caught, and the cause is wrapped in a {@link ConcurrentRuntimeException}. * So client code does not have to deal with checked exceptions. * * @param <T> the type of the object produced by the initializer * @param initializer the {@code ConcurrentInitializer} to be invoked * @return the object managed by the {@code ConcurrentInitializer} * @throws ConcurrentRuntimeException if the initializer throws an exception */ public static <T> T initializeUnchecked(final ConcurrentInitializer<T> initializer) { try { return initialize(initializer); } catch (final ConcurrentException cex) { throw new ConcurrentRuntimeException(cex.getCause()); } }
/** * Checks if a concurrent map contains a key and creates a corresponding * value if not, suppressing checked exceptions. This method calls * {@code createIfAbsent()}. If a {@link ConcurrentException} is thrown, it * is caught and re-thrown as a {@link ConcurrentRuntimeException}. * * @param <K> the type of the keys of the map * @param <V> the type of the values of the map * @param map the map to be modified * @param key the key of the value to be added * @param init the {@link ConcurrentInitializer} for creating the value * @return the value stored in the map after this operation; this may or may * not be the object created by the {@link ConcurrentInitializer} * @throws ConcurrentRuntimeException if the initializer throws an exception */ public static <K, V> V createIfAbsentUnchecked(final ConcurrentMap<K, V> map, final K key, final ConcurrentInitializer<V> init) { try { return createIfAbsent(map, key, init); } catch (final ConcurrentException cex) { throw new ConcurrentRuntimeException(cex.getCause()); } }
/** * Checks if a concurrent map contains a key and creates a corresponding * value if not, suppressing checked exceptions. This method calls * {@code createIfAbsent()}. If a {@link ConcurrentException} is thrown, it * is caught and re-thrown as a {@link ConcurrentRuntimeException}. * * @param <K> the type of the keys of the map * @param <V> the type of the values of the map * @param map the map to be modified * @param key the key of the value to be added * @param init the {@link ConcurrentInitializer} for creating the value * @return the value stored in the map after this operation; this may or may * not be the object created by the {@link ConcurrentInitializer} * @throws ConcurrentRuntimeException if the initializer throws an exception */ public static <K, V> V createIfAbsentUnchecked(final ConcurrentMap<K, V> map, final K key, final ConcurrentInitializer<V> init) { try { return createIfAbsent(map, key, init); } catch (final ConcurrentException cex) { throw new ConcurrentRuntimeException(cex.getCause()); } }