本文整理汇总了Java中com.google.common.collect.MapMakerInternalMap.InternalEntry类的典型用法代码示例。如果您正苦于以下问题:Java InternalEntry类的具体用法?Java InternalEntry怎么用?Java InternalEntry使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
InternalEntry类属于com.google.common.collect.MapMakerInternalMap包,在下文中一共展示了InternalEntry类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: intern
import com.google.common.collect.MapMakerInternalMap.InternalEntry; //导入依赖的package包/类
@Override
public E intern(E sample) {
while (true) {
// trying to read the canonical...
InternalEntry<E, Dummy, ?> entry = map.getEntry(sample);
if (entry != null) {
E canonical = entry.getKey();
if (canonical != null) { // only matters if weak/soft keys are used
return canonical;
}
}
// didn't see it, trying to put it instead...
Dummy sneaky = map.putIfAbsent(sample, Dummy.VALUE);
if (sneaky == null) {
return sample;
} else {
/* Someone beat us to it! Trying again...
*
* Technically this loop not guaranteed to terminate, so theoretically (extremely
* unlikely) this thread might starve, but even then, there is always going to be another
* thread doing progress here.
*/
}
}
}
示例2: testClear
import com.google.common.collect.MapMakerInternalMap.InternalEntry; //导入依赖的package包/类
public void testClear() {
MapMakerInternalMap<Object, Object, ?, ?> map =
makeMap(createMapMaker().concurrencyLevel(1).initialCapacity(1));
Segment<Object, Object, ?, ?> segment = map.segments[0];
AtomicReferenceArray<? extends InternalEntry<Object, Object, ?>> table = segment.table;
assertEquals(1, table.length());
Object key = new Object();
Object value = new Object();
int hash = map.hash(key);
InternalEntry<Object, Object, ?> entry = segment.newEntryForTesting(key, hash, null);
segment.setValueForTesting(entry, value);
segment.setTableEntryForTesting(0, entry);
segment.readCount.incrementAndGet();
segment.count = 1;
assertSame(entry, table.get(0));
segment.clear();
assertNull(table.get(0));
assertEquals(0, segment.readCount.get());
assertEquals(0, segment.count);
}
示例3: testRemoveEntry
import com.google.common.collect.MapMakerInternalMap.InternalEntry; //导入依赖的package包/类
public void testRemoveEntry() {
MapMakerInternalMap<Object, Object, ?, ?> map =
makeMap(createMapMaker().concurrencyLevel(1).initialCapacity(1));
Segment<Object, Object, ?, ?> segment = map.segments[0];
AtomicReferenceArray<? extends InternalEntry<Object, Object, ?>> table = segment.table;
assertEquals(1, table.length());
Object key = new Object();
Object value = new Object();
int hash = map.hash(key);
InternalEntry<Object, Object, ?> entry = segment.newEntryForTesting(key, hash, null);
segment.setValueForTesting(entry, value);
// remove absent
assertFalse(segment.removeTableEntryForTesting(entry));
segment.setTableEntryForTesting(0, entry);
segment.count = 1;
assertTrue(segment.removeTableEntryForTesting(entry));
assertEquals(0, segment.count);
assertNull(table.get(0));
}
示例4: testNewEntry
import com.google.common.collect.MapMakerInternalMap.InternalEntry; //导入依赖的package包/类
public void testNewEntry() {
for (MapMaker maker : allWeakValueStrengthMakers()) {
MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(maker);
Segment<Object, Object, ?, ?> segment = map.segments[0];
Object keyOne = new Object();
Object valueOne = new Object();
int hashOne = map.hash(keyOne);
InternalEntry<Object, Object, ?> entryOne = segment.newEntryForTesting(keyOne, hashOne, null);
WeakValueReference<Object, Object, ?> valueRefOne =
segment.newWeakValueReferenceForTesting(entryOne, valueOne);
assertSame(valueOne, valueRefOne.get());
segment.setWeakValueReferenceForTesting(entryOne, valueRefOne);
assertSame(keyOne, entryOne.getKey());
assertEquals(hashOne, entryOne.getHash());
assertNull(entryOne.getNext());
assertSame(valueRefOne, segment.getWeakValueReferenceForTesting(entryOne));
Object keyTwo = new Object();
Object valueTwo = new Object();
int hashTwo = map.hash(keyTwo);
InternalEntry<Object, Object, ?> entryTwo =
segment.newEntryForTesting(keyTwo, hashTwo, entryOne);
WeakValueReference<Object, Object, ?> valueRefTwo =
segment.newWeakValueReferenceForTesting(entryTwo, valueTwo);
assertSame(valueTwo, valueRefTwo.get());
segment.setWeakValueReferenceForTesting(entryTwo, valueRefTwo);
assertSame(keyTwo, entryTwo.getKey());
assertEquals(hashTwo, entryTwo.getHash());
assertSame(entryOne, entryTwo.getNext());
assertSame(valueRefTwo, segment.getWeakValueReferenceForTesting(entryTwo));
}
}
示例5: testCopyEntry
import com.google.common.collect.MapMakerInternalMap.InternalEntry; //导入依赖的package包/类
public void testCopyEntry() {
for (MapMaker maker : allWeakValueStrengthMakers()) {
MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(maker);
Segment<Object, Object, ?, ?> segment = map.segments[0];
Object keyOne = new Object();
Object valueOne = new Object();
int hashOne = map.hash(keyOne);
InternalEntry<Object, Object, ?> entryOne = segment.newEntryForTesting(keyOne, hashOne, null);
segment.setValueForTesting(entryOne, valueOne);
Object keyTwo = new Object();
Object valueTwo = new Object();
int hashTwo = map.hash(keyTwo);
InternalEntry<Object, Object, ?> entryTwo = segment.newEntryForTesting(keyTwo, hashTwo, null);
segment.setValueForTesting(entryTwo, valueTwo);
InternalEntry<Object, Object, ?> copyOne = segment.copyForTesting(entryOne, null);
assertSame(keyOne, entryOne.getKey());
assertEquals(hashOne, entryOne.getHash());
assertNull(entryOne.getNext());
assertSame(valueOne, copyOne.getValue());
InternalEntry<Object, Object, ?> copyTwo = segment.copyForTesting(entryTwo, copyOne);
assertSame(keyTwo, copyTwo.getKey());
assertEquals(hashTwo, copyTwo.getHash());
assertSame(copyOne, copyTwo.getNext());
assertSame(valueTwo, copyTwo.getValue());
}
}
示例6: testSegmentReplace
import com.google.common.collect.MapMakerInternalMap.InternalEntry; //导入依赖的package包/类
public void testSegmentReplace() {
MapMakerInternalMap<Object, Object, ?, ?> map =
makeMap(createMapMaker().concurrencyLevel(1).weakValues());
Segment<Object, Object, ?, ?> segment = map.segments[0];
// TODO(fry): check recency ordering
Object key = new Object();
int hash = map.hash(key);
Object oldValue = new Object();
Object newValue = new Object();
AtomicReferenceArray<? extends InternalEntry<Object, Object, ?>> table = segment.table;
int index = hash & (table.length() - 1);
InternalEntry<Object, Object, ?> entry = segment.newEntryForTesting(key, hash, null);
WeakValueReference<Object, Object, ?> oldValueRef =
segment.newWeakValueReferenceForTesting(entry, oldValue);
segment.setWeakValueReferenceForTesting(entry, oldValueRef);
// no entry
assertNull(segment.replace(key, hash, newValue));
assertEquals(0, segment.count);
// same key
segment.setTableEntryForTesting(index, entry);
segment.count++;
assertEquals(1, segment.count);
assertSame(oldValue, segment.get(key, hash));
assertSame(oldValue, segment.replace(key, hash, newValue));
assertEquals(1, segment.count);
assertSame(newValue, segment.get(key, hash));
// cleared
segment.setWeakValueReferenceForTesting(entry, oldValueRef);
oldValueRef.clear();
assertNull(segment.replace(key, hash, newValue));
assertEquals(0, segment.count);
assertNull(segment.get(key, hash));
}
示例7: testSegmentPut
import com.google.common.collect.MapMakerInternalMap.InternalEntry; //导入依赖的package包/类
public void testSegmentPut() {
MapMakerInternalMap<Object, Object, ?, ?> map =
makeMap(createMapMaker().concurrencyLevel(1).weakValues());
Segment<Object, Object, ?, ?> segment = map.segments[0];
// TODO(fry): check recency ordering
Object key = new Object();
int hash = map.hash(key);
Object oldValue = new Object();
Object newValue = new Object();
// no entry
assertEquals(0, segment.count);
assertNull(segment.put(key, hash, oldValue, false));
assertEquals(1, segment.count);
// same key
assertSame(oldValue, segment.put(key, hash, newValue, false));
assertEquals(1, segment.count);
assertSame(newValue, segment.get(key, hash));
// cleared
InternalEntry<Object, Object, ?> entry = segment.getEntry(key, hash);
WeakValueReference<Object, Object, ?> oldValueRef =
segment.newWeakValueReferenceForTesting(entry, oldValue);
segment.setWeakValueReferenceForTesting(entry, oldValueRef);
assertSame(oldValue, segment.get(key, hash));
oldValueRef.clear();
assertNull(segment.put(key, hash, newValue, false));
assertEquals(1, segment.count);
assertSame(newValue, segment.get(key, hash));
}
示例8: testSegmentPutIfAbsent
import com.google.common.collect.MapMakerInternalMap.InternalEntry; //导入依赖的package包/类
public void testSegmentPutIfAbsent() {
MapMakerInternalMap<Object, Object, ?, ?> map =
makeMap(createMapMaker().concurrencyLevel(1).weakValues());
Segment<Object, Object, ?, ?> segment = map.segments[0];
// TODO(fry): check recency ordering
Object key = new Object();
int hash = map.hash(key);
Object oldValue = new Object();
Object newValue = new Object();
// no entry
assertEquals(0, segment.count);
assertNull(segment.put(key, hash, oldValue, true));
assertEquals(1, segment.count);
// same key
assertSame(oldValue, segment.put(key, hash, newValue, true));
assertEquals(1, segment.count);
assertSame(oldValue, segment.get(key, hash));
// cleared
InternalEntry<Object, Object, ?> entry = segment.getEntry(key, hash);
WeakValueReference<Object, Object, ?> oldValueRef =
segment.newWeakValueReferenceForTesting(entry, oldValue);
segment.setWeakValueReferenceForTesting(entry, oldValueRef);
assertSame(oldValue, segment.get(key, hash));
oldValueRef.clear();
assertNull(segment.put(key, hash, newValue, true));
assertEquals(1, segment.count);
assertSame(newValue, segment.get(key, hash));
}
示例9: testSegmentRemove
import com.google.common.collect.MapMakerInternalMap.InternalEntry; //导入依赖的package包/类
public void testSegmentRemove() {
MapMakerInternalMap<Object, Object, ?, ?> map =
makeMap(createMapMaker().concurrencyLevel(1).weakValues());
Segment<Object, Object, ?, ?> segment = map.segments[0];
Object key = new Object();
int hash = map.hash(key);
Object oldValue = new Object();
AtomicReferenceArray<? extends InternalEntry<Object, Object, ?>> table = segment.table;
int index = hash & (table.length() - 1);
InternalEntry<Object, Object, ?> entry = segment.newEntryForTesting(key, hash, null);
WeakValueReference<Object, Object, ?> oldValueRef =
segment.newWeakValueReferenceForTesting(entry, oldValue);
segment.setWeakValueReferenceForTesting(entry, oldValueRef);
// no entry
assertEquals(0, segment.count);
assertNull(segment.remove(key, hash));
assertEquals(0, segment.count);
// same key
segment.setTableEntryForTesting(index, entry);
segment.count++;
assertEquals(1, segment.count);
assertSame(oldValue, segment.get(key, hash));
assertSame(oldValue, segment.remove(key, hash));
assertEquals(0, segment.count);
assertNull(segment.get(key, hash));
// cleared
segment.setTableEntryForTesting(index, entry);
segment.count++;
assertEquals(1, segment.count);
assertSame(oldValue, segment.get(key, hash));
oldValueRef.clear();
assertNull(segment.remove(key, hash));
assertEquals(0, segment.count);
assertNull(segment.get(key, hash));
}
示例10: testExpand
import com.google.common.collect.MapMakerInternalMap.InternalEntry; //导入依赖的package包/类
public void testExpand() {
MapMakerInternalMap<Object, Object, ?, ?> map =
makeMap(createMapMaker().concurrencyLevel(1).initialCapacity(1));
Segment<Object, Object, ?, ?> segment = map.segments[0];
assertEquals(1, segment.table.length());
// manually add elements to avoid expansion
int originalCount = 1024;
InternalEntry<Object, Object, ?> entry = null;
for (int i = 0; i < originalCount; i++) {
Object key = new Object();
Object value = new Object();
int hash = map.hash(key);
// chain all entries together as we only have a single bucket
entry = segment.newEntryForTesting(key, hash, entry);
segment.setValueForTesting(entry, value);
}
segment.setTableEntryForTesting(0, entry);
segment.count = originalCount;
ImmutableMap<Object, Object> originalMap = ImmutableMap.copyOf(map);
assertEquals(originalCount, originalMap.size());
assertEquals(originalMap, map);
for (int i = 1; i <= originalCount * 2; i *= 2) {
if (i > 1) {
segment.expand();
}
assertEquals(i, segment.table.length());
assertEquals(originalCount, countLiveEntries(map));
assertEquals(originalCount, segment.count);
assertEquals(originalMap, map);
}
}
示例11: countLiveEntries
import com.google.common.collect.MapMakerInternalMap.InternalEntry; //导入依赖的package包/类
private static <K, V> int countLiveEntries(MapMakerInternalMap<K, V, ?, ?> map) {
int result = 0;
for (Segment<K, V, ?, ?> segment : map.segments) {
AtomicReferenceArray<? extends InternalEntry<K, V, ?>> table = segment.table;
for (int i = 0; i < table.length(); i++) {
for (InternalEntry<K, V, ?> e = table.get(i); e != null; e = e.getNext()) {
if (map.isLiveForTesting(e)) {
result++;
}
}
}
}
return result;
}
示例12: testClearValue
import com.google.common.collect.MapMakerInternalMap.InternalEntry; //导入依赖的package包/类
public void testClearValue() {
MapMakerInternalMap<Object, Object, ?, ?> map =
makeMap(createMapMaker().concurrencyLevel(1).initialCapacity(1).weakValues());
Segment<Object, Object, ?, ?> segment = map.segments[0];
AtomicReferenceArray<? extends InternalEntry<Object, Object, ?>> table = segment.table;
assertEquals(1, table.length());
Object key = new Object();
Object value = new Object();
int hash = map.hash(key);
InternalEntry<Object, Object, ?> entry = segment.newEntryForTesting(key, hash, null);
segment.setValueForTesting(entry, value);
WeakValueReference<Object, Object, ?> valueRef = segment.getWeakValueReferenceForTesting(entry);
// clear absent
assertFalse(segment.clearValueForTesting(key, hash, valueRef));
segment.setTableEntryForTesting(0, entry);
// don't increment count; this is used during computation
assertTrue(segment.clearValueForTesting(key, hash, valueRef));
// no notification sent with clearValue
assertEquals(0, segment.count);
assertNull(table.get(0));
// clear wrong value reference
segment.setTableEntryForTesting(0, entry);
WeakValueReference<Object, Object, ?> otherValueRef =
segment.newWeakValueReferenceForTesting(entry, value);
segment.setWeakValueReferenceForTesting(entry, otherValueRef);
assertFalse(segment.clearValueForTesting(key, hash, valueRef));
segment.setWeakValueReferenceForTesting(entry, valueRef);
assertTrue(segment.clearValueForTesting(key, hash, valueRef));
}
示例13: testDrainKeyReferenceQueueOnWrite
import com.google.common.collect.MapMakerInternalMap.InternalEntry; //导入依赖的package包/类
public void testDrainKeyReferenceQueueOnWrite() {
for (MapMaker maker : allWeakKeyStrengthMakers()) {
MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(maker.concurrencyLevel(1));
if (maker.getKeyStrength() == 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);
InternalEntry<Object, Object, ?> entry = segment.getEntry(keyOne, hashOne);
@SuppressWarnings("unchecked")
Reference<Object> reference = (Reference) entry;
reference.enqueue();
map.put(keyTwo, valueTwo);
assertFalse(map.containsKey(keyOne));
assertFalse(map.containsValue(valueOne));
assertNull(map.get(keyOne));
assertEquals(1, map.size());
assertNull(segment.getKeyReferenceQueueForTesting().poll());
}
}
}
示例14: testDrainKeyReferenceQueueOnRead
import com.google.common.collect.MapMakerInternalMap.InternalEntry; //导入依赖的package包/类
public void testDrainKeyReferenceQueueOnRead() {
for (MapMaker maker : allWeakKeyStrengthMakers()) {
MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(maker.concurrencyLevel(1));
if (maker.getKeyStrength() == 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);
InternalEntry<Object, Object, ?> entry = segment.getEntry(keyOne, hashOne);
@SuppressWarnings("unchecked")
Reference<Object> reference = (Reference) entry;
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.getKeyReferenceQueueForTesting().poll());
}
}
}