View Javadoc

1   /*
2      Copyright 2008 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 static fulmine.util.Utils.nullCheck;
19  
20  /**
21   * Type safe object for holding 2 objects.
22   * 
23   * @author Ramon Servadei
24   * @param <FIRST>
25   *            the first type
26   * @param <SECOND>
27   *            the second type
28   */
29  public class DualValue<FIRST, SECOND>
30  {
31      /** Holds the values */
32      protected Values values;
33  
34      public DualValue(FIRST first, SECOND second)
35      {
36          this(new Values(first, second));
37      }
38  
39      protected DualValue(Values values)
40      {
41          nullCheck(values, "No values provided");
42          this.values = values;
43      }
44  
45      /**
46       * Get the first object from the constructor
47       * 
48       * @return the first object this was constructed with
49       */
50      @SuppressWarnings("unchecked")
51      public FIRST getFirst()
52      {
53          return (FIRST) this.values.get(0);
54      }
55  
56      /**
57       * Get the second object from the constructor
58       * 
59       * @return the second object this was constructed with
60       */
61      @SuppressWarnings("unchecked")
62      public SECOND getSecond()
63      {
64          return (SECOND) this.values.get(1);
65      }
66  
67      @Override
68      public final String toString()
69      {
70          return this.values.toString();
71      }
72  
73      @Override
74      public final int hashCode()
75      {
76          final int prime = 31;
77          int result = 1;
78          result = prime * result + ((values == null) ? 0 : values.hashCode());
79          return result;
80      }
81  
82      @SuppressWarnings("unchecked")
83      @Override
84      public final 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 DualValue other = (DualValue) obj;
95          return is.eq(values, other.values);
96      }
97  
98  }