/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.sdk.trace;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

/** A map with a fixed capacity that drops attributes when the map gets full. */
@SuppressWarnings({"rawtypes", "unchecked"})
final class AttributesMap extends HashMap<AttributeKey<?>, Object> implements Attributes {

  private static final long serialVersionUID = -5072696312123632376L;

  private final long capacity;
  private int totalAddedValues = 0;

  AttributesMap(long capacity) {
    this.capacity = capacity;
  }

  <T> void put(AttributeKey<T> key, T value) {
    totalAddedValues++;
    if (size() >= capacity && !containsKey(key)) {
      return;
    }
    super.put(key, value);
  }

  int getTotalAddedValues() {
    return totalAddedValues;
  }

  @SuppressWarnings("unchecked")
  @Override
  public <T> T get(AttributeKey<T> key) {
    return (T) super.get(key);
  }

  @SuppressWarnings({"rawtypes", "unchecked"})
  @Override
  public void forEach(BiConsumer<? super AttributeKey<?>, ? super Object> consumer) {
    for (Map.Entry<AttributeKey<?>, Object> entry : entrySet()) {
      AttributeKey key = entry.getKey();
      Object value = entry.getValue();
      consumer.accept((AttributeKey<?>) key, value);
    }
  }

  @Override
  public Map<AttributeKey<?>, Object> asMap() {
    // Because Attributes is marked Immutable, IDEs may recognize this as redundant usage. However,
    // this class is private and is actually mutable, so we need to wrap with unmodifiableMap
    // anyways. We implement the immutable Attributes for this class to support the
    // Attributes.builder().putAll usage - it is tricky but an implementation detail of this private
    // class.
    return Collections.unmodifiableMap(this);
  }

  @Override
  public AttributesBuilder toBuilder() {
    return Attributes.builder().putAll(this);
  }

  @Override
  public String toString() {
    return "AttributesMap{"
        + "data="
        + super.toString()
        + ", capacity="
        + capacity
        + ", totalAddedValues="
        + totalAddedValues
        + '}';
  }

  Attributes immutableCopy() {
    return Attributes.builder().putAll(this).build();
  }
}

@@ -347,46 +347,8 @@
 Map;
-%0Aimport java.util.function.BiConsumer;
 %0A%0A/*
@@ -430,53 +430,8 @@
  */%0A
-@SuppressWarnings(%7B%22rawtypes%22, %22unchecked%22%7D)%0A
 fina
@@ -1090,363 +1090,8 @@
  %7D%0A%0A
-  @SuppressWarnings(%7B%22rawtypes%22, %22unchecked%22%7D)%0A  @Override%0A  public void forEach(BiConsumer%3C? super AttributeKey%3C?%3E, ? super Object%3E consumer) %7B%0A    for (Map.Entry%3CAttributeKey%3C?%3E, Object%3E entry : entrySet()) %7B%0A      AttributeKey key = entry.getKey();%0A      Object value = entry.getValue();%0A      consumer.accept((AttributeKey%3C?%3E) key, value);%0A    %7D%0A  %7D%0A%0A
   @O

Remove unneeded forEach from AttributesMap. (#3266)