View Javadoc

1   /*
2    * Copyright [2006] [University Corporation for Advanced Internet Development, Inc.]
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  
17  package org.opensaml.xml.util;
18  
19  /**
20   * Container for a pair of objects.
21   * 
22   * @param <T1> type of the first object in the pair
23   * @param <T2> type of the second object in the pair
24   */
25  public class Pair<T1, T2> {
26  
27      /** First object in pair. */
28      private T1 first;
29  
30      /** Second object in pair. */
31      private T2 second;
32  
33      /**
34       * Constructor.
35       * 
36       * @param newFirst first object in the pair
37       * @param newSecond second object in the pair
38       */
39      public Pair(T1 newFirst, T2 newSecond) {
40          first = newFirst;
41          second = newSecond;
42      }
43  
44      /**
45       * Gets the first object in the pair.
46       * 
47       * @return first object in the pair
48       */
49      public T1 getFirst() {
50          return first;
51      }
52  
53      /**
54       * Sets the first object in the pair.
55       * 
56       * @param newFirst first object in the pair
57       */
58      public void setFirst(T1 newFirst) {
59          first = newFirst;
60      }
61  
62      /**
63       * Gets the second object in the pair.
64       * 
65       * @return second object in the pair
66       */
67      public T2 getSecond() {
68          return second;
69      }
70  
71      /**
72       * Sets the second object in the pair.
73       * 
74       * @param newSecond second object in the pair
75       */
76      public void setSecond(T2 newSecond) {
77          second = newSecond;
78      }
79  
80      /** {@inheritDoc} */
81      @SuppressWarnings("unchecked")
82      public boolean equals(Object o) {
83          if(o == this){
84              return true;
85          }
86          
87          if (o instanceof Pair) {
88              Pair<T1, T2> otherPair = (Pair<T1, T2>) o;
89              return DatatypeHelper.safeEquals(getFirst(), otherPair.getFirst())
90                      && DatatypeHelper.safeEquals(getSecond(), otherPair.getSecond());
91          }
92  
93          return false;
94      }
95  
96      /** {@inheritDoc} */
97      public int hashCode() {
98          int result = 17;
99          if (first != null) {
100             result = 37 * result + first.hashCode();
101         }
102         if (second != null) {
103             result = 37 * result + second.hashCode();
104         }
105         return result;
106     }
107 
108     /** {@inheritDoc} */
109     public String toString() {
110         return "(" + getFirst() + "," + getSecond() + ")";
111     }
112 }