class Lutaml::Model::OneEntryCache

hash allocation for the common case of a single repeated key.
When the key changes, the old entry is replaced. This avoids
is queried repeatedly for all rules of a given element.
Used for MappingRule#namespaced_names where the same parent_namespace
A single-entry cache that stores one key-value pair at a time.

def clear

Clear the cached entry.
def clear
  @key = nil
  @value = nil
  @filled = false
end

def empty?

def empty?
  !@filled
end

def fetch(key)

Uses @filled flag to distinguish "no cache" from "cached nil".
Returns the cached value if key matches, nil otherwise.
def fetch(key)
  return nil unless @filled
  return @value if @key == key
  nil
end

def fetch_or_compute(key)

Fetch cached value, computing via block on cache miss.
def fetch_or_compute(key)
  cached = fetch(key)
  return cached unless cached.nil?
  value = yield
  store(key, value)
  value
end

def initialize

def initialize
  @key = nil
  @value = nil
  @filled = false
end

def store(key, value)

Store a value for the given key, replacing any previous entry.
def store(key, value)
  @key = key
  @value = value
  @filled = true
  value
end