Class 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 java.lang.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 Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.Object[] data  
      private int hashcode  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected ImmutableKeyValuePairs​(java.lang.Object[] data)
      Stores the raw object data directly.
      protected ImmutableKeyValuePairs​(java.lang.Object[] data, java.util.Comparator<?> keyComparator)
      Sorts and dedupes the key/value pairs in data.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.Map<K,​V> asMap()  
      private static <K> int compareToNullSafe​(K key, K pivotKey, java.util.Comparator<K> keyComparator)  
      protected java.util.List<java.lang.Object> data()  
      private static <K> java.lang.Object[] dedupe​(java.lang.Object[] data, java.util.Comparator<K> keyComparator)  
      boolean equals​(java.lang.Object o)  
      void forEach​(java.util.function.BiConsumer<? super K,​? super V> consumer)
      Iterates over all the key-value pairs of labels contained by this instance.
      V get​(K key)
      Returns the value for the given key, or null if the key is not present.
      java.lang.Object[] getData()
      Return the backing data array for these attributes.
      int hashCode()  
      boolean isEmpty()  
      private static <K> void merge​(java.lang.Object[] sourceArray, int beginIndex, int middleIndex, int endIndex, java.lang.Object[] targetArray, java.util.Comparator<K> keyComparator)
      Left source half is sourceArray[ beginIndex:middleIndex-1].
      private static void mergeSort​(java.lang.Object[] data, java.util.Comparator<?> keyComparator)  
      int size()  
      private static java.lang.Object[] sortAndFilter​(java.lang.Object[] data, java.util.Comparator<?> keyComparator)
      Sorts and dedupes the key/value pairs in data.
      private static void splitAndMerge​(java.lang.Object[] workArray, int beginIndex, int endIndex, java.lang.Object[] targetArray, java.util.Comparator<?> keyComparator)
      Sort the given run of array targetArray[] using array workArray[] as a source.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • data

        private final java.lang.Object[] data
      • hashcode

        private int hashcode
    • Constructor Detail

      • ImmutableKeyValuePairs

        protected ImmutableKeyValuePairs​(java.lang.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​(java.lang.Object[] data,
                                         java.util.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 Detail

      • data

        protected final java.util.List<java.lang.Object> data()
      • size

        public final int size()
      • isEmpty

        public final boolean isEmpty()
      • asMap

        public final java.util.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​(java.util.function.BiConsumer<? super K,​? super V> consumer)
        Iterates over all the key-value pairs of labels contained by this instance.
      • sortAndFilter

        private static java.lang.Object[] sortAndFilter​(java.lang.Object[] data,
                                                        java.util.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​(java.lang.Object[] data,
                                      java.util.Comparator<?> keyComparator)
      • splitAndMerge

        private static void splitAndMerge​(java.lang.Object[] workArray,
                                          int beginIndex,
                                          int endIndex,
                                          java.lang.Object[] targetArray,
                                          java.util.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​(java.lang.Object[] sourceArray,
                                      int beginIndex,
                                      int middleIndex,
                                      int endIndex,
                                      java.lang.Object[] targetArray,
                                      java.util.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,
                                                 java.util.Comparator<K> keyComparator)
      • dedupe

        private static <K> java.lang.Object[] dedupe​(java.lang.Object[] data,
                                                     java.util.Comparator<K> keyComparator)
      • equals

        public boolean equals​(@Nullable
                              java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

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

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

        public java.lang.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.