LazyRef.java

  1. package swingtree.style;

  2. import org.jspecify.annotations.Nullable;

  3. import java.util.Objects;

  4. final class LazyRef<T>
  5. {
  6.     private final CacheProducerAndValidator<T> _producerAndValidator;
  7.     private @Nullable T _value;


  8.     LazyRef(CacheProducerAndValidator<T> producerAndValidator) {
  9.         _producerAndValidator = Objects.requireNonNull(producerAndValidator);
  10.     }

  11.     final T getFor(BoxModelConf currentState, ComponentAreas context ) {
  12.         if ( _value == null )
  13.             _value = _producerAndValidator.produce(currentState, context);
  14.         return _value;
  15.     }

  16.     final boolean exists() {
  17.         return _value != null;
  18.     }
  19. }