This example Java source code file (StripedTest.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.
/*
* 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.util.concurrent;
import static com.google.common.collect.Iterables.concat;
import com.google.common.base.Functions;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;
import com.google.common.testing.GcFinalization;
import com.google.common.testing.NullPointerTester;
import junit.framework.TestCase;
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* Tests for Striped.
*
* @author Dimitris Andreou
*/
public class StripedTest extends TestCase {
private static List<Striped>> strongImplementations() {
return ImmutableList.of(
Striped.readWriteLock(100),
Striped.readWriteLock(256),
Striped.lock(100),
Striped.lock(256),
Striped.semaphore(100, 1),
Striped.semaphore(256, 1));
}
private static final Supplier<ReadWriteLock> READ_WRITE_LOCK_SUPPLIER =
new Supplier<ReadWriteLock>() {
@Override public ReadWriteLock get() {
return new ReentrantReadWriteLock();
}
};
private static final Supplier<Lock> LOCK_SUPPLER = new Supplier() {
@Override public Lock get() {
return new ReentrantLock();
}
};
private static final Supplier<Semaphore> SEMAPHORE_SUPPLER = new Supplier() {
@Override public Semaphore get() {
return new Semaphore(1, false);
}
};
private static List<Striped>> weakImplementations() {
return ImmutableList.<Striped>>builder()
.add(new Striped.SmallLazyStriped<ReadWriteLock>(50, READ_WRITE_LOCK_SUPPLIER))
.add(new Striped.SmallLazyStriped<ReadWriteLock>(64, READ_WRITE_LOCK_SUPPLIER))
.add(new Striped.LargeLazyStriped<ReadWriteLock>(50, READ_WRITE_LOCK_SUPPLIER))
.add(new Striped.LargeLazyStriped<ReadWriteLock>(64, READ_WRITE_LOCK_SUPPLIER))
.add(new Striped.SmallLazyStriped<Lock>(50, LOCK_SUPPLER))
.add(new Striped.SmallLazyStriped<Lock>(64, LOCK_SUPPLER))
.add(new Striped.LargeLazyStriped<Lock>(50, LOCK_SUPPLER))
.add(new Striped.LargeLazyStriped<Lock>(64, LOCK_SUPPLER))
.add(new Striped.SmallLazyStriped<Semaphore>(50, SEMAPHORE_SUPPLER))
.add(new Striped.SmallLazyStriped<Semaphore>(64, SEMAPHORE_SUPPLER))
.add(new Striped.LargeLazyStriped<Semaphore>(50, SEMAPHORE_SUPPLER))
.add(new Striped.LargeLazyStriped<Semaphore>(64, SEMAPHORE_SUPPLER))
.build();
}
private static Iterable<Striped>> allImplementations() {
return concat(strongImplementations(), weakImplementations());
}
public void testNull() throws Exception {
for (Striped<?> striped : allImplementations()) {
new NullPointerTester().testAllPublicInstanceMethods(striped);
}
}
public void testSizes() {
// not bothering testing all variations, since we know they share implementations
assertTrue(Striped.lock(100).size() >= 100);
assertTrue(Striped.lock(256).size() == 256);
assertTrue(Striped.lazyWeakLock(100).size() >= 100);
assertTrue(Striped.lazyWeakLock(256).size() == 256);
}
public void testWeakImplementations() {
for (Striped<?> striped : weakImplementations()) {
WeakReference<Object> weakRef = new WeakReference
Other Java examples (source code examples)
Here is a short list of links related to this Java StripedTest.java source code file: