package org.apache.cassandra.stress.operations;
/*
 * 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 * 
 */


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.math3.distribution.EnumeratedDistribution;
import org.apache.commons.math3.util.Pair;

import org.apache.cassandra.stress.Operation;
import org.apache.cassandra.stress.generate.DistributionFactory;
import org.apache.cassandra.stress.generate.PartitionGenerator;
import org.apache.cassandra.stress.util.Timer;

public abstract class SampledOpDistributionFactory<T> implements OpDistributionFactory
{

    final List<Pair<T, Double>> ratios;
    final DistributionFactory clustering;
    protected SampledOpDistributionFactory(List<Pair<T, Double>> ratios, DistributionFactory clustering)
    {
        this.ratios = ratios;
        this.clustering = clustering;
    }

    protected abstract Operation get(Timer timer, PartitionGenerator generator, T key);
    protected abstract PartitionGenerator newGenerator();

    public OpDistribution get(Timer timer)
    {
        PartitionGenerator generator = newGenerator();
        List<Pair<Operation, Double>> operations = new ArrayList<>();
        for (Pair<T, Double> ratio : ratios)
            operations.add(new Pair<>(get(timer, generator, ratio.getFirst()), ratio.getSecond()));
        return new SampledOpDistribution(new EnumeratedDistribution<>(operations), clustering.get());
    }

    public String desc()
    {
        List<T> keys = new ArrayList<>();
        for (Pair<T, Double> p : ratios)
            keys.add(p.getFirst());
        return keys.toString();
    }

    public Iterable<OpDistributionFactory> each()
    {
        List<OpDistributionFactory> out = new ArrayList<>();
        for (final Pair<T, Double> ratio : ratios)
        {
            out.add(new OpDistributionFactory()
            {
                public OpDistribution get(Timer timer)
                {
                    return new FixedOpDistribution(SampledOpDistributionFactory.this.get(timer, newGenerator(), ratio.getFirst()));
                }

                public String desc()
                {
                    return ratio.getFirst().toString();
                }

                public Iterable<OpDistributionFactory> each()
                {
                    return Collections.<OpDistributionFactory>singleton(this);
                }
            });
        }
        return out;
    }

}

@@ -941,16 +941,38 @@
 il.List;
+%0Aimport java.util.Map;
 %0A%0Aimport
@@ -1401,25 +1401,19 @@
   final 
-List%3CPair
+Map
 %3CT, Doub
@@ -1411,25 +1411,24 @@
 p%3CT, Double%3E
-%3E
  ratios;%0A   
@@ -1509,25 +1509,19 @@
 Factory(
-List%3CPair
+Map
 %3CT, Doub
@@ -1523,17 +1523,16 @@
  Double%3E
-%3E
  ratios,
@@ -1971,36 +1971,41 @@
 );%0A        for (
-Pair
+Map.Entry
 %3CT, Double%3E rati
@@ -2006,32 +2006,43 @@
 %3E ratio : ratios
+.entrySet()
 )%0A            op
@@ -2096,21 +2096,19 @@
 atio.get
-First
+Key
 ()), rat
@@ -2117,14 +2117,13 @@
 .get
-Second
+Value
 ()))
@@ -2319,20 +2319,25 @@
    for (
-Pair
+Map.Entry
 %3CT, Doub
@@ -2340,17 +2340,21 @@
 Double%3E 
-p
+ratio
  : ratio
@@ -2346,32 +2346,43 @@
 %3E ratio : ratios
+.entrySet()
 )%0A            ke
@@ -2392,18 +2392,20 @@
 add(
-p.getFirst
+ratio.getKey
 ());
@@ -2580,20 +2580,25 @@
  (final 
-Pair
+Map.Entry
 %3CT, Doub
@@ -2615,16 +2615,27 @@
 : ratios
+.entrySet()
 )%0A      
@@ -2894,21 +2894,19 @@
 atio.get
-First
+Key
 ()));%0A  
@@ -3017,13 +3017,11 @@
 .get
-First
+Key
 ().t

fix merge of 7519