Java example source code file (ComputingConcurrentHashMapTest.java)
This example Java source code file (ComputingConcurrentHashMapTest.java) is included in the alvinalexander.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
The ComputingConcurrentHashMapTest.java Java example source code
/*
* Copyright (C) 2011 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.collect;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.collect.MapMaker.ComputingMapAdapter;
import com.google.common.collect.MapMakerInternalMap.ReferenceEntry;
import com.google.common.collect.MapMakerInternalMap.Segment;
import com.google.common.collect.MapMakerInternalMapTest.DummyEntry;
import com.google.common.collect.MapMakerInternalMapTest.DummyValueReference;
import com.google.common.testing.NullPointerTester;
import junit.framework.TestCase;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReferenceArray;
/**
* @author Charles Fry
*/
public class ComputingConcurrentHashMapTest extends TestCase {
private static <K, V> ComputingConcurrentHashMap makeComputingMap(
MapMaker maker, Function<? super K, ? extends V> computingFunction) {
return new ComputingConcurrentHashMap<K, V>(
maker, computingFunction);
}
private static <K, V> ComputingMapAdapter makeAdaptedMap(
MapMaker maker, Function<? super K, ? extends V> computingFunction) {
return new ComputingMapAdapter<K, V>(
maker, computingFunction);
}
private static MapMaker createMapMaker() {
MapMaker maker = new MapMaker();
maker.useCustomMap = true;
return maker;
}
// constructor tests
public void testComputingFunction() {
Function<Object, Object> computingFunction = Functions.identity();
ComputingConcurrentHashMap<Object, Object> map =
makeComputingMap(createMapMaker(), computingFunction);
assertSame(computingFunction, map.computingFunction);
}
// computation tests
public void testCompute() throws ExecutionException {
CountingFunction computingFunction = new CountingFunction();
ComputingConcurrentHashMap<Object, Object> map =
makeComputingMap(createMapMaker(), computingFunction);
assertEquals(0, computingFunction.getCount());
Object key = new Object();
Object value = map.getOrCompute(key);
assertEquals(1, computingFunction.getCount());
assertEquals(value, map.getOrCompute(key));
assertEquals(1, computingFunction.getCount());
}
public void testComputeNull() {
Function<Object, Object> computingFunction = new ConstantLoader
Other Java examples (source code examples)
Here is a short list of links related to this Java ComputingConcurrentHashMapTest.java source code file: