Advertisement
Guest User

Untitled

a guest
Aug 30th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. package net.amoebaman.util;
  2.  
  3. import java.util.Arrays;
  4.  
  5. import org.apache.commons.lang.Validate;
  6.  
  7. /**
  8.  * Represents a wrapper around an array class of an arbitrary reference type,
  9.  * which properly implements "value" hash code and equality functions.
  10.  * <p>
  11.  * This class is intended for use as a key to a map.
  12.  * </p>
  13.  * @author Glen Husman
  14.  * @param <E> The type of elements in the array.
  15.  * @see Arrays
  16.  */
  17. public final class ArrayWrapper<E> {
  18.  
  19.     /**
  20.      * Creates an array wrapper with some elements.
  21.      * @param elements The elements of the array.
  22.      */
  23.     public ArrayWrapper(E... elements){
  24.         setArray(elements);
  25.     }
  26.    
  27.     private E[] _array;
  28.    
  29.     /**
  30.      * Retrieves a reference to the wrapped array instance.
  31.      * @return The array wrapped by this instance.
  32.      */
  33.     public E[] getArray(){
  34.         return _array; 
  35.     }
  36.    
  37.     /**
  38.      * Set this wrapper to wrap a new array instance.
  39.      * @param array The new wrapped array.
  40.      */
  41.     public void setArray(E[] array){
  42.         Validate.notNull(array, "The array must not be null.");
  43.         _array = array;
  44.     }
  45.    
  46.     /**
  47.      * Determines if this object has a value equivalent to another object.
  48.      * @see Arrays#equals(Object[], Object[])
  49.      */
  50.     @SuppressWarnings("rawtypes")
  51.     @Override
  52.     public boolean equals(Object other)
  53.     {
  54.         if (!(other instanceof ArrayWrapper))
  55.         {
  56.             return false;
  57.         }
  58.         return Arrays.equals(_array, ((ArrayWrapper)other)._array);
  59.     }
  60.  
  61.     /**
  62.      * Gets the hash code represented by this objects value.
  63.      * @see Arrays#hashCode(Object[])
  64.      * @return This object's hash code.
  65.      */
  66.     @Override
  67.     public int hashCode()
  68.     {
  69.         return Arrays.hashCode(_array);
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement