View Javadoc

1   package cz.cuni.amis.utils;
2   
3   /**
4    * Utility class for lazy initialization of objects.
5    * <p><p>
6    * THREAD-UNSAFE!
7    * 
8    * @author ik
9    */
10  public abstract class Lazy<T> {
11  
12      protected T obj = null;
13  
14      /**
15       * Creates lazy initialized object.
16       * @return
17       */
18      abstract protected T create();
19  
20      /**
21       * @deprecated
22       * @return value created by {@link Lazy#create()} (UNSYNCHRONIZED CREATION!)
23       */
24      public T getVal() {
25          if (obj == null) {
26              obj = create();
27          }
28          return obj;
29      }
30      
31      /**
32       * Synonym for {@link Lazy#getVal()}.
33       * @return
34       */
35      public T get() {
36          return getVal();
37      }
38      
39      /**
40       * Sets value that should be returned via {@link Lazy#get()}.
41       * @param val
42       */
43      public void set(T val) {
44      	this.obj = val;
45      }
46  }