/** * Invokes the specified {@code ConcurrentInitializer} and returns the * object produced by the initializer. This method just invokes the {@code * get()} method of the given {@code ConcurrentInitializer}. It is * <b>null</b>-safe: if the argument is <b>null</b>, result is also * <b>null</b>. * * @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 ConcurrentException if the {@code ConcurrentInitializer} throws * an exception */ public static <T> T initialize(final ConcurrentInitializer<T> initializer) throws ConcurrentException { return initializer != null ? initializer.get() : null; }
return putIfAbsent(map, key, init.get());
/** * Tests whether sequential get() invocations always return the same * instance. * * @throws org.apache.commons.lang3.concurrent.ConcurrentException because the object under test may throw it */ @Test public void testGetMultipleTimes() throws ConcurrentException { final ConcurrentInitializer<Object> initializer = createInitializer(); final Object obj = initializer.get(); for (int i = 0; i < 10; i++) { assertEquals("Got different object at " + i, obj, initializer.get()); } }
/** * Tests a successful initializeUnchecked() operation. * * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it */ @Test public void testInitializeUnchecked() 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 .initializeUnchecked(init)); EasyMock.verify(init); }
/** * 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 createIfAbsent() if the map does not contain the key in question. * * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it */ @Test public void testCreateIfAbsentKeyNotPresent() throws ConcurrentException { @SuppressWarnings("unchecked") final ConcurrentInitializer<Integer> init = EasyMock .createMock(ConcurrentInitializer.class); final String key = "testKey"; final Integer value = 42; EasyMock.expect(init.get()).andReturn(value); EasyMock.replay(init); final ConcurrentMap<String, Integer> map = new ConcurrentHashMap<>(); assertEquals("Wrong result", value, ConcurrentUtils.createIfAbsent(map, key, init)); assertEquals("Wrong value in map", value, map.get(key)); EasyMock.verify(init); }
/** * Tests a simple invocation of the get() method. * * @throws org.apache.commons.lang3.concurrent.ConcurrentException because the object under test may throw it */ @Test public void testGet() throws ConcurrentException { assertNotNull("No managed object", createInitializer().get()); }
/** * 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); }
/** * 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); } }
final Object managedObject = initializer.get(); for (final GetThread t : threads) { assertEquals("Wrong object", managedObject, t.object);
/** * Invokes the specified {@code ConcurrentInitializer} and returns the * object produced by the initializer. This method just invokes the {@code * get()} method of the given {@code ConcurrentInitializer}. It is * <b>null</b>-safe: if the argument is <b>null</b>, result is also * <b>null</b>. * * @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 ConcurrentException if the {@code ConcurrentInitializer} throws * an exception */ public static <T> T initialize(final ConcurrentInitializer<T> initializer) throws ConcurrentException { return initializer != null ? initializer.get() : null; }
/** * Invokes the specified {@code ConcurrentInitializer} and returns the * object produced by the initializer. This method just invokes the {@code * get()} method of the given {@code ConcurrentInitializer}. It is * <b>null</b>-safe: if the argument is <b>null</b>, result is also * <b>null</b>. * * @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 ConcurrentException if the {@code ConcurrentInitializer} throws * an exception */ public static <T> T initialize(final ConcurrentInitializer<T> initializer) throws ConcurrentException { return initializer != null ? initializer.get() : null; }
/** * Invokes the specified {@code ConcurrentInitializer} and returns the * object produced by the initializer. This method just invokes the {@code * get()} method of the given {@code ConcurrentInitializer}. It is * <b>null</b>-safe: if the argument is <b>null</b>, result is also * <b>null</b>. * * @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 ConcurrentException if the {@code ConcurrentInitializer} throws * an exception */ public static <T> T initialize(final ConcurrentInitializer<T> initializer) throws ConcurrentException { return initializer != null ? initializer.get() : null; }
return putIfAbsent(map, key, init.get());
return putIfAbsent(map, key, init.get());
return putIfAbsent(map, key, init.get());