View Javadoc

1   /*
2      Copyright 2007 Ramon Servadei
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15   */
16  package fulmine.util.reference;
17  
18  import java.util.Arrays;
19  
20  /**
21   * Wraps arbitrary values. Useful as a single reference to a list of arguments.
22   * Equality of {@link Values} instances is performed on the elements and their
23   * order.
24   * 
25   * @author Ramon Servadei
26   */
27  public class Values
28  {
29      /** The objects this instance is wrapping */
30      private final Object[] values;
31  
32      public Values(Object... values)
33      {
34          this.values = values;
35      }
36  
37      /**
38       * Get the backing array holding the values
39       * 
40       * @return the array of objects representing the values
41       */
42      Object[] get()
43      {
44          return this.values;
45      }
46  
47      /**
48       * Get a single value identified by its index (0 based) as provided in the
49       * constructor.
50       * 
51       * @param <T>
52       * @param index
53       *            the index of the value to retrieve, 0 based, in the order
54       *            provided in the constructor
55       * @return the value at the index or <code>null</code> if the index is out
56       *         of bounds
57       */
58      @SuppressWarnings("unchecked")
59      <T> T get(int index)
60      {
61          if (index < 0 || index > this.values.length)
62          {
63              return null;
64          }
65          return (T) this.values[index];
66      }
67  
68      @Override
69      public String toString()
70      {
71          return Arrays.deepToString(this.values);
72      }
73  
74      @Override
75      public int hashCode()
76      {
77          final int prime = 31;
78          int result = 1;
79          result = prime * result + Arrays.hashCode(values);
80          return result;
81      }
82  
83      @Override
84      public boolean equals(Object obj)
85      {
86          if (is.same(this, obj))
87          {
88              return true;
89          }
90          if (is.differentClass(this, obj))
91          {
92              return false;
93          }
94          final Values other = (Values) obj;
95          return is.deepEq(get(), other.get());
96      }
97  }