Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.util; |
Pavlin Radoslavov | 9f9d549 | 2013-12-04 18:21:48 -0800 | [diff] [blame] | 2 | |
Sho SHIMIZU | 69aa0f3 | 2014-06-09 15:05:22 -0700 | [diff] [blame] | 3 | import java.util.Objects; |
| 4 | |
Pavlin Radoslavov | 9f9d549 | 2013-12-04 18:21:48 -0800 | [diff] [blame] | 5 | /** |
| 6 | * A generic class representing a pair of two values. |
Sho SHIMIZU | 26d7789 | 2014-06-10 11:07:06 -0700 | [diff] [blame] | 7 | * |
| 8 | * If a user supplies immutable objects, the pair become immutable. |
| 9 | * Otherwise, the pair become mutable. |
| 10 | * |
| 11 | * @param <F> the type of the first value |
| 12 | * @param <S> the type type of the second value |
Pavlin Radoslavov | 9f9d549 | 2013-12-04 18:21:48 -0800 | [diff] [blame] | 13 | */ |
Sho SHIMIZU | af5004e | 2014-06-10 15:17:43 -0700 | [diff] [blame] | 14 | public final class Pair<F, S> { |
Sho SHIMIZU | 26d7789 | 2014-06-10 11:07:06 -0700 | [diff] [blame] | 15 | private final F first; // The first value in the pair |
| 16 | private final S second; // The second value in the pair |
Pavlin Radoslavov | 9f9d549 | 2013-12-04 18:21:48 -0800 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * Constructor for a pair of two values. |
| 20 | * |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 21 | * @param first the first value in the pair. |
Pavlin Radoslavov | 4535bc1 | 2013-12-05 10:43:49 -0800 | [diff] [blame] | 22 | * @param second the second value in the pair. |
Pavlin Radoslavov | 9f9d549 | 2013-12-04 18:21:48 -0800 | [diff] [blame] | 23 | */ |
Pavlin Radoslavov | 4535bc1 | 2013-12-05 10:43:49 -0800 | [diff] [blame] | 24 | public Pair(F first, S second) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 25 | this.first = first; |
| 26 | this.second = second; |
Pavlin Radoslavov | 9f9d549 | 2013-12-04 18:21:48 -0800 | [diff] [blame] | 27 | } |
Toshio Koide | d7476d0 | 2014-02-22 15:59:04 -0800 | [diff] [blame] | 28 | |
Sho SHIMIZU | 26d7789 | 2014-06-10 11:07:06 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Get the first value of the Pair. |
| 31 | * |
| 32 | * @return the first value of the Pair. |
| 33 | */ |
| 34 | public F getFirst() { |
| 35 | return first; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Get the second value of the Pair. |
| 40 | * |
| 41 | * @return the second value of the Pair. |
| 42 | */ |
| 43 | public S getSecond() { |
| 44 | return second; |
| 45 | } |
| 46 | |
Toshio Koide | d7476d0 | 2014-02-22 15:59:04 -0800 | [diff] [blame] | 47 | @Override |
| 48 | public String toString() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 49 | return String.format("<%s, %s>", first, second); |
Toshio Koide | d7476d0 | 2014-02-22 15:59:04 -0800 | [diff] [blame] | 50 | } |
Sho SHIMIZU | 69aa0f3 | 2014-06-09 15:05:22 -0700 | [diff] [blame] | 51 | |
| 52 | @Override |
| 53 | public boolean equals(Object o) { |
| 54 | if (o == this) { |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | if (!(o instanceof Pair)) { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | Pair<?, ?> that = (Pair<?, ?>) o; |
| 63 | return Objects.equals(this.first, that.first) |
| 64 | && Objects.equals(this.second, that.second); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public int hashCode() { |
| 69 | return Objects.hash(this.first, this.second); |
| 70 | } |
Pavlin Radoslavov | 9f9d549 | 2013-12-04 18:21:48 -0800 | [diff] [blame] | 71 | } |