Home | History | Annotate | Download | only in collect
      1 /*
      2  * Copyright (C) 2012 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.common.collect;
     18 
     19 import java.util.Collection;
     20 import java.util.Collections;
     21 import java.util.HashSet;
     22 import java.util.LinkedHashSet;
     23 import java.util.Map;
     24 import java.util.Random;
     25 import java.util.Set;
     26 import java.util.SortedMap;
     27 import java.util.TreeSet;
     28 import java.util.concurrent.ConcurrentHashMap;
     29 import java.util.concurrent.ConcurrentSkipListMap;
     30 
     31 /**
     32  * Helper classes for various benchmarks.
     33  *
     34  * @author Christopher Swenson
     35  */
     36 final class BenchmarkHelpers {
     37   /**
     38    * So far, this is the best way to test various implementations of {@link Set} subclasses.
     39    */
     40   public enum SetImpl {
     41     Hash {
     42       @Override <E extends Comparable<E>> Set<E> create(Collection<E> contents) {
     43         return new HashSet<E>(contents);
     44       }
     45     },
     46     LinkedHash {
     47       @Override <E extends Comparable<E>> Set<E> create(Collection<E> contents) {
     48         return new LinkedHashSet<E>(contents);
     49       }
     50     },
     51     Tree {
     52       @Override <E extends Comparable<E>> Set<E> create(Collection<E> contents) {
     53         return new TreeSet<E>(contents);
     54       }
     55     },
     56     Unmodifiable {
     57       @Override <E extends Comparable<E>> Set<E> create(Collection<E> contents) {
     58         return Collections.unmodifiableSet(new HashSet<E>(contents));
     59       }
     60     },
     61     Synchronized {
     62       @Override <E extends Comparable<E>> Set<E> create(Collection<E> contents) {
     63         return Collections.synchronizedSet(new HashSet<E>(contents));
     64       }
     65     },
     66     Immutable {
     67       @Override <E extends Comparable<E>> Set<E> create(Collection<E> contents) {
     68         return ImmutableSet.copyOf(contents);
     69       }
     70     },
     71     ImmutableSorted {
     72       @Override <E extends Comparable<E>> Set<E> create(Collection<E> contents) {
     73         return ImmutableSortedSet.copyOf(contents);
     74       }
     75     },
     76     ;
     77 
     78     abstract <E extends Comparable<E>> Set<E> create(Collection<E> contents);
     79   }
     80 
     81   public enum ListMultimapImpl {
     82     ArrayList {
     83       @Override
     84       <K, V> ListMultimap<K, V> create(Multimap<K, V> contents) {
     85         return ArrayListMultimap.create(contents);
     86       }
     87     },
     88     LinkedList {
     89       @Override
     90       <K, V> ListMultimap<K, V> create(Multimap<K, V> contents) {
     91         return LinkedListMultimap.create(contents);
     92       }
     93     },
     94     ImmutableList {
     95       @Override
     96       <K, V> ListMultimap<K, V> create(Multimap<K, V> contents) {
     97         return ImmutableListMultimap.copyOf(contents);
     98       }
     99     };
    100 
    101     abstract <K, V> ListMultimap<K, V> create(Multimap<K, V> contents);
    102   }
    103 
    104   public enum SetMultimapImpl {
    105     Hash {
    106       @Override
    107       <K extends Comparable<K>, V extends Comparable<V>> SetMultimap<K, V> create(
    108           Multimap<K, V> contents) {
    109         return HashMultimap.create(contents);
    110       }
    111     },
    112     LinkedHash {
    113       @Override
    114       <K extends Comparable<K>, V extends Comparable<V>> SetMultimap<K, V> create(
    115           Multimap<K, V> contents) {
    116         return LinkedHashMultimap.create(contents);
    117       }
    118     },
    119     Tree {
    120       @Override
    121       <K extends Comparable<K>, V extends Comparable<V>> SetMultimap<K, V> create(
    122           Multimap<K, V> contents) {
    123         return TreeMultimap.create(contents);
    124       }
    125     },
    126     ImmutableSet {
    127       @Override
    128       <K extends Comparable<K>, V extends Comparable<V>> SetMultimap<K, V> create(
    129           Multimap<K, V> contents) {
    130         return ImmutableSetMultimap.copyOf(contents);
    131       }
    132     };
    133 
    134     abstract <K extends Comparable<K>, V extends Comparable<V>> SetMultimap<K, V> create(
    135         Multimap<K, V> contents);
    136   }
    137 
    138   public enum MapImpl {
    139     Hash {
    140       @Override
    141       <K, V> Map<K, V> create(Map<K, V> map) {
    142         return Maps.newHashMap(map);
    143       }
    144     },
    145     LinkedHash {
    146       @Override
    147       <K, V> Map<K, V> create(Map<K, V> map) {
    148         return Maps.newLinkedHashMap(map);
    149       }
    150     },
    151     ConcurrentHash {
    152       @Override
    153       <K, V> Map<K, V> create(Map<K, V> map) {
    154         return new ConcurrentHashMap<K, V>(map);
    155       }
    156     },
    157     Immutable {
    158       @Override
    159       <K, V> Map<K, V> create(Map<K, V> map) {
    160         return ImmutableMap.copyOf(map);
    161       }
    162     };
    163 
    164     abstract <K, V> Map<K, V> create(Map<K, V> map);
    165   }
    166 
    167   enum SortedMapImpl {
    168     Tree {
    169       @Override
    170       <K extends Comparable<K>, V> SortedMap<K, V> create(Map<K, V> map) {
    171         SortedMap<K, V> result = Maps.newTreeMap();
    172         result.putAll(map);
    173         return result;
    174       }
    175     },
    176     ConcurrentSkipList {
    177       @Override
    178       <K extends Comparable<K>, V> SortedMap<K, V> create(Map<K, V> map) {
    179         return new ConcurrentSkipListMap<K, V>(map);
    180       }
    181     },
    182     ImmutableSorted {
    183       @Override
    184       <K extends Comparable<K>, V> SortedMap<K, V> create(Map<K, V> map) {
    185         return ImmutableSortedMap.copyOf(map);
    186       }
    187     };
    188 
    189     abstract <K extends Comparable<K>, V> SortedMap<K, V> create(Map<K, V> map);
    190   }
    191 
    192   enum BiMapImpl {
    193     Hash{
    194       @Override
    195       <K, V> BiMap<K, V> create(BiMap<K, V> map) {
    196         return HashBiMap.create(map);
    197       }
    198     },
    199     Immutable {
    200       @Override
    201       <K, V> BiMap<K, V> create(BiMap<K, V> map) {
    202         return ImmutableBiMap.copyOf(map);
    203       }
    204     };
    205 
    206     abstract <K, V> BiMap<K, V> create(BiMap<K, V> map);
    207   }
    208 
    209   enum MultisetImpl {
    210     Hash {
    211       @Override
    212       <E> Multiset<E> create(Multiset<E> contents) {
    213         return HashMultiset.create(contents);
    214       }
    215     },
    216     LinkedHash {
    217       @Override
    218       <E> Multiset<E> create(Multiset<E> contents) {
    219         return LinkedHashMultiset.create(contents);
    220       }
    221     },
    222     ConcurrentHash {
    223       @Override
    224       <E> Multiset<E> create(Multiset<E> contents) {
    225         return ConcurrentHashMultiset.create(contents);
    226       }
    227     },
    228     Immutable {
    229       @Override
    230       <E> Multiset<E> create(Multiset<E> contents) {
    231         return ImmutableMultiset.copyOf(contents);
    232       }
    233     };
    234 
    235     abstract <E> Multiset<E> create(Multiset<E> contents);
    236   }
    237 
    238   enum SortedMultisetImpl {
    239     Tree {
    240       @Override
    241       <E extends Comparable<E>> SortedMultiset<E> create(Multiset<E> contents) {
    242         return TreeMultiset.create(contents);
    243       }
    244     },
    245     ImmutableSorted {
    246       @Override
    247       <E extends Comparable<E>> SortedMultiset<E> create(Multiset<E> contents) {
    248         return ImmutableSortedMultiset.copyOf(contents);
    249       }
    250     };
    251 
    252     abstract <E extends Comparable<E>> SortedMultiset<E> create(Multiset<E> contents);
    253   }
    254 
    255   enum TableImpl {
    256     HashBased {
    257       @Override
    258       <R extends Comparable<R>, C extends Comparable<C>, V> Table<R, C, V> create(
    259           Table<R, C, V> contents) {
    260         return HashBasedTable.create(contents);
    261       }
    262     },
    263     TreeBased {
    264       @Override
    265       <R extends Comparable<R>, C extends Comparable<C>, V> Table<R, C, V> create(
    266           Table<R, C, V> contents) {
    267         Table<R, C, V> table = TreeBasedTable.create();
    268         table.putAll(contents);
    269         return table;
    270       }
    271     },
    272     Array {
    273       @Override
    274       <R extends Comparable<R>, C extends Comparable<C>, V> Table<R, C, V> create(
    275           Table<R, C, V> contents) {
    276         if (contents.isEmpty()) {
    277           return ImmutableTable.of();
    278         } else {
    279           return ArrayTable.create(contents);
    280         }
    281       }
    282     },
    283     Immutable {
    284       @Override
    285       <R extends Comparable<R>, C extends Comparable<C>, V> Table<R, C, V> create(
    286           Table<R, C, V> contents) {
    287         return ImmutableTable.copyOf(contents);
    288       }
    289     };
    290 
    291     abstract <R extends Comparable<R>, C extends Comparable<C>, V>
    292         Table<R, C, V> create(Table<R, C, V> contents);
    293   }
    294 
    295   public enum Value {
    296     INSTANCE;
    297   }
    298 
    299   public enum ListSizeDistribution {
    300     UNIFORM_0_TO_2(0, 2), UNIFORM_0_TO_9(0, 9), ALWAYS_0(0, 0), ALWAYS_10(10, 10);
    301 
    302     final int min;
    303     final int max;
    304 
    305     private ListSizeDistribution(int min, int max) {
    306       this.min = min;
    307       this.max = max;
    308     }
    309 
    310     public int chooseSize(Random random) {
    311       return random.nextInt(max - min + 1) + min;
    312     }
    313   }
    314 }
    315