View Javadoc

1   /*
2    * Copyright (C) 2013 AMIS research group, Faculty of Mathematics and Physics, Charles University in Prague, Czech Republic
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package cz.cuni.amis.utils;
18  
19  /**
20   * An object that may be used to combine two keys in a map.
21   * Added benefit over {@link NKey} is that it is generic.
22   * @author Martin Cerny
23   * @see NKey
24   */
25  public class PairKey<FIRST, SECOND> {
26      private final FIRST first;
27      private final SECOND second;
28  
29      public PairKey(FIRST first, SECOND second) {
30          this.first = first;
31          this.second = second;
32      }
33  
34      public FIRST getFirst() {
35          return first;
36      }
37  
38      public SECOND getSecond() {
39          return second;
40      }
41  
42      @Override
43      public int hashCode() {
44          int hash = 7;
45          hash = 97 * hash + (this.first != null ? this.first.hashCode() : 0);
46          hash = 97 * hash + (this.second != null ? this.second.hashCode() : 0);
47          return hash;
48      }
49  
50      @Override
51      public boolean equals(Object obj) {
52          if (obj == null) {
53              return false;
54          }
55          if (getClass() != obj.getClass()) {
56              return false;
57          }
58          final PairKey<FIRST, SECOND> other = (PairKey<FIRST, SECOND>) obj;
59          if (this.first != other.first && (this.first == null || !this.first.equals(other.first))) {
60              return false;
61          }
62          if (this.second != other.second && (this.second == null || !this.second.equals(other.second))) {
63              return false;
64          }
65          return true;
66      }
67  
68  
69      @Override
70      public String toString() {
71          return "Pair<" + first.toString() + ", " + second.toString();
72      }
73      
74      
75  }