All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.opentelemetry.exporter.internal.otlp.AttributeArrayAnyValueStatelessMarshaler Maven / Gradle / Ivy

There is a newer version: 1.45.0
Show newest version
/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.exporter.internal.otlp;

import io.opentelemetry.api.common.AttributeType;
import io.opentelemetry.exporter.internal.marshal.MarshalerContext;
import io.opentelemetry.exporter.internal.marshal.Serializer;
import io.opentelemetry.exporter.internal.marshal.StatelessMarshaler2;
import io.opentelemetry.exporter.internal.marshal.StatelessMarshalerUtil;
import io.opentelemetry.proto.common.v1.internal.ArrayValue;
import java.io.IOException;
import java.util.List;

/** See {@link ArrayAnyValueMarshaler}. */
final class AttributeArrayAnyValueStatelessMarshaler
    implements StatelessMarshaler2> {
  static final AttributeArrayAnyValueStatelessMarshaler INSTANCE =
      new AttributeArrayAnyValueStatelessMarshaler<>();

  private AttributeArrayAnyValueStatelessMarshaler() {}

  @SuppressWarnings("unchecked")
  @Override
  public void writeTo(Serializer output, AttributeType type, List list, MarshalerContext context)
      throws IOException {
    switch (type) {
      case STRING_ARRAY:
        output.serializeRepeatedMessageWithContext(
            ArrayValue.VALUES,
            (List) list,
            StringAnyValueStatelessMarshaler.INSTANCE,
            context);
        return;
      case LONG_ARRAY:
        output.serializeRepeatedMessageWithContext(
            ArrayValue.VALUES, (List) list, IntAnyValueStatelessMarshaler.INSTANCE, context);
        return;
      case BOOLEAN_ARRAY:
        output.serializeRepeatedMessageWithContext(
            ArrayValue.VALUES,
            (List) list,
            BoolAnyValueStatelessMarshaler.INSTANCE,
            context);
        return;
      case DOUBLE_ARRAY:
        output.serializeRepeatedMessageWithContext(
            ArrayValue.VALUES,
            (List) list,
            DoubleAnyValueStatelessMarshaler.INSTANCE,
            context);
        return;
      default:
        throw new IllegalArgumentException("Unsupported attribute type.");
    }
  }

  @SuppressWarnings("unchecked")
  @Override
  public int getBinarySerializedSize(AttributeType type, List list, MarshalerContext context) {
    switch (type) {
      case STRING_ARRAY:
        return StatelessMarshalerUtil.sizeRepeatedMessageWithContext(
            ArrayValue.VALUES,
            (List) list,
            StringAnyValueStatelessMarshaler.INSTANCE,
            context);
      case LONG_ARRAY:
        return StatelessMarshalerUtil.sizeRepeatedMessageWithContext(
            ArrayValue.VALUES, (List) list, IntAnyValueStatelessMarshaler.INSTANCE, context);
      case BOOLEAN_ARRAY:
        return StatelessMarshalerUtil.sizeRepeatedMessageWithContext(
            ArrayValue.VALUES,
            (List) list,
            BoolAnyValueStatelessMarshaler.INSTANCE,
            context);
      case DOUBLE_ARRAY:
        return StatelessMarshalerUtil.sizeRepeatedMessageWithContext(
            ArrayValue.VALUES,
            (List) list,
            DoubleAnyValueStatelessMarshaler.INSTANCE,
            context);
      default:
        throw new IllegalArgumentException("Unsupported attribute type.");
    }
  }
}