Record Class KeyValue

java.lang.Object
java.lang.Record
io.jstach.ezkv.kvs.KeyValue
Record Components:
key - the key associated with this key-value pair.
expanded - the value after any interpolation.
meta - additional metadata associated with this key-value pair.

public record KeyValue(String key, String expanded, KeyValue.Meta meta) extends Record
Represents a key-value pair in the Ezkv configuration system. Unlike a simple Map.Entry<String, String>, this record holds additional metadata that provides more context about the key-value pair, such as:
  • The original raw value (before any interpolation).
  • The expanded value (after interpolation).
  • Source information indicating where the key-value pair was loaded from.
  • Flags indicating special behavior (e.g., sensitive data or disabling interpolation).
This class is immutable and provides methods for handling sensitive data, interpolation, and chaining resources for further key-value loading.

Example Usage

The following example shows how to load key-values using the Ezkv system:

var kvs = KeyValuesSystem.defaults()
	.loader()
	.add("classpath:/start.properties")
	.add("system:///")
	.add("env:///")
	.load();

// Accessing key-value pairs:
for (KeyValue kv : kvs) {
	System.out.println("Key: " + kv.key() + ", Value: " + kv.value());
}
  • Field Details

  • Constructor Details

    • KeyValue

      public KeyValue(String key, String expanded, KeyValue.Meta meta)
      Constructs a new KeyValue with the given key, expanded value, and associated metadata.
      Parameters:
      key - the key associated with this key-value pair (cannot be null).
      expanded - the value after any interpolation (cannot be null).
      meta - additional metadata (cannot be null).
    • KeyValue

      public KeyValue(String key, String raw)
      Constructs a new KeyValue with a raw value, using the provided key and raw value. The KeyValue.Meta information will be initialized with default settings.
      Parameters:
      key - the key associated with this key-value pair.
      raw - the raw (unexpanded) value associated with this key.
  • Method Details

    • value

      public String value()
      Gets the expanded value after interpolation.
      Returns:
      the expanded value.
    • raw

      public String raw()
      Gets the original raw value before any interpolation.
      Returns:
      the raw value.
    • withKey

      public KeyValue withKey(String key)
      Creates a new KeyValue with an updated key.
      Parameters:
      key - the new key.
      Returns:
      new key value.
    • withExpanded

      public KeyValue withExpanded(String expanded)
      Creates a new KeyValue with an updated expanded value.
      Parameters:
      expanded - the new expanded value.
      Returns:
      a new KeyValue with the updated expanded value.
    • withExpanded

      public KeyValue withExpanded(Function<String,@Nullable String> expanded)
      Returns a new KeyValue instance with its value expanded using the provided function. The expansion function takes the key as input and returns the expanded value, or null if no expansion is necessary. If an expanded value is provided by the function, a new KeyValue instance is created with that expanded value; otherwise, the current instance is returned unchanged.
      Parameters:
      expanded - a function that takes the key as input and returns the expanded value or null if no expansion is needed
      Returns:
      a new KeyValue instance with the expanded value, or the original instance if no expansion was applied
    • addFlags

      Adds additional flags to this KeyValue and returns a new instance.
      Parameters:
      flagsCol - a collection of flags to add.
      Returns:
      a new KeyValue with the added flags.
    • isNoInterpolation

      public boolean isNoInterpolation()
      Checks if the KeyValue.Flag.NO_INTERPOLATION flag is set on this KeyValue. This indicates that the value should not undergo any interpolation or variable substitution.
      Returns:
      true if the NO_INTERPOLATION flag is set, otherwise false.
    • isSensitive

      public boolean isSensitive()
      Checks if the KeyValue.Flag.SENSITIVE flag is set on this KeyValue. A sensitive value is marked to indicate that it should be treated as confidential and may be redacted when displayed to prevent leaking sensitive information. It also means it cannot be used for interpolation with the exception of it being allowed locally in the same resource!
      Returns:
      true if the SENSITIVE flag is set, otherwise false.
    • isFlag

      public boolean isFlag(KeyValue.Flag flag)
      Checks if a specific flag is set on this KeyValue.
      Parameters:
      flag - the flag to check
      Returns:
      true if the specified flag is set, otherwise false.
    • redact

      public KeyValue redact()
      Redacts the sensitive value in this key-value pair by replacing it with a default message.
      Returns:
      a new KeyValue with the sensitive data redacted.
    • redact

      public KeyValue redact(String redactMessage)
      Redacts the sensitive value in this key-value pair by replacing it with a custom message.
      Parameters:
      redactMessage - the message to use for redaction.
      Returns:
      a new KeyValue with the sensitive data redacted.
    • toString

      public 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 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 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

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

      public String expanded()
      Returns the value of the expanded record component.
      Returns:
      the value of the expanded record component
    • meta

      public KeyValue.Meta meta()
      Returns the value of the meta record component.
      Returns:
      the value of the meta record component