com.aliyun.openservices.shade.io.opentelemetry.api.common.AttributesBuilder Maven / Gradle / Ivy
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package com.aliyun.openservices.shade.com.aliyun.openservices.shade.io.opentelemetry.api.common;
import static com.aliyun.openservices.shade.com.aliyun.openservices.shade.io.opentelemetry.api.common.ArrayBackedAttributesBuilder.toList;
import static com.aliyun.openservices.shade.com.aliyun.openservices.shade.io.opentelemetry.api.common.AttributeKey.booleanArrayKey;
import static com.aliyun.openservices.shade.com.aliyun.openservices.shade.io.opentelemetry.api.common.AttributeKey.booleanKey;
import static com.aliyun.openservices.shade.com.aliyun.openservices.shade.io.opentelemetry.api.common.AttributeKey.doubleArrayKey;
import static com.aliyun.openservices.shade.com.aliyun.openservices.shade.io.opentelemetry.api.common.AttributeKey.doubleKey;
import static com.aliyun.openservices.shade.com.aliyun.openservices.shade.io.opentelemetry.api.common.AttributeKey.longArrayKey;
import static com.aliyun.openservices.shade.com.aliyun.openservices.shade.io.opentelemetry.api.common.AttributeKey.longKey;
import static com.aliyun.openservices.shade.com.aliyun.openservices.shade.io.opentelemetry.api.common.AttributeKey.stringArrayKey;
import static com.aliyun.openservices.shade.com.aliyun.openservices.shade.io.opentelemetry.api.common.AttributeKey.stringKey;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
/** A builder of {@link Attributes} supporting an arbitrary number of key-value pairs. */
public interface AttributesBuilder {
/** Create the {@link Attributes} from this. */
Attributes build();
/**
* Puts a {@link AttributeKey} with associated value into this.
*
* The type parameter is unused.
*/
// The type parameter was added unintentionally and unfortunately it is an API break for
// implementations of this interface to remove it. It doesn't affect users of the interface in
// any way, and has almost no effect on implementations, so we leave it until a future major
// version.
AttributesBuilder put(AttributeKey key, int value);
/** Puts a {@link AttributeKey} with associated value into this. */
AttributesBuilder put(AttributeKey key, T value);
/**
* Puts a String attribute into this.
*
* Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate
* your keys, if possible.
*
* @return this Builder
*/
default AttributesBuilder put(String key, String value) {
return put(stringKey(key), value);
}
/**
* Puts a long attribute into this.
*
*
Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate
* your keys, if possible.
*
* @return this Builder
*/
default AttributesBuilder put(String key, long value) {
return put(longKey(key), value);
}
/**
* Puts a double attribute into this.
*
*
Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate
* your keys, if possible.
*
* @return this Builder
*/
default AttributesBuilder put(String key, double value) {
return put(doubleKey(key), value);
}
/**
* Puts a boolean attribute into this.
*
*
Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate
* your keys, if possible.
*
* @return this Builder
*/
default AttributesBuilder put(String key, boolean value) {
return put(booleanKey(key), value);
}
/**
* Puts a String array attribute into this.
*
*
Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate
* your keys, if possible.
*
* @return this Builder
*/
default AttributesBuilder put(String key, String... value) {
if (value == null) {
return this;
}
return put(stringArrayKey(key), Arrays.asList(value));
}
/**
* Puts a List attribute into this.
*
* @return this Builder
*/
@SuppressWarnings("unchecked")
default AttributesBuilder put(AttributeKey> key, T... value) {
if (value == null) {
return this;
}
return put(key, Arrays.asList(value));
}
/**
* Puts a Long array attribute into this.
*
* Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate
* your keys, if possible.
*
* @return this Builder
*/
default AttributesBuilder put(String key, long... value) {
if (value == null) {
return this;
}
return put(longArrayKey(key), toList(value));
}
/**
* Puts a Double array attribute into this.
*
*
Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate
* your keys, if possible.
*
* @return this Builder
*/
default AttributesBuilder put(String key, double... value) {
if (value == null) {
return this;
}
return put(doubleArrayKey(key), toList(value));
}
/**
* Puts a Boolean array attribute into this.
*
*
Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate
* your keys, if possible.
*
* @return this Builder
*/
default AttributesBuilder put(String key, boolean... value) {
if (value == null) {
return this;
}
return put(booleanArrayKey(key), toList(value));
}
/**
* Puts all the provided attributes into this Builder.
*
* @return this Builder
*/
AttributesBuilder putAll(Attributes attributes);
/**
* Remove all attributes where {@link AttributeKey#getKey()} and {@link AttributeKey#getType()}
* match the {@code key}.
*
* @return this Builder
*/
default AttributesBuilder remove(AttributeKey key) {
// default implementation is no-op
return this;
}
/**
* Remove all attributes that satisfy the given predicate. Errors or runtime exceptions thrown by
* the predicate are relayed to the caller.
*
* @return this Builder
*/
default AttributesBuilder removeIf(Predicate> filter) {
// default implementation is no-op
return this;
}
}