Class ImmutableKeyValuePairs<K,V>

java.lang.Object
io.opentelemetry.api.internal.ImmutableKeyValuePairs<K,V>
Type Parameters:
V - The type of the values contained in this.
Direct Known Subclasses:
ArrayBackedAttributes, ImmutableBaggage

@Immutable public abstract class ImmutableKeyValuePairs<K,V> extends Object
An immutable set of key-value pairs.

Key-value pairs are dropped for null or empty keys.

Note: for subclasses of this, null keys will be removed, but if your key has another concept of being "empty", you'll need to remove them before calling the constructor, assuming you don't want the "empty" keys to be kept in your collection.

This class is internal and is hence not for public use. Its APIs are unstable and can change at any time.

  • Field Details

    • data

      private final Object[] data
    • hashcode

      private int hashcode
  • Constructor Details

    • ImmutableKeyValuePairs

      protected ImmutableKeyValuePairs(Object[] data)
      Stores the raw object data directly. Does not do any de-duping or sorting. If you use this constructor, you *must* guarantee that the data has been de-duped and sorted by key before it is passed here.
    • ImmutableKeyValuePairs

      protected ImmutableKeyValuePairs(Object[] data, Comparator<?> keyComparator)
      Sorts and dedupes the key/value pairs in data. null values will be removed. Keys will be compared with the given Comparator.
  • Method Details

    • data

      protected final List<Object> data()
    • size

      public final int size()
    • isEmpty

      public final boolean isEmpty()
    • asMap

      public final Map<K,V> asMap()
    • get

      @Nullable public final V get(K key)
      Returns the value for the given key, or null if the key is not present.
    • forEach

      public final void forEach(BiConsumer<? super K,? super V> consumer)
      Iterates over all the key-value pairs of labels contained by this instance.
    • sortAndFilter

      private static Object[] sortAndFilter(Object[] data, Comparator<?> keyComparator)
      Sorts and dedupes the key/value pairs in data. null values will be removed. Keys will be compared with the given Comparator.
    • mergeSort

      private static void mergeSort(Object[] data, Comparator<?> keyComparator)
    • splitAndMerge

      private static void splitAndMerge(Object[] workArray, int beginIndex, int endIndex, Object[] targetArray, Comparator<?> keyComparator)
      Sort the given run of array targetArray[] using array workArray[] as a source. beginIndex is inclusive; endIndex is exclusive (targetArray[endIndex] is not in the set).
    • merge

      private static <K> void merge(Object[] sourceArray, int beginIndex, int middleIndex, int endIndex, Object[] targetArray, Comparator<K> keyComparator)
      Left source half is sourceArray[ beginIndex:middleIndex-1]. Right source half is sourceArray[ middleIndex:endIndex-1]. Result is targetArray[ beginIndex:endIndex-1].
    • compareToNullSafe

      private static <K> int compareToNullSafe(@Nullable K key, @Nullable K pivotKey, Comparator<K> keyComparator)
    • dedupe

      private static <K> Object[] dedupe(Object[] data, Comparator<K> keyComparator)
    • equals

      public boolean equals(@Nullable Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getData

      public Object[] getData()
      Return the backing data array for these attributes. This is only exposed for internal use by opentelemetry authors. The contents of the array MUST NOT be modified.