Advertisement
Centril

PassByAwesome

Apr 28th, 2013
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. package com.skipifzero.superiority.utils;
  2.  
  3. import com.google.common.base.Objects;
  4.  
  5. /**
  6.  * <p>Oh Oracle, why do thy smite me with lack of pass-by-reference?<br/>
  7.  * I shallt redeem this injustice with providing me with C++ goodies.</p>
  8.  *
  9.  * @author Centril<twingoow@gmail.com> / Mazdak Farrokhzad.
  10.  * @since 28 apr 2013
  11.  * @version 1.0
  12.  * @see http://stackoverflow.com/questions/430479/how-do-i-use-an-equivalent-to-c-reference-parameters-in-java
  13.  */
  14. public final class _<E> {
  15.     private E ref;
  16.  
  17.     /**
  18.      * Constructs the reference with null as value.
  19.      */
  20.     public _() {
  21.     }
  22.  
  23.     /**
  24.      * Constructs the reference with initial value.<br/>
  25.      * Null is allowed.
  26.      *
  27.      * @param value the value to set.
  28.      */
  29.     public _( E value ) {
  30.         this.ref = value;
  31.     }
  32.  
  33.     /**
  34.      * Returns the value of the reference.
  35.      *
  36.      * @return the value of the reference.
  37.      */
  38.     public E r() {
  39.         return this.ref;
  40.     }
  41.  
  42.     /**
  43.      * Sets the value of the reference.
  44.      *
  45.      * @param value the value to set.
  46.      */
  47.     public void r( E value ) {
  48.         this.ref = value;
  49.     }
  50.  
  51.     /**
  52.      * The given obj is equal to this iff one of the following:<br/>
  53.      * this == obj, the {@link #r()} equals obj.r() if obj instanceof {@link _},<br/>
  54.      * or if {@link #r()} equals obj.
  55.      */
  56.     public boolean equals( Object obj ) {
  57.         return  this == obj || (obj != null && obj instanceof _
  58.             ?   Objects.equal( this.r(), ((_<?>) obj).r() )
  59.             :   Objects.equal( this.r(), obj ));
  60.     }
  61.  
  62.     /**
  63.      * If {@link #r()} == null => 0,<br/>
  64.      * otherwise the hashcode of {@link #r()}.
  65.      */
  66.     @Override
  67.     public int hashCode() {
  68.         return this.r() == null ? 0 : this.r().hashCode();
  69.     }
  70.  
  71.     /**
  72.      * Relays to the values toString method.
  73.      *
  74.      * @return the value as string.
  75.      */
  76.     public String toString() {
  77.         return ref.toString();
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement