Annotation Type Value.Lazy


  • @Documented
    @Target(METHOD)
    public static @interface Value.Lazy
    Lazy attributes cannot be set, defined as method that computes value, which is invoke lazily once and only once in a thread safe manner.
     @Value.Immutable
     public abstract class Order {
    
       public abstract List<Item> items();
    
       @Value.Lazy
       public int totalCost() {
         int cost = 0;
    
         for (Item i : items())
           cost += i.count() * i.price();
    
         return cost;
       }
     }
     

    This kind of attribute cannot be set during building, but they are lazily computed from other attributes and stored in non-final field, but initialization is guarded by synchronization with volatile field check. Should be applied to non-abstract method - attribute value initializer.

    In general, lazy attribute initializer is more safe than using Value.Derived attributes, lazy attribute's initializer method body can refer to abstract mandatory and container attributes as well as to other lazy attributes. Though lazy attributes act as Value.Auxiliary.