Class LazyFeatures<H>

java.lang.Object
de.team33.patterns.lazy.narvi.LazyFeatures<H>
Type Parameters:
H - The host type.

public abstract class LazyFeatures<H> extends Object
A tool for managing properties of a host instance, which typically result from other properties of the host and are only actually determined when needed. Once determined, properties are retained until reset.

Example:

 public class Sample {

     // to manage some "lazy" features ...
     // ----------------------------------
     private final transient Features features = new Features();

     // some "normal" properties (with corresponding getters and setters) ...
     // ---------------------------------------------------------------------
     private int intValue;
     private String stringValue;
     private Instant instantValue;

     public final int getIntValue() {
         return intValue;
     }

     public final Sample setIntValue(final int intValue) {
         // when a "normal" property is modified some "lazy" features will expire ...
         // -------------------------------------------------------------------------
         features.reset();

         this.intValue = intValue;
         return this;
     }

     public final String getStringValue() {
         return stringValue;
     }

     public final Sample setStringValue(final String stringValue) {
         features.reset(); // s.a. - features will expire
         this.stringValue = stringValue;
         return this;
     }

     public final Instant getInstantValue() {
         return instantValue;
     }

     public final Sample setInstantValue(final Instant instantValue) {
         features.reset(); // s.a. - features will expire
         this.instantValue = instantValue;
         return this;
     }

     // A List representation of this instance - a "lazy" feature ...
     // -------------------------------------------------------------
     public final List<Object> toList() {
         return features.get(Key.LIST);
     }

     @Override
     public final boolean equals(final Object obj) {
         return (this == obj) || ((obj instanceof final Sample other) && toList().equals(other.toList()));
     }

     // Also provided as "lazy" feature ...
     // -----------------------------------
     @Override
     public final int hashCode() {
         return features.get(Key.HASH);
     }

     // Also provided as "lazy" feature ...
     // -----------------------------------
     @Override
     public final String toString() {
         return features.get(Key.STRING);
     }

     // A local Features derivative (also works as host <H> here) ...
     // -------------------------------------------------------------
     private class Features extends LazyFeatures<Features> {

         @Override
         protected final Features host() {
             return this;
         }

         // --------------------------------------------------------------
         // ... and provides factory methods for the "lazy" features ...
         // --------------------------------------------------------------

         private List<Object> newList() {
             return Arrays.asList(intValue, stringValue, instantValue);
         }

         private Integer newHash() {
             return toList().hashCode();
         }

         private String newString() {
             return Sample.class.getSimpleName() + toList().toString();
         }
     }

     // A local Key derivative ...
     // --------------------------
     @FunctionalInterface
     private interface Key<R> extends LazyFeatures.Key<Features, R> {

         // ... to simplify the local Key definitions ...
         // ---------------------------------------------
         Key<List<Object>> LIST = Features::newList;
         Key<Integer> HASH = Features::newHash;
         Key<String> STRING = Features::newString;
     }
 }
 
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Abstracts the keys needed to access values managed by a LazyFeatures instance.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    final <R> R
    get(LazyFeatures.Key<? super H,? extends R> key)
    On the first call after initialization or reset, executes the initialization code provided by the given key and stores the result.
    protected abstract H
    Returns the actual host <H>.
    final <R> Optional<R>
    peek(LazyFeatures.Key<? super H,? extends R> key)
    Determines whether the initialization code provided by the given key has already been executed and its result stored.
    final void
    Resets (discards) all the stored results.
    final void
    Resets (discards) the stored result for the given key.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • LazyFeatures

      public LazyFeatures()
  • Method Details

    • host

      protected abstract H host()
      Returns the actual host <H>. This is the instance that finally provides the initialization code referred by the keys.
    • peek

      public final <R> Optional<R> peek(LazyFeatures.Key<? super H,? extends R> key)
      Determines whether the initialization code provided by the given key has already been executed and its result stored.

      Returns an Optional containing the stored result if any, otherwise Optional.empty().

      CAUTION: returns Optional.empty() even if a stored result exists but is null!

      Type Parameters:
      R - The result type.
    • get

      public final <R> R get(LazyFeatures.Key<? super H,? extends R> key)
      On the first call after initialization or reset, executes the initialization code provided by the given key and stores the result.

      Returns the stored result.

      Type Parameters:
      R - The result type.
      See Also:
    • reset

      public final void reset(LazyFeatures.Key<?,?> key)
      Resets (discards) the stored result for the given key.
    • reset

      public final void reset()
      Resets (discards) all the stored results.