Advertisement
MightyPork

Mutable<T>

Mar 28th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. package mightypork.utils.objects;
  2.  
  3.  
  4. /**
  5.  * Mutable object
  6.  *
  7.  * @author MightyPork
  8.  * @param <T> type
  9.  */
  10. public class Mutable<T> {
  11.  
  12.     /** The wrapped value */
  13.     private T o = null;
  14.  
  15.  
  16.     /**
  17.      * New mutable object
  18.      *
  19.      * @param o value
  20.      */
  21.     public Mutable(T o) {
  22.         this.o = o;
  23.     }
  24.  
  25.  
  26.     /**
  27.      * Get the wrapped value
  28.      *
  29.      * @return value
  30.      */
  31.     public T get()
  32.     {
  33.         return o;
  34.     }
  35.  
  36.  
  37.     /**
  38.      * Set value
  39.      *
  40.      * @param o new value to set
  41.      */
  42.     public void set(T o)
  43.     {
  44.         this.o = o;
  45.     }
  46.  
  47.  
  48.     @Override
  49.     public int hashCode()
  50.     {
  51.         final int prime = 31;
  52.         int result = 1;
  53.         result = prime * result + ((o == null) ? 0 : o.hashCode());
  54.         return result;
  55.     }
  56.  
  57.  
  58.     @Override
  59.     public boolean equals(Object obj)
  60.     {
  61.         if (this == obj) return true;
  62.         if (obj == null) return false;
  63.         if (!(obj instanceof Mutable)) return false;
  64.        
  65.         Mutable<?> other = (Mutable<?>) obj;       
  66.         if (o == null) {
  67.             if (other.o != null) return false;
  68.         } else if (!o.equals(other.o)) {
  69.             return false;
  70.         }
  71.         return true;
  72.     }
  73.    
  74.     @Override
  75.     public String toString()
  76.     {
  77.         if(o == null) return "<null>";
  78.         return o.toString();
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement