Record Class KeyedValue<T>

java.lang.Object
java.lang.Record
ch.njol.skript.lang.KeyedValue<T>
Type Parameters:
T - The type of the value associated with the key.
All Implemented Interfaces:
Map.Entry<String,T>

public record KeyedValue<T>(@NotNull String key, T value) extends Record implements Map.Entry<String,T>
A record that represents a key-value pair
  • Constructor Details

    • KeyedValue

      public KeyedValue(@NotNull @NotNull String key, @NotNull T value)
      Creates an instance of a KeyedValue record class.
      Parameters:
      key - the value for the key record component
      value - the value for the value record component
    • KeyedValue

      public KeyedValue(Map.Entry<String,T> entry)
  • Method Details

    • getKey

      public String getKey()
      Specified by:
      getKey in interface Map.Entry<String,T>
    • getValue

      public T getValue()
      Specified by:
      getValue in interface Map.Entry<String,T>
    • setValue

      public T setValue(T value)
      Specified by:
      setValue in interface Map.Entry<String,T>
    • withKey

      public KeyedValue<T> withKey(@NotNull @NotNull String newKey)
      Creates a new KeyedValue with the same value but a different key.
      Parameters:
      newKey - the new key for the KeyedValue
      Returns:
      a new KeyedValue with the specified key and the same value
    • withValue

      public <U> KeyedValue<U> withValue(@NotNull U newValue)
      Creates a new KeyedValue with the same key but a different value.
      Type Parameters:
      U - the type of the new value
      Parameters:
      newValue - the new value for the KeyedValue
      Returns:
      a new KeyedValue with the same key and the specified value
    • zip

      public static <T> KeyedValue<T> @NotNull [] zip(@NotNull T @NotNull [] values, @NotNull @NotNull String @Nullable [] keys)
      Zips the given values and keys into a KeyedValue array.
      Type Parameters:
      T - the type of the values
      Parameters:
      values - the values to zip
      keys - the keys to zip with the values, or null to use numerical indices (1, 2, 3, ..., n)
      Returns:
      an array of KeyedValues
      Throws:
      IllegalArgumentException - if the keys are present and the lengths of values and keys do not match
    • unzip

      public static <T> KeyedValue.UnzippedKeyValues<T> unzip(@NotNull @NotNull KeyedValue<T> @NotNull [] keyedValues)
      Unzips an array of KeyedValues into separate lists of keys and values.
      Type Parameters:
      T - The type of the values in the KeyedValues.
      Parameters:
      keyedValues - An array of KeyedValues to unzip.
      Returns:
      An KeyedValue.UnzippedKeyValues object containing two lists: one for keys and one for values.
    • unzip

      public static <T> KeyedValue.UnzippedKeyValues<T> unzip(Iterator<KeyedValue<T>> keyedValues)
      Unzips an iterator of KeyedValues into separate lists of keys and values.
      Type Parameters:
      T - The type of the values in the KeyedValues.
      Parameters:
      keyedValues - An iterator of KeyedValues to unzip.
      Returns:
      An KeyedValue.UnzippedKeyValues object containing two lists: one for keys and one for values.
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in interface Map.Entry<String,T>
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in interface Map.Entry<String,T>
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • key

      @NotNull public @NotNull String key()
      Returns the value of the key record component.
      Returns:
      the value of the key record component
    • value

      @NotNull public T value()
      Returns the value of the value record component.
      Returns:
      the value of the value record component