@Override public V computeIfPresent( K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { synchronized (mutex) { return delegate().computeIfPresent(key, remappingFunction); } }
@Override public V computeIfPresent( K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { synchronized (mutex) { return delegate().computeIfPresent(key, remappingFunction); } }
private void cleanupMonitorListeners( Object monitorListener, Method key ) { methodMonitorListeners.computeIfPresent( key, ( method1, handlers ) -> { handlers.removeIf( handler -> monitorListener.equals( handler.getMonitorListener() ) ); return handlers.isEmpty() ? null : handlers; } ); }
public void removeSegment(String serverName, DataSegment dataSegment) { serversCostCache.computeIfPresent( serverName, (s, builder) -> builder.removeSegment(dataSegment).isEmpty() ? null : builder ); }
@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS}) public void testComputeIfPresent_nullKeySupportedAbsent() { assertNull( "computeIfPresent(null, function) should return null", getMap() .computeIfPresent( null, (k, v) -> { throw new AssertionFailedError(); })); expectUnchanged(); }
@MapFeature.Require(absent = SUPPORTS_PUT) public void testComputeIfPresent_unsupportedAbsent() { try { getMap() .computeIfPresent( k3(), (k, v) -> { throw new AssertionFailedError(); }); } catch (UnsupportedOperationException tolerated) { } expectUnchanged(); }
@MapFeature.Require(SUPPORTS_PUT) public void testComputeIfPresent_supportedAbsent() { assertNull( "computeIfPresent(notPresent, function) should return null", getMap() .computeIfPresent( k3(), (k, v) -> { throw new AssertionFailedError(); })); expectUnchanged(); }
@Override public Integer apply(Integer key, Integer value) { if (recursed) { throw new StackOverflowError(); } recursed = true; return map.computeIfPresent(key, this); } };
public Builder removeSegment(DataSegment dataSegment) { allSegmentsCostCache.removeSegment(dataSegment); segmentsPerDataSource.computeIfPresent( dataSegment.getDataSource(), (ds, builder) -> builder.removeSegment(dataSegment).isEmpty() ? null : builder ); return this; }
@CheckNoWriter @CheckNoStats @Test(dataProvider = "caches") @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void computeIfPresent_absent(Map<Integer, Integer> map, CacheContext context) { assertThat(map.computeIfPresent(context.absentKey(), (key, value) -> value), is(nullValue())); assertThat(map.get(context.absentKey()), is(nullValue())); assertThat(map.size(), is(context.original().size())); }
@MapFeature.Require(absent = SUPPORTS_PUT) @CollectionSize.Require(absent = ZERO) public void testComputeIfPresent_unsupportedPresent() { try { getMap().computeIfPresent(k0(), (k, v) -> v3()); fail("Expected UnsupportedOperationException"); } catch (UnsupportedOperationException expected) { } expectUnchanged(); } }
@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES}) @CollectionSize.Require(absent = ZERO) public void testComputeIfPresent_nullTreatedAsAbsent() { initMapWithNullValue(); assertNull( "computeIfPresent(presentAssignedToNull, function) should return null", getMap() .computeIfPresent( getKeyForNullValue(), (k, v) -> { throw new AssertionFailedError(); })); expectReplacement(entry(getKeyForNullValue(), null)); }
@Override public Integer apply(Integer key, Integer value) { if (++recursed == 2) { throw new StackOverflowError(); } return map.computeIfPresent(context.lastKey(), this); } };
@CheckNoWriter @CheckNoStats @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) @Test(dataProvider = "caches", expectedExceptions = NullPointerException.class) public void computeIfPresent_nullKey(Map<Integer, Integer> map, CacheContext context) { map.computeIfPresent(null, (key, value) -> -key); }
@CacheSpec @Test(dataProvider = "caches") public void compute_error(Map<Integer, Integer> map, CacheContext context) { try { map.compute(context.absentKey(), (key, value) -> { throw new Error(); }); } catch (Error e) {} assertThat(map, is(equalTo(context.original()))); assertThat(context, both(hasMissCount(0)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(1))); assertThat(map.computeIfPresent(context.absentKey(), (k, v) -> -k), is(nullValue())); }
@Test(dataProvider = "caches") @CacheSpec(population = { Population.SINGLETON, Population.PARTIAL, Population.FULL }) public void computeIfPresent_error(Map<Integer, Integer> map, CacheContext context) { try { map.computeIfPresent(context.firstKey(), (key, value) -> { throw new Error(); }); } catch (Error e) {} assertThat(map, is(equalTo(context.original()))); assertThat(context, both(hasMissCount(0)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(1))); assertThat(map.computeIfPresent(context.firstKey(), (k, v) -> -k), is(-context.firstKey())); }
@MapFeature.Require(SUPPORTS_PUT) @CollectionSize.Require(absent = ZERO) public void testComputeIfPresent_functionReturnsNull() { assertNull( "computeIfPresent(present, returnsNull) should return null", getMap() .computeIfPresent( k0(), (k, v) -> { assertEquals(k0(), k); assertEquals(v0(), v); return null; })); expectMissing(e0()); }
@MapFeature.Require(SUPPORTS_PUT) @CollectionSize.Require(absent = ZERO) public void testComputeIfPresent_functionThrows() { try { getMap() .computeIfPresent( k0(), (k, v) -> { assertEquals(k0(), k); assertEquals(v0(), v); throw new ExpectedException(); }); fail("Expected ExpectedException"); } catch (ExpectedException expected) { } expectUnchanged(); }
@MapFeature.Require(SUPPORTS_PUT) @CollectionSize.Require(absent = ZERO) public void testComputeIfPresent_supportedPresent() { assertEquals( "computeIfPresent(present, function) should return new value", v3(), getMap() .computeIfPresent( k0(), (k, v) -> { assertEquals(k0(), k); assertEquals(v0(), v); return v3(); })); expectReplacement(entry(k0(), v3())); }
@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS}) @CollectionSize.Require(absent = ZERO) public void testComputeIfPresent_nullKeySupportedPresent() { initMapWithNullKey(); assertEquals( "computeIfPresent(null, function) should return new value", v3(), getMap() .computeIfPresent( null, (k, v) -> { assertNull(k); assertEquals(getValueForNullKey(), v); return v3(); })); Entry<K, V>[] expected = createArrayWithNullKey(); expected[getNullLocation()] = entry(null, v3()); expectContents(expected); }