/*
 * Copyright 2017, Google Inc.
 * 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.instrumentation.stats;

import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.truth.Truth;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/** Stats test utilities. */
final class StatsTestUtil {

  private StatsTestUtil() {}

  /**
   * Creates a {@code StatsContextImpl} from a factory and a list of alternating tag keys and
   * values.
   *
   * @param factory the factory used to produce the {@code StatsContextImpl}.
   * @param tagKeysAndValues a list of alternating {@link TagKey}s and {@link TagValue}s. It must
   *     have an even length.
   * @return a {@code StatsContextImpl} with the given keys and values.
   */
  static StatsContextImpl createContext(
      StatsContextFactoryImpl factory, Object... tagKeysAndValues) {
    StatsContextImpl.Builder builder = factory.getDefault().builder();
    for (Iterator<Object> i = Arrays.asList(tagKeysAndValues).iterator(); i.hasNext(); ) {
      TagKey key = (TagKey) i.next();
      TagValue value = (TagValue) i.next();
      builder.set(key, value);
    }
    return builder.build();
  }

  /**
   * Creates a {@code DistributionAggregation} by adding the given values to a new {@link
   * MutableDistribution}.
   *
   * @param tags the {@code DistributionAggregation}'s tags.
   * @param bucketBoundaries the bucket boundaries.
   * @param values the values to add to the distribution.
   * @return the new {@code DistributionAggregation}
   */
  static DistributionAggregation createDistributionAggregation(
      List<Tag> tags, BucketBoundaries bucketBoundaries, List<Double> values) {
    MutableDistribution mdist = MutableDistribution.create(bucketBoundaries);
    for (double value : values) {
      mdist.add(value);
    }
    MutableDistribution.Range range = mdist.getRange();
    return DistributionAggregation.create(
        mdist.getCount(),
        mdist.getMean(),
        mdist.getSum(),
        DistributionAggregation.Range.create(range.getMin(), range.getMax()),
        tags,
        mdist.getBucketCounts());
  }

  /**
   * Asserts that the two sets of {@code DistributionAggregation}s are equivalent, with a given
   * tolerance. The tolerance is used when comparing the mean and sum of values. The order of the
   * {@code DistributionAggregation}s has no effect. The expected parameter is last, because it is
   * likely to be a larger expression.
   *
   * @param tolerance the tolerance used for {@code double} comparison.
   * @param actual the actual test result.
   * @param expected the expected value.
   * @throws AssertionError if the {@code DistributionAggregation}s don't match.
   */
  static void assertDistributionAggregationsEquivalent(
      double tolerance,
      Collection<DistributionAggregation> actual,
      Collection<DistributionAggregation> expected) {
    Function<DistributionAggregation, List<Tag>> getTagsFunction =
        new Function<DistributionAggregation, List<Tag>>() {
          @Override
          public List<Tag> apply(DistributionAggregation agg) {
            return agg.getTags();
          }
        };
    Iterable<List<Tag>> expectedTags = Iterables.transform(expected, getTagsFunction);
    Iterable<List<Tag>> actualTags = Iterables.transform(actual, getTagsFunction);
    Truth.assertThat(actualTags).containsExactlyElementsIn(expectedTags);
    for (DistributionAggregation expectedAgg : expected) {
      DistributionAggregation actualAgg =
          Iterables.find(
              actual,
              Predicates.compose(Predicates.equalTo(expectedAgg.getTags()), getTagsFunction));
      assertDistributionAggregationValuesEquivalent(
          "DistributionAggregation tags=" + expectedAgg.getTags(),
          tolerance,
          expectedAgg,
          actualAgg);
    }
  }

  private static void assertDistributionAggregationValuesEquivalent(
      String msg, double tolerance, DistributionAggregation agg1, DistributionAggregation agg2) {
    Truth.assertWithMessage(msg + " count").that(agg1.getCount()).isEqualTo(agg2.getCount());
    Truth.assertWithMessage(msg + " mean")
        .that(agg1.getMean())
        .isWithin(tolerance)
        .of(agg2.getMean());
    Truth.assertWithMessage(msg + " sum").that(agg1.getSum()).isWithin(tolerance).of(agg2.getSum());
    Truth.assertWithMessage(msg + " range").that(agg1.getRange()).isEqualTo(agg2.getRange());
    Truth.assertWithMessage(msg + " bucket counts")
        .that(removeTrailingZeros(agg1.getBucketCounts()))
        .isEqualTo(removeTrailingZeros(agg2.getBucketCounts()));
  }

  private static List<Long> removeTrailingZeros(List<Long> longs) {
    List<Long> truncated = new ArrayList<Long>(longs);
    while (!truncated.isEmpty() && Iterables.getLast(truncated) == 0) {
      truncated.remove(truncated.size() - 1);
    }
    return truncated;
  }
}

@@ -837,70 +837,18 @@
 til.
-Arrays;%0Aimport java.util.Collection;%0Aimport java.util.Iterator
+Collection
 ;%0Aim
@@ -963,328 +963,645 @@
 %0A%0A  
-/**%0A   * Creates a %7B@code StatsContextImpl%7D from a factory and a list of alternating tag keys and%0A   * values.%0A   *%0A   * @param factory the factory used to produce the %7B@code StatsContextImpl%7D.%0A   * @param tagKeysAndValues a list of alternating %7B@link TagKey%7Ds and %7B@link TagValue%7Ds. It must%0A   *     have an even length
+static StatsContextImpl createContext(%0A      StatsContextFactoryImpl factory, TagKey key, TagValue value) %7B%0A    return createContext(factory, Tag.create(key, value));%0A  %7D%0A%0A  static StatsContextImpl createContext(%0A      StatsContextFactoryImpl factory, TagKey key1, TagValue value1, TagKey key2, TagValue value2) %7B%0A    return createContext(factory, Tag.create(key1, value1), Tag.create(key2, value2));%0A  %7D%0A%0A  /**%0A   * Creates a %7B@code StatsContextImpl%7D from a factory and a list tags.%0A   *%0A   * @param factory the factory used to produce the %7B@code StatsContextImpl%7D.%0A   * @param tags a list of tags to add to the %7B@code StatsContextImpl%7D
 .%0A  
@@ -1657,22 +1657,11 @@
 ven 
-keys and value
+tag
 s.%0A 
@@ -1663,24 +1663,32 @@
 gs.%0A   */%0A  
+private 
 static Stats
@@ -1757,33 +1757,18 @@
 ry, 
-Object
+Tag
 ... tag
-KeysAndValue
 s) %7B
@@ -1852,200 +1852,72 @@
 or (
-Iterator%3CObject%3E i = Arrays.asList(tagKeysAndValues).iterator(); i.hasNext(); ) %7B%0A      TagKey key = (TagKey) i.next();%0A      TagValue value = (TagValue) i.next();%0A      builder.set(key, v
+Tag tag : tags) %7B%0A      builder.set(tag.getKey(), tag.getV
 alue
+()
 );%0A 

Refactor testing util to not use Object type.