View Javadoc

1   /* Copyright (c) 2008 Sascha Kohlmann
2    *
3    * This program is free software: you can redistribute it and/or modify
4    * it under the terms of the GNU Affero General Public License as published by
5    * the Free Software Foundation, either version 3 of the License, or
6    * (at your option) any later version.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Affero General Public License for more details.
12   *
13   * You should have received a copy of the GNU Affero General Public License
14   */
15  package net.sf.eos.util;
16  
17  import net.sf.eos.Nullable;
18  
19  
20  /**
21   * A class to store a pair of generic types. Use with care as keys in {@code maps}.
22   *
23   * @param <F> the type of the first value
24   * @param <S> the type of the second value
25   *
26   * @since 0.1.0
27   * @author Sascha Kohlmann
28   */
29  public final class Pair<F, S> {
30  
31      /** The first value of the pair. */
32      private final F first;
33  
34      /** The second value of the pair. */
35      private final S second;
36  
37      /**
38       * Creates a new pair.
39       * @param first the first value of this pair
40       * @param second the second value of this pair
41       *
42       */
43      public Pair(@SuppressWarnings("hiding") @Nullable final F first,
44                  @SuppressWarnings("hiding") @Nullable final S second) {
45          this.first = first;
46          this.second = second;
47      }
48  
49      /**
50       * Returns the first value of the pair.
51       * @return the first value of the pair
52       */
53      public F getFirst() {
54          return this.first;
55      }
56  
57      /**
58       * Returns the second value of the pair.
59       * @return the second value of the pair
60       */
61      public S getSecond() {
62          return this.second;
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      public int hashCode() {
68          int hash = EqualsAndHashUtil.hash(this.first);
69          return hash * EqualsAndHashUtil.hash(this.second);
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public boolean equals(@Nullable final Object other) {
75          if (this == other) {
76              return true;
77          }
78  
79          if (other == null) {
80              return false;
81          }
82  
83          if (!(this.getClass().equals(other.getClass()))) {
84              return false;
85          }
86  
87          final Pair<? super F, ? extends F> pair = (Pair<? super F, ? extends F>) other;
88          return (EqualsAndHashUtil.isEqual(this.first, pair.first)
89                  && EqualsAndHashUtil.isEqual(this.second, pair.second));
90      }
91  }