|
Java example source code file (BenchmarkHelpers.java)
This example Java source code file (BenchmarkHelpers.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.
Learn more about this Java project at its project page.
The BenchmarkHelpers.java Java example source code
/*
* Copyright (C) 2012 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 java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
/**
* Helper classes for various benchmarks.
*
* @author Christopher Swenson
*/
final class BenchmarkHelpers {
/**
* So far, this is the best way to test various implementations of {@link Set} subclasses.
*/
public enum SetImpl {
Hash {
@Override <E extends Comparable Set create(Collection contents) {
return new HashSet<E>(contents);
}
},
LinkedHash {
@Override <E extends Comparable Set create(Collection contents) {
return new LinkedHashSet<E>(contents);
}
},
Tree {
@Override <E extends Comparable Set create(Collection contents) {
return new TreeSet<E>(contents);
}
},
Unmodifiable {
@Override <E extends Comparable Set create(Collection contents) {
return Collections.unmodifiableSet(new HashSet<E>(contents));
}
},
Synchronized {
@Override <E extends Comparable Set create(Collection contents) {
return Collections.synchronizedSet(new HashSet<E>(contents));
}
},
Immutable {
@Override <E extends Comparable Set create(Collection contents) {
return ImmutableSet.copyOf(contents);
}
},
ImmutableSorted {
@Override <E extends Comparable Set create(Collection contents) {
return ImmutableSortedSet.copyOf(contents);
}
},
;
abstract <E extends Comparable Set create(Collection contents);
}
public enum ListMultimapImpl {
ArrayList {
@Override
<K, V> ListMultimap create(Multimap contents) {
return ArrayListMultimap.create(contents);
}
},
LinkedList {
@Override
<K, V> ListMultimap create(Multimap contents) {
return LinkedListMultimap.create(contents);
}
},
ImmutableList {
@Override
<K, V> ListMultimap create(Multimap contents) {
return ImmutableListMultimap.copyOf(contents);
}
};
abstract <K, V> ListMultimap create(Multimap contents);
}
public enum SetMultimapImpl {
Hash {
@Override
<K extends Comparable> SetMultimap create(
Multimap<K, V> contents) {
return HashMultimap.create(contents);
}
},
LinkedHash {
@Override
<K extends Comparable> SetMultimap create(
Multimap<K, V> contents) {
return LinkedHashMultimap.create(contents);
}
},
Tree {
@Override
<K extends Comparable> SetMultimap create(
Multimap<K, V> contents) {
return TreeMultimap.create(contents);
}
},
ImmutableSet {
@Override
<K extends Comparable> SetMultimap create(
Multimap<K, V> contents) {
return ImmutableSetMultimap.copyOf(contents);
}
};
abstract <K extends Comparable> SetMultimap create(
Multimap<K, V> contents);
}
public enum MapImpl {
Hash {
@Override
<K, V> Map create(Map map) {
return Maps.newHashMap(map);
}
},
LinkedHash {
@Override
<K, V> Map create(Map map) {
return Maps.newLinkedHashMap(map);
}
},
ConcurrentHash {
@Override
<K, V> Map create(Map map) {
return new ConcurrentHashMap<K, V>(map);
}
},
Immutable {
@Override
<K, V> Map create(Map map) {
return ImmutableMap.copyOf(map);
}
};
abstract <K, V> Map create(Map map);
}
enum SortedMapImpl {
Tree {
@Override
<K extends Comparable SortedMap create(Map map) {
SortedMap<K, V> result = Maps.newTreeMap();
result.putAll(map);
return result;
}
},
ConcurrentSkipList {
@Override
<K extends Comparable SortedMap create(Map map) {
return new ConcurrentSkipListMap<K, V>(map);
}
},
ImmutableSorted {
@Override
<K extends Comparable SortedMap create(Map map) {
return ImmutableSortedMap.copyOf(map);
}
};
abstract <K extends Comparable SortedMap create(Map map);
}
enum BiMapImpl {
Hash{
@Override
<K, V> BiMap create(BiMap map) {
return HashBiMap.create(map);
}
},
Immutable {
@Override
<K, V> BiMap create(BiMap map) {
return ImmutableBiMap.copyOf(map);
}
};
abstract <K, V> BiMap create(BiMap map);
}
enum MultisetImpl {
Hash {
@Override
<E> Multiset create(Multiset contents) {
return HashMultiset.create(contents);
}
},
LinkedHash {
@Override
<E> Multiset create(Multiset contents) {
return LinkedHashMultiset.create(contents);
}
},
ConcurrentHash {
@Override
<E> Multiset create(Multiset contents) {
return ConcurrentHashMultiset.create(contents);
}
},
Immutable {
@Override
<E> Multiset create(Multiset contents) {
return ImmutableMultiset.copyOf(contents);
}
};
abstract <E> Multiset create(Multiset contents);
}
enum SortedMultisetImpl {
Tree {
@Override
<E extends Comparable SortedMultiset create(Multiset contents) {
return TreeMultiset.create(contents);
}
},
ImmutableSorted {
@Override
<E extends Comparable SortedMultiset create(Multiset contents) {
return ImmutableSortedMultiset.copyOf(contents);
}
};
abstract <E extends Comparable SortedMultiset create(Multiset contents);
}
enum TableImpl {
HashBased {
@Override
<R extends Comparable, V> Table create(
Table<R, C, V> contents) {
return HashBasedTable.create(contents);
}
},
TreeBased {
@Override
<R extends Comparable, V> Table create(
Table<R, C, V> contents) {
Table<R, C, V> table = TreeBasedTable.create();
table.putAll(contents);
return table;
}
},
Array {
@Override
<R extends Comparable, V> Table create(
Table<R, C, V> contents) {
if (contents.isEmpty()) {
return ImmutableTable.of();
} else {
return ArrayTable.create(contents);
}
}
},
Immutable {
@Override
<R extends Comparable, V> Table create(
Table<R, C, V> contents) {
return ImmutableTable.copyOf(contents);
}
};
abstract <R extends Comparable, V>
Table<R, C, V> create(Table contents);
}
public enum Value {
INSTANCE;
}
public enum ListSizeDistribution {
UNIFORM_0_TO_2(0, 2), UNIFORM_0_TO_9(0, 9), ALWAYS_0(0, 0), ALWAYS_10(10, 10);
final int min;
final int max;
private ListSizeDistribution(int min, int max) {
this.min = min;
this.max = max;
}
public int chooseSize(Random random) {
return random.nextInt(max - min + 1) + min;
}
}
}
Other Java examples (source code examples)
Here is a short list of links related to this Java BenchmarkHelpers.java source code file:
|