/*
 * Copyright 2014-present Facebook, 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.facebook.litho;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

public class TestPerfEvent implements PerfEvent {

  private static final AtomicInteger sInstanceCounter = new AtomicInteger(0);
  private final int mMarkerId;
  private final Map<String, Object> mAnnotations = new HashMap<>();
  private final List<String> mPoints = new LinkedList<>();

  public TestPerfEvent(int markerId) {
    mMarkerId = markerId;
  }

  /**
   * Identify a particular instance of a performance event if there are multiple, parallel events of
   * the same marker id.
   */
  @Override
  public int getInstanceKey() {
    return sInstanceCounter.getAndIncrement();
  }

  /** Identify the type of a performance event. */
  @Override
  public int getMarkerId() {
    return mMarkerId;
  }

  /**
   * Adds a key:value annotation to an already active marker.
   *
   * @param annotationKey
   * @param annotationValue
   */
  @Override
  public void markerAnnotate(String annotationKey, String annotationValue) {
    mAnnotations.put(annotationKey, annotationValue);
  }

  /**
   * @param annotationKey
   * @param annotationValue
   * @see #markerAnnotate(String, String)
   */
  @Override
  public void markerAnnotate(String annotationKey, double annotationValue) {
    mAnnotations.put(annotationKey, annotationValue);
  }

  /**
   * @param annotationKey
   * @param annotationValue
   * @see #markerAnnotate(String, String)
   */
  @Override
  public void markerAnnotate(String annotationKey, int annotationValue) {
    mAnnotations.put(annotationKey, annotationValue);
  }

  /**
   * @param annotationKey
   * @param annotationValue
   * @see #markerAnnotate(String, String)
   */
  @Override
  public void markerAnnotate(String annotationKey, boolean annotationValue) {
    mAnnotations.put(annotationKey, annotationValue);
  }

  /**
   * @param annotationKey
   * @param annotationValue
   * @see #markerAnnotate(String, String)
   */
  @Override
  public void markerAnnotate(String annotationKey, String[] annotationValue) {
    mAnnotations.put(annotationKey, annotationValue);
  }

  /**
   * @param annotationKey
   * @param annotationValue
   * @see #markerAnnotate(String, String)
   */
  @Override
  public void markerAnnotate(String annotationKey, Double[] annotationValue) {
    mAnnotations.put(annotationKey, annotationValue);
  }

  /**
   * @param annotationKey
   * @param annotationValue
   * @see #markerAnnotate(String, String)
   */
  @Override
  public void markerAnnotate(String annotationKey, int[] annotationValue) {
    mAnnotations.put(annotationKey, annotationValue);
  }

  /**
   * Annotate the current event with a sub-routing at the point of invocation under the given name.
   *
   * @param eventName
   */
  @Override
  public void markerPoint(String eventName) {
    mPoints.add(eventName);
  }

  public Map<String, Object> getAnnotations() {
    return mAnnotations;
  }

  public List<String> getPoints() {
    return mPoints;
  }
}

@@ -627,16 +627,41 @@
 litho;%0A%0A
+import java.util.Arrays;%0A
 import j
@@ -748,24 +748,50 @@
 a.util.Map;%0A
+import java.util.Objects;%0A
 import java.
@@ -3725,11 +3725,1249 @@
 nts;%0A  %7D
+%0A%0A  @Override%0A  public String toString() %7B%0A    final StringBuilder annotations = new StringBuilder();%0A    for (Map.Entry%3CString, Object%3E entry : mAnnotations.entrySet()) %7B%0A      annotations.append(entry.getKey());%0A      annotations.append(%22=%22);%0A      if (entry.getValue() instanceof Object%5B%5D) %7B%0A        annotations.append(Arrays.toString((Object%5B%5D) entry.getValue()));%0A      %7D else if (entry.getValue() instanceof int%5B%5D) %7B%0A        annotations.append(Arrays.toString((int%5B%5D) entry.getValue()));%0A      %7D else %7B%0A        annotations.append(entry.getValue());%0A      %7D%0A      annotations.append(%22;%22);%0A    %7D%0A%0A    return %22TestPerfEvent%7B%22%0A        + %22mMarkerId=%22%0A        + mMarkerId%0A        + %22, mAnnotations=%7B%22%0A        + annotations%0A        + %22%7D%22%0A        + %22, mPoints=%22%0A        + mPoints%0A        + '%7D';%0A  %7D%0A%0A  @Override%0A  public boolean equals(Object o) %7B%0A    if (this == o) return true;%0A    if (o == null %7C%7C getClass() != o.getClass()) return false;%0A    TestPerfEvent that = (TestPerfEvent) o;%0A    return mMarkerId == that.mMarkerId%0A        && Objects.equals(mAnnotations, that.mAnnotations)%0A        && Objects.equals(mPoints, that.mPoints);%0A  %7D%0A%0A  @Override%0A  public int hashCode() %7B%0A    return Objects.hash(mMarkerId, mAnnotations, mPoints);%0A  %7D
 %0A%7D%0A

Improve TestPerfEvent