Refine search
return putIfAbsent(map, key, init.get());
/** * 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 extractCause() if the cause of the passed in exception is null. */ @Test public void testExtractCauseNullCause() { assertNull("Non null result", ConcurrentUtils .extractCause(new ExecutionException("Test", null))); }
/** * 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 handleCause() for a null parameter or a null cause. In this case * the method should do nothing. We can only test that no exception is * thrown. * * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it */ @Test public void testHandleCauseNull() throws ConcurrentException { ConcurrentUtils.handleCause(null); ConcurrentUtils.handleCause(new ExecutionException("Test", null)); }
/** * Tests extractCauseUnchecked() if the cause of the passed in exception is null. */ @Test public void testExtractCauseUncheckedNullCause() { assertNull("Non null result", ConcurrentUtils .extractCauseUnchecked(new ExecutionException("Test", null))); }
/** * Tests constant future. * * @throws java.lang.Exception so we don't have to catch it */ @Test public void testConstantFuture_Integer() throws Exception { final Integer value = Integer.valueOf(5); final Future<Integer> test = ConcurrentUtils.constantFuture(value); assertTrue(test.isDone()); assertSame(value, test.get()); assertSame(value, test.get(1000, TimeUnit.SECONDS)); assertSame(value, test.get(1000, null)); assertFalse(test.isCancelled()); assertFalse(test.cancel(true)); assertFalse(test.cancel(false)); }
/** * Creates a new instance of {@code ConcurrentRuntimeException} and * initializes it with the given message and cause. * * @param msg the error message * @param cause the cause of this exception * @throws IllegalArgumentException if the cause is not a checked exception */ public ConcurrentRuntimeException(final String msg, final Throwable cause) { super(msg, ConcurrentUtils.checkedException(cause)); } }
/** * 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()); } }
/** * Handles the specified {@code ExecutionException}. This method calls * {@link #extractCause(ExecutionException)} for obtaining the cause of the * exception - which might already cause an unchecked exception or an error * being thrown. If the cause is a checked exception however, it is wrapped * in a {@code ConcurrentException}, which is thrown. If the passed in * exception is <b>null</b> or has no cause, the method simply returns * without throwing an exception. * * @param ex the exception to be handled * @throws ConcurrentException if the cause of the {@code * ExecutionException} is a checked exception */ public static void handleCause(final ExecutionException ex) throws ConcurrentException { final ConcurrentException cex = extractCause(ex); if (cex != null) { throw cex; } }
/** * Handles the specified {@code ExecutionException} and transforms it into a * runtime exception. This method works exactly like * {@link #handleCause(ExecutionException)}, but instead of a * {@link ConcurrentException} it throws a * {@link ConcurrentRuntimeException}. This is an alternative for client * code that does not want to deal with checked exceptions. * * @param ex the exception to be handled * @throws ConcurrentRuntimeException if the cause of the {@code * ExecutionException} is a checked exception; this exception is then * wrapped in the thrown runtime exception */ public static void handleCauseUnchecked(final ExecutionException ex) { final ConcurrentRuntimeException crex = extractCauseUnchecked(ex); if (crex != null) { throw crex; } }
/** * Returns the result of the background initialization. This method blocks * until initialization is complete. If the background processing caused a * runtime exception, it is directly thrown by this method. Checked * exceptions, including {@code InterruptedException} are wrapped in a * {@link ConcurrentException}. Calling this method before {@link #start()} * was called causes an {@code IllegalStateException} exception to be * thrown. * * @return the object produced by this initializer * @throws ConcurrentException if a checked exception occurred during * background processing * @throws IllegalStateException if {@link #start()} has not been called */ @Override public T get() throws ConcurrentException { try { return getFuture().get(); } catch (final ExecutionException execex) { ConcurrentUtils.handleCause(execex); return null; // should not be reached } catch (final InterruptedException iex) { // reset interrupted state Thread.currentThread().interrupt(); throw new ConcurrentException(iex); } }
/** * Inspects the cause of the specified {@code ExecutionException} and * creates a {@code ConcurrentException} with the checked cause if * necessary. This method performs the following checks on the cause of the * passed in exception: * <ul> * <li>If the passed in exception is <b>null</b> or the cause is * <b>null</b>, this method returns <b>null</b>.</li> * <li>If the cause is a runtime exception, it is directly thrown.</li> * <li>If the cause is an error, it is directly thrown, too.</li> * <li>In any other case the cause is a checked exception. The method then * creates a {@link ConcurrentException}, initializes it with the cause, and * returns it.</li> * </ul> * * @param ex the exception to be processed * @return a {@code ConcurrentException} with the checked cause */ public static ConcurrentException extractCause(final ExecutionException ex) { if (ex == null || ex.getCause() == null) { return null; } throwCause(ex); return new ConcurrentException(ex.getMessage(), ex.getCause()); }
/** * Tests handleCause() if the cause is an error. * * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it */ @Test public void testHandleCauseError() throws ConcurrentException { final Error err = new AssertionError("Test"); try { ConcurrentUtils.handleCause(new ExecutionException(err)); fail("Error not thrown!"); } catch (final Error e) { assertEquals("Wrong error", err, e); } }
/** * 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()); }
/** * Tests constant future. * * @throws java.lang.Exception so we don't have to catch it */ @Test public void testConstantFuture_null() throws Exception { final Integer value = null; final Future<Integer> test = ConcurrentUtils.constantFuture(value); assertTrue(test.isDone()); assertSame(value, test.get()); assertSame(value, test.get(1000, TimeUnit.SECONDS)); assertSame(value, test.get(1000, null)); assertFalse(test.isCancelled()); assertFalse(test.cancel(true)); assertFalse(test.cancel(false)); }
/** * Creates a new instance of {@code ConcurrentException} and initializes it * with the given message and cause. * * @param msg the error message * @param cause the cause of this exception * @throws IllegalArgumentException if the cause is not a checked exception */ public ConcurrentException(final String msg, final Throwable cause) { super(msg, ConcurrentUtils.checkedException(cause)); } }
/** * Tests a successful initialize() operation. * * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it */ @Test public void testInitialize() throws ConcurrentException { @SuppressWarnings("unchecked") final ConcurrentInitializer<Object> init = EasyMock .createMock(ConcurrentInitializer.class); final Object result = new Object(); EasyMock.expect(init.get()).andReturn(result); EasyMock.replay(init); assertSame("Wrong result object", result, ConcurrentUtils .initialize(init)); EasyMock.verify(init); }
/** * Tests extractCause() for a null exception. */ @Test public void testExtractCauseNull() { assertNull("Non null result", ConcurrentUtils.extractCause(null)); }
/** * Tests extractCauseUnchecked() for a null exception. */ @Test public void testExtractCauseUncheckedNull() { assertNull("Non null result", ConcurrentUtils.extractCauseUnchecked(null)); }