/** * Tests createIfAbsentUnchecked() if no exception is thrown. */ @Test public void testCreateIfAbsentUncheckedSuccess() { final String key = "testKey"; final Integer value = 42; final ConcurrentMap<String, Integer> map = new ConcurrentHashMap<>(); assertEquals("Wrong result", value, ConcurrentUtils.createIfAbsentUnchecked(map, key, new ConstantInitializer<>(value))); assertEquals("Wrong value in map", value, map.get(key)); }
/** * 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); } }
/** * Returns the group key table for specified device. * * @param deviceId identifier of the device * @return Map representing group key table of given device. */ private ConcurrentMap<GroupKey, StoredGroupEntry> getGroupKeyTable(DeviceId deviceId) { return createIfAbsentUnchecked(groupEntriesByKey, deviceId, lazyEmptyGroupKeyTable()); }
/** * Returns the group id table for specified device. * * @param deviceId identifier of the device * @return Map representing group key table of given device. */ private ConcurrentMap<GroupId, StoredGroupEntry> getGroupIdTable(DeviceId deviceId) { return createIfAbsentUnchecked(groupEntriesById, deviceId, lazyEmptyGroupIdTable()); }
/** * Returns the pending group key table for specified device. * * @param deviceId identifier of the device * @return Map representing group key table of given device. */ private ConcurrentMap<GroupKey, StoredGroupEntry> getPendingGroupKeyTable(DeviceId deviceId) { return createIfAbsentUnchecked(pendingGroupEntriesByKey, deviceId, lazyEmptyPendingGroupKeyTable()); }
/** * Returns the flow table for specified device. * * @param deviceId identifier of the device * @return Map representing Flow Table of given device. */ private ConcurrentMap<FlowId, List<StoredFlowEntry>> getFlowTable(DeviceId deviceId) { return createIfAbsentUnchecked(flowEntries, deviceId, lazyEmptyFlowTable()); }
private ConcurrentMap<PortNumber, Port> getPortMap(DeviceId deviceId) { return createIfAbsentUnchecked(devicePorts, deviceId, NewConcurrentHashMap.<PortNumber, Port>ifNeeded()); }
/** * Returns the extraneous group id table for specified device. * * @param deviceId identifier of the device * @return Map representing group key table of given device. */ private ConcurrentMap<GroupId, Group> getExtraneousGroupIdTable(DeviceId deviceId) { return createIfAbsentUnchecked(extraneousGroupEntriesById, deviceId, lazyEmptyExtraneousGroupIdTable()); }