@Test public void shouldResultInNotEmptyAfterIncAndDecWhenHaveKey() { map.put(1, 1); map.incrementAndGet(1); map.decrementAndGet(1); assertEquals(1, map.get(1)); assertEquals(1, map.size()); }
public int decRefToStream(final int streamId) { final int count = refCountByStreamIdMap.decrementAndGet(streamId); if (-1 == count) { refCountByStreamIdMap.remove(streamId); throw new IllegalStateException("could not find stream Id to decrement: " + streamId); } return count; }
/** * Try get a value for a key and if not present then apply mapping function. * * @param key to search on. * @param mappingFunction to provide a value if the get returns null. * @return the value if found otherwise the missing value. */ public int computeIfAbsent(final int key, final IntUnaryOperator mappingFunction) { int value = get(key); if (value == initialValue) { value = mappingFunction.applyAsInt(key); if (value != initialValue) { put(key, value); } } return value; }
/** * Compact the backing arrays by rehashing with a capacity just larger than current size * and giving consideration to the load factor. */ public void compact() { @DoNotSub final int idealCapacity = (int)Math.round(size() * (1.0d / loadFactor)); rehash(findNextPositivePowerOfTwo(Math.max(MIN_CAPACITY, idealCapacity))); }
@Test public void shouldContainValueForIncAndDecEntries() { map.incrementAndGet(1); map.getAndIncrement(2); map.getAndAdd(3, 2); map.addAndGet(4, 3); map.decrementAndGet(5); map.getAndDecrement(6); assertTrue(map.containsKey(1)); assertTrue(map.containsKey(2)); assertTrue(map.containsKey(3)); assertTrue(map.containsKey(4)); assertTrue(map.containsKey(5)); assertTrue(map.containsKey(6)); }
@Test public void shouldAllowInitialValueAsKey() { map.put(INITIAL_VALUE, 1); assertEquals(1, map.get(INITIAL_VALUE)); assertTrue(map.containsKey(INITIAL_VALUE)); assertEquals(1, map.size()); final int[] tuple = new int[2]; map.forEach((k, v) -> { tuple[0] = k; tuple[1] = v; }); assertEquals(INITIAL_VALUE, tuple[0]); assertEquals(1, tuple[1]); assertEquals(1, map.remove(INITIAL_VALUE)); assertEquals(0, map.size()); assertEquals(INITIAL_VALUE, map.get(INITIAL_VALUE)); }
@Test public void shouldRemoveEntryAfterDecToInitialVal() { map.put(1, INITIAL_VALUE + 1); map.decrementAndGet(1); assertEquals(INITIAL_VALUE, map.get(1)); assertTrue(map.isEmpty()); assertFalse(map.containsKey(1)); }
@Test public void shouldRemoveEntryAfterIncToInitialVal() { map.put(1, INITIAL_VALUE - 1); map.incrementAndGet(1); assertEquals(INITIAL_VALUE, map.get(1)); assertTrue(map.isEmpty()); assertFalse(map.containsKey(1)); }
@Test public void shouldResultInEmptyAfterIncAndDecWhenEmpty() { map.incrementAndGet(1); map.decrementAndGet(1); assertTrue(map.isEmpty()); map.getAndIncrement(1); map.getAndDecrement(1); assertTrue(map.isEmpty()); map.incrementAndGet(1); map.getAndDecrement(1); assertTrue(map.isEmpty()); map.getAndIncrement(1); map.decrementAndGet(1); assertTrue(map.isEmpty()); }
@Test public void removeShouldRemoveEntry() { map.put(1, 2); map.remove(1); assertTrue(map.isEmpty()); assertFalse(map.containsKey(1)); assertFalse(map.containsValue(2)); }
public int streamCount() { return refCountByStreamIdMap.size() + refCountByStreamIdAndSessionIdMap.size(); }
/** * Convenience version of {@link #getAndAdd(int, int)} (key, -1). * * @param key for the counter. * @return the old value. */ public int getAndDecrement(final int key) { return getAndAdd(key, -1); }
public boolean shouldBeClosed() { return refCountByStreamIdMap.isEmpty() && refCountByStreamIdAndSessionIdMap.isEmpty() && !statusIndicator.isClosed(); }