Advertisement
Guest User

Untitled

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