本文整理汇总了Java中com.google.common.collect.MapMakerInternalMap.ReferenceEntry.setValueReference方法的典型用法代码示例。如果您正苦于以下问题:Java ReferenceEntry.setValueReference方法的具体用法?Java ReferenceEntry.setValueReference怎么用?Java ReferenceEntry.setValueReference使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.common.collect.MapMakerInternalMap.ReferenceEntry
的用法示例。
在下文中一共展示了ReferenceEntry.setValueReference方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testNewEntry
import com.google.common.collect.MapMakerInternalMap.ReferenceEntry; //导入方法依赖的package包/类
public void testNewEntry() {
for (MapMaker maker : allEntryTypeMakers()) {
MapMakerInternalMap<Object, Object> map = makeMap(maker);
Object keyOne = new Object();
Object valueOne = new Object();
int hashOne = map.hash(keyOne);
ReferenceEntry<Object, Object> entryOne = map.newEntry(keyOne, hashOne, null);
ValueReference<Object, Object> valueRefOne = map.newValueReference(entryOne, valueOne);
assertSame(valueOne, valueRefOne.get());
entryOne.setValueReference(valueRefOne);
assertSame(keyOne, entryOne.getKey());
assertEquals(hashOne, entryOne.getHash());
assertNull(entryOne.getNext());
assertSame(valueRefOne, entryOne.getValueReference());
Object keyTwo = new Object();
Object valueTwo = new Object();
int hashTwo = map.hash(keyTwo);
ReferenceEntry<Object, Object> entryTwo = map.newEntry(keyTwo, hashTwo, entryOne);
ValueReference<Object, Object> valueRefTwo = map.newValueReference(entryTwo, valueTwo);
assertSame(valueTwo, valueRefTwo.get());
entryTwo.setValueReference(valueRefTwo);
assertSame(keyTwo, entryTwo.getKey());
assertEquals(hashTwo, entryTwo.getHash());
assertSame(entryOne, entryTwo.getNext());
assertSame(valueRefTwo, entryTwo.getValueReference());
}
}
示例2: testCopyEntry
import com.google.common.collect.MapMakerInternalMap.ReferenceEntry; //导入方法依赖的package包/类
public void testCopyEntry() {
for (MapMaker maker : allEntryTypeMakers()) {
MapMakerInternalMap<Object, Object> map = makeMap(maker);
Object keyOne = new Object();
Object valueOne = new Object();
int hashOne = map.hash(keyOne);
ReferenceEntry<Object, Object> entryOne = map.newEntry(keyOne, hashOne, null);
entryOne.setValueReference(map.newValueReference(entryOne, valueOne));
Object keyTwo = new Object();
Object valueTwo = new Object();
int hashTwo = map.hash(keyTwo);
ReferenceEntry<Object, Object> entryTwo = map.newEntry(keyTwo, hashTwo, entryOne);
entryTwo.setValueReference(map.newValueReference(entryTwo, valueTwo));
if (map.evictsBySize()) {
MapMakerInternalMap.connectEvictables(entryOne, entryTwo);
}
if (map.expires()) {
MapMakerInternalMap.connectExpirables(entryOne, entryTwo);
}
assertConnected(map, entryOne, entryTwo);
ReferenceEntry<Object, Object> copyOne = map.copyEntry(entryOne, null);
assertSame(keyOne, entryOne.getKey());
assertEquals(hashOne, entryOne.getHash());
assertNull(entryOne.getNext());
assertSame(valueOne, copyOne.getValueReference().get());
assertConnected(map, copyOne, entryTwo);
ReferenceEntry<Object, Object> copyTwo = map.copyEntry(entryTwo, copyOne);
assertSame(keyTwo, copyTwo.getKey());
assertEquals(hashTwo, copyTwo.getHash());
assertSame(copyOne, copyTwo.getNext());
assertSame(valueTwo, copyTwo.getValueReference().get());
assertConnected(map, copyOne, copyTwo);
}
}
示例3: testSegmentPut
import com.google.common.collect.MapMakerInternalMap.ReferenceEntry; //导入方法依赖的package包/类
public void testSegmentPut() {
MapMakerInternalMap<Object, Object> map =
makeMap(createMapMaker().concurrencyLevel(1).expireAfterAccess(99999, SECONDS));
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
ReferenceEntry<Object, Object> entry = segment.getEntry(key, hash);
DummyValueReference<Object, Object> oldValueRef = DummyValueReference.create(oldValue, entry);
entry.setValueReference(oldValueRef);
assertSame(oldValue, segment.get(key, hash));
oldValueRef.clear(null);
assertNull(segment.put(key, hash, newValue, false));
assertEquals(1, segment.count);
assertSame(newValue, segment.get(key, hash));
}
示例4: testSegmentPutIfAbsent
import com.google.common.collect.MapMakerInternalMap.ReferenceEntry; //导入方法依赖的package包/类
public void testSegmentPutIfAbsent() {
MapMakerInternalMap<Object, Object> map =
makeMap(createMapMaker().concurrencyLevel(1).expireAfterAccess(99999, SECONDS));
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
ReferenceEntry<Object, Object> entry = segment.getEntry(key, hash);
DummyValueReference<Object, Object> oldValueRef = DummyValueReference.create(oldValue, entry);
entry.setValueReference(oldValueRef);
assertSame(oldValue, segment.get(key, hash));
oldValueRef.clear(null);
assertNull(segment.put(key, hash, newValue, true));
assertEquals(1, segment.count);
assertSame(newValue, segment.get(key, hash));
}
示例5: testExpand
import com.google.common.collect.MapMakerInternalMap.ReferenceEntry; //导入方法依赖的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;
ReferenceEntry<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 = map.newEntry(key, hash, entry);
ValueReference<Object, Object> valueRef = map.newValueReference(entry, value);
entry.setValueReference(valueRef);
}
segment.table.set(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);
}
}
示例6: testEvictEntries
import com.google.common.collect.MapMakerInternalMap.ReferenceEntry; //导入方法依赖的package包/类
public void testEvictEntries() {
int maxSize = 10;
MapMakerInternalMap<Object, Object> map =
makeMap(createMapMaker().concurrencyLevel(1).maximumSize(maxSize));
Segment<Object, Object> segment = map.segments[0];
// manually add elements to avoid eviction
int originalCount = 1024;
ReferenceEntry<Object, Object> entry = null;
LinkedHashMap<Object, Object> originalMap = Maps.newLinkedHashMap();
for (int i = 0; i < originalCount; i++) {
Object key = new Object();
Object value = new Object();
AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
int hash = map.hash(key);
int index = hash & (table.length() - 1);
ReferenceEntry<Object, Object> first = table.get(index);
entry = map.newEntry(key, hash, first);
ValueReference<Object, Object> valueRef = map.newValueReference(entry, value);
entry.setValueReference(valueRef);
segment.recordWrite(entry);
table.set(index, entry);
originalMap.put(key, value);
}
segment.count = originalCount;
assertEquals(originalCount, originalMap.size());
assertEquals(originalMap, map);
for (int i = maxSize - 1; i < originalCount; i++) {
assertTrue(segment.evictEntries());
Iterator<Object> it = originalMap.keySet().iterator();
it.next();
it.remove();
assertEquals(originalMap, map);
}
assertFalse(segment.evictEntries());
}
示例7: testSegmentGetAndContains
import com.google.common.collect.MapMakerInternalMap.ReferenceEntry; //导入方法依赖的package包/类
public void testSegmentGetAndContains() {
MapMakerInternalMap<Object, Object> map =
makeMap(createMapMaker().concurrencyLevel(1).expireAfterAccess(99999, SECONDS));
Segment<Object, Object> segment = map.segments[0];
// TODO(fry): check recency ordering
Object key = new Object();
int hash = map.hash(key);
Object value = new Object();
AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
int index = hash & (table.length() - 1);
ReferenceEntry<Object, Object> entry = map.newEntry(key, hash, null);
ValueReference<Object, Object> valueRef = map.newValueReference(entry, value);
entry.setValueReference(valueRef);
assertNull(segment.get(key, hash));
// count == 0
table.set(index, entry);
assertNull(segment.get(key, hash));
assertFalse(segment.containsKey(key, hash));
assertFalse(segment.containsValue(value));
// count == 1
segment.count++;
assertSame(value, segment.get(key, hash));
assertTrue(segment.containsKey(key, hash));
assertTrue(segment.containsValue(value));
// don't see absent values now that count > 0
assertNull(segment.get(new Object(), hash));
// null key
DummyEntry<Object, Object> nullEntry = DummyEntry.create(null, hash, entry);
Object nullValue = new Object();
ValueReference<Object, Object> nullValueRef = map.newValueReference(nullEntry, nullValue);
nullEntry.setValueReference(nullValueRef);
table.set(index, nullEntry);
// skip the null key
assertSame(value, segment.get(key, hash));
assertTrue(segment.containsKey(key, hash));
assertTrue(segment.containsValue(value));
assertFalse(segment.containsValue(nullValue));
// hash collision
DummyEntry<Object, Object> dummy = DummyEntry.create(new Object(), hash, entry);
Object dummyValue = new Object();
ValueReference<Object, Object> dummyValueRef = map.newValueReference(dummy, dummyValue);
dummy.setValueReference(dummyValueRef);
table.set(index, dummy);
assertSame(value, segment.get(key, hash));
assertTrue(segment.containsKey(key, hash));
assertTrue(segment.containsValue(value));
assertTrue(segment.containsValue(dummyValue));
// key collision
dummy = DummyEntry.create(key, hash, entry);
dummyValue = new Object();
dummyValueRef = map.newValueReference(dummy, dummyValue);
dummy.setValueReference(dummyValueRef);
table.set(index, dummy);
// returns the most recent entry
assertSame(dummyValue, segment.get(key, hash));
assertTrue(segment.containsKey(key, hash));
assertTrue(segment.containsValue(value));
assertTrue(segment.containsValue(dummyValue));
// expired
dummy.setExpirationTime(0);
assertNull(segment.get(key, hash));
assertFalse(segment.containsKey(key, hash));
assertTrue(segment.containsValue(value));
assertFalse(segment.containsValue(dummyValue));
}
示例8: testExpand_cleanup
import com.google.common.collect.MapMakerInternalMap.ReferenceEntry; //导入方法依赖的package包/类
public void testExpand_cleanup() {
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
// 1/3 null keys, 1/3 null values
int originalCount = 1024;
ReferenceEntry<Object, Object> entry = null;
for (int i = 0; i < originalCount; i++) {
Object key = new Object();
Object value = (i % 3 == 0) ? null : new Object();
int hash = map.hash(key);
if (i % 3 == 1) {
key = null;
}
// chain all entries together as we only have a single bucket
entry = DummyEntry.create(key, hash, entry);
ValueReference<Object, Object> valueRef = DummyValueReference.create(value, entry);
entry.setValueReference(valueRef);
}
segment.table.set(0, entry);
segment.count = originalCount;
int liveCount = originalCount / 3;
assertEquals(1, segment.table.length());
assertEquals(liveCount, countLiveEntries(map));
ImmutableMap<Object, Object> originalMap = ImmutableMap.copyOf(map);
assertEquals(liveCount, originalMap.size());
// can't compare map contents until cleanup occurs
for (int i = 1; i <= originalCount * 2; i *= 2) {
if (i > 1) {
segment.expand();
}
assertEquals(i, segment.table.length());
assertEquals(liveCount, countLiveEntries(map));
// expansion cleanup is sloppy, with a goal of avoiding unnecessary copies
assertTrue(segment.count >= liveCount);
assertTrue(segment.count <= originalCount);
assertEquals(originalMap, ImmutableMap.copyOf(map));
}
}