Java 类com.google.common.collect.MapMakerInternalMap.WeakValueEntry 实例源码

项目:guava-mock    文件:MapMakerInternalMapTest.java   
public void testDrainValueReferenceQueueOnWrite() {
  for (MapMaker maker : allWeakValueStrengthMakers()) {
    MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(maker.concurrencyLevel(1));
    if (maker.getValueStrength() == Strength.WEAK) {
      Segment<Object, Object, ?, ?> segment = map.segments[0];

      Object keyOne = new Object();
      int hashOne = map.hash(keyOne);
      Object valueOne = new Object();
      Object keyTwo = new Object();
      Object valueTwo = new Object();

      map.put(keyOne, valueOne);
      @SuppressWarnings("unchecked")
      WeakValueEntry<Object, Object, ?> entry =
          (WeakValueEntry<Object, Object, ?>) segment.getEntry(keyOne, hashOne);
      WeakValueReference<Object, Object, ?> valueReference = entry.getValueReference();

      @SuppressWarnings("unchecked")
      Reference<Object> reference = (Reference) valueReference;
      reference.enqueue();

      map.put(keyTwo, valueTwo);
      assertFalse(map.containsKey(keyOne));
      assertFalse(map.containsValue(valueOne));
      assertNull(map.get(keyOne));
      assertEquals(1, map.size());
      assertNull(segment.getValueReferenceQueueForTesting().poll());
    }
  }
}
项目:guava-mock    文件:MapMakerInternalMapTest.java   
public void testDrainValueReferenceQueueOnRead() {
  for (MapMaker maker : allWeakValueStrengthMakers()) {
    MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(maker.concurrencyLevel(1));
    if (maker.getValueStrength() == Strength.WEAK) {
      Segment<Object, Object, ?, ?> segment = map.segments[0];

      Object keyOne = new Object();
      int hashOne = map.hash(keyOne);
      Object valueOne = new Object();
      Object keyTwo = new Object();

      map.put(keyOne, valueOne);
      @SuppressWarnings("unchecked")
      WeakValueEntry<Object, Object, ?> entry =
          (WeakValueEntry<Object, Object, ?>) segment.getEntry(keyOne, hashOne);
      WeakValueReference<Object, Object, ?> valueReference = entry.getValueReference();

      @SuppressWarnings("unchecked")
      Reference<Object> reference = (Reference) valueReference;
      reference.enqueue();

      for (int i = 0; i < SMALL_MAX_SIZE; i++) {
        Object unused = map.get(keyTwo);
      }
      assertFalse(map.containsKey(keyOne));
      assertFalse(map.containsValue(valueOne));
      assertNull(map.get(keyOne));
      assertEquals(0, map.size());
      assertNull(segment.getValueReferenceQueueForTesting().poll());
    }
  }
}
项目:googles-monorepo-demo    文件:MapMakerInternalMapTest.java   
public void testDrainValueReferenceQueueOnWrite() {
  for (MapMaker maker : allWeakValueStrengthMakers()) {
    MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(maker.concurrencyLevel(1));
    if (maker.getValueStrength() == Strength.WEAK) {
      Segment<Object, Object, ?, ?> segment = map.segments[0];

      Object keyOne = new Object();
      int hashOne = map.hash(keyOne);
      Object valueOne = new Object();
      Object keyTwo = new Object();
      Object valueTwo = new Object();

      map.put(keyOne, valueOne);
      @SuppressWarnings("unchecked")
      WeakValueEntry<Object, Object, ?> entry =
          (WeakValueEntry<Object, Object, ?>) segment.getEntry(keyOne, hashOne);
      WeakValueReference<Object, Object, ?> valueReference = entry.getValueReference();

      @SuppressWarnings("unchecked")
      Reference<Object> reference = (Reference) valueReference;
      reference.enqueue();

      map.put(keyTwo, valueTwo);
      assertFalse(map.containsKey(keyOne));
      assertFalse(map.containsValue(valueOne));
      assertNull(map.get(keyOne));
      assertEquals(1, map.size());
      assertNull(segment.getValueReferenceQueueForTesting().poll());
    }
  }
}
项目:googles-monorepo-demo    文件:MapMakerInternalMapTest.java   
public void testDrainValueReferenceQueueOnRead() {
  for (MapMaker maker : allWeakValueStrengthMakers()) {
    MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(maker.concurrencyLevel(1));
    if (maker.getValueStrength() == Strength.WEAK) {
      Segment<Object, Object, ?, ?> segment = map.segments[0];

      Object keyOne = new Object();
      int hashOne = map.hash(keyOne);
      Object valueOne = new Object();
      Object keyTwo = new Object();

      map.put(keyOne, valueOne);
      @SuppressWarnings("unchecked")
      WeakValueEntry<Object, Object, ?> entry =
          (WeakValueEntry<Object, Object, ?>) segment.getEntry(keyOne, hashOne);
      WeakValueReference<Object, Object, ?> valueReference = entry.getValueReference();

      @SuppressWarnings("unchecked")
      Reference<Object> reference = (Reference) valueReference;
      reference.enqueue();

      for (int i = 0; i < SMALL_MAX_SIZE; i++) {
        Object unused = map.get(keyTwo);
      }
      assertFalse(map.containsKey(keyOne));
      assertFalse(map.containsValue(valueOne));
      assertNull(map.get(keyOne));
      assertEquals(0, map.size());
      assertNull(segment.getValueReferenceQueueForTesting().poll());
    }
  }
}
项目:guava    文件:MapMakerInternalMapTest.java   
public void testDrainValueReferenceQueueOnWrite() {
  for (MapMaker maker : allWeakValueStrengthMakers()) {
    MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(maker.concurrencyLevel(1));
    if (maker.getValueStrength() == Strength.WEAK) {
      Segment<Object, Object, ?, ?> segment = map.segments[0];

      Object keyOne = new Object();
      int hashOne = map.hash(keyOne);
      Object valueOne = new Object();
      Object keyTwo = new Object();
      Object valueTwo = new Object();

      map.put(keyOne, valueOne);
      @SuppressWarnings("unchecked")
      WeakValueEntry<Object, Object, ?> entry =
          (WeakValueEntry<Object, Object, ?>) segment.getEntry(keyOne, hashOne);
      WeakValueReference<Object, Object, ?> valueReference = entry.getValueReference();

      @SuppressWarnings("unchecked")
      Reference<Object> reference = (Reference) valueReference;
      reference.enqueue();

      map.put(keyTwo, valueTwo);
      assertFalse(map.containsKey(keyOne));
      assertFalse(map.containsValue(valueOne));
      assertNull(map.get(keyOne));
      assertEquals(1, map.size());
      assertNull(segment.getValueReferenceQueueForTesting().poll());
    }
  }
}
项目:guava    文件:MapMakerInternalMapTest.java   
public void testDrainValueReferenceQueueOnRead() {
  for (MapMaker maker : allWeakValueStrengthMakers()) {
    MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(maker.concurrencyLevel(1));
    if (maker.getValueStrength() == Strength.WEAK) {
      Segment<Object, Object, ?, ?> segment = map.segments[0];

      Object keyOne = new Object();
      int hashOne = map.hash(keyOne);
      Object valueOne = new Object();
      Object keyTwo = new Object();

      map.put(keyOne, valueOne);
      @SuppressWarnings("unchecked")
      WeakValueEntry<Object, Object, ?> entry =
          (WeakValueEntry<Object, Object, ?>) segment.getEntry(keyOne, hashOne);
      WeakValueReference<Object, Object, ?> valueReference = entry.getValueReference();

      @SuppressWarnings("unchecked")
      Reference<Object> reference = (Reference) valueReference;
      reference.enqueue();

      for (int i = 0; i < SMALL_MAX_SIZE; i++) {
        Object unused = map.get(keyTwo);
      }
      assertFalse(map.containsKey(keyOne));
      assertFalse(map.containsValue(valueOne));
      assertNull(map.get(keyOne));
      assertEquals(0, map.size());
      assertNull(segment.getValueReferenceQueueForTesting().poll());
    }
  }
}
项目:guava    文件:MapMakerInternalMapTest.java   
public void testDrainValueReferenceQueueOnWrite() {
  for (MapMaker maker : allWeakValueStrengthMakers()) {
    MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(maker.concurrencyLevel(1));
    if (maker.getValueStrength() == Strength.WEAK) {
      Segment<Object, Object, ?, ?> segment = map.segments[0];

      Object keyOne = new Object();
      int hashOne = map.hash(keyOne);
      Object valueOne = new Object();
      Object keyTwo = new Object();
      Object valueTwo = new Object();

      map.put(keyOne, valueOne);
      @SuppressWarnings("unchecked")
      WeakValueEntry<Object, Object, ?> entry =
          (WeakValueEntry<Object, Object, ?>) segment.getEntry(keyOne, hashOne);
      WeakValueReference<Object, Object, ?> valueReference = entry.getValueReference();

      @SuppressWarnings("unchecked")
      Reference<Object> reference = (Reference) valueReference;
      reference.enqueue();

      map.put(keyTwo, valueTwo);
      assertFalse(map.containsKey(keyOne));
      assertFalse(map.containsValue(valueOne));
      assertNull(map.get(keyOne));
      assertEquals(1, map.size());
      assertNull(segment.getValueReferenceQueueForTesting().poll());
    }
  }
}
项目:guava    文件:MapMakerInternalMapTest.java   
public void testDrainValueReferenceQueueOnRead() {
  for (MapMaker maker : allWeakValueStrengthMakers()) {
    MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(maker.concurrencyLevel(1));
    if (maker.getValueStrength() == Strength.WEAK) {
      Segment<Object, Object, ?, ?> segment = map.segments[0];

      Object keyOne = new Object();
      int hashOne = map.hash(keyOne);
      Object valueOne = new Object();
      Object keyTwo = new Object();

      map.put(keyOne, valueOne);
      @SuppressWarnings("unchecked")
      WeakValueEntry<Object, Object, ?> entry =
          (WeakValueEntry<Object, Object, ?>) segment.getEntry(keyOne, hashOne);
      WeakValueReference<Object, Object, ?> valueReference = entry.getValueReference();

      @SuppressWarnings("unchecked")
      Reference<Object> reference = (Reference) valueReference;
      reference.enqueue();

      for (int i = 0; i < SMALL_MAX_SIZE; i++) {
        Object unused = map.get(keyTwo);
      }
      assertFalse(map.containsKey(keyOne));
      assertFalse(map.containsValue(valueOne));
      assertNull(map.get(keyOne));
      assertEquals(0, map.size());
      assertNull(segment.getValueReferenceQueueForTesting().poll());
    }
  }
}