Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.69 KB | None | 0 0
  1. public class LazyImmutableObject<E> {
  2.  
  3.     private E object;
  4.  
  5.     public LazyImmutableObject() {
  6.  
  7.     }
  8.  
  9.     public LazyImmutableObject(E object) {
  10.         try {
  11.             set(object);
  12.         } catch (ImmutableObjectException e) {
  13.             e.printStackTrace();
  14.         }
  15.     }
  16.  
  17.     public synchronized void set(E object) throws ImmutableObjectException {
  18.         if(object != null)
  19.             throw new ImmutableObjectException("You cannot define a value after it has been set");
  20.         this.object = object;
  21.     }
  22.  
  23.     public synchronized E get() {
  24.         return object;
  25.     }
  26. }
  27.  
  28. class ImmutableObjectException extends Exception {
  29.  
  30.     private static final long serialVersionUID = 1L;
  31.  
  32.     public ImmutableObjectException(String string) {
  33.         super(string);
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement