blob: 66b60b8fc0fd9e7397189f4ed3f384fc689cf0b8 [file] [log] [blame]
Jonathan Hart23701d12014-04-03 10:45:48 -07001package net.onrc.onos.core.util;
Pavlin Radoslavov9f9d5492013-12-04 18:21:48 -08002
Sho SHIMIZU69aa0f32014-06-09 15:05:22 -07003import java.util.Objects;
4
Pavlin Radoslavov9f9d5492013-12-04 18:21:48 -08005/**
6 * A generic class representing a pair of two values.
Sho SHIMIZU26d77892014-06-10 11:07:06 -07007 *
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 Radoslavov9f9d5492013-12-04 18:21:48 -080013 */
Sho SHIMIZUaf5004e2014-06-10 15:17:43 -070014public final class Pair<F, S> {
Sho SHIMIZU26d77892014-06-10 11:07:06 -070015 private final F first; // The first value in the pair
16 private final S second; // The second value in the pair
Pavlin Radoslavov9f9d5492013-12-04 18:21:48 -080017
18 /**
19 * Constructor for a pair of two values.
20 *
Ray Milkey269ffb92014-04-03 14:43:30 -070021 * @param first the first value in the pair.
Pavlin Radoslavov4535bc12013-12-05 10:43:49 -080022 * @param second the second value in the pair.
Pavlin Radoslavov9f9d5492013-12-04 18:21:48 -080023 */
Pavlin Radoslavov4535bc12013-12-05 10:43:49 -080024 public Pair(F first, S second) {
Ray Milkey269ffb92014-04-03 14:43:30 -070025 this.first = first;
26 this.second = second;
Pavlin Radoslavov9f9d5492013-12-04 18:21:48 -080027 }
Toshio Koided7476d02014-02-22 15:59:04 -080028
Sho SHIMIZU26d77892014-06-10 11:07:06 -070029 /**
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 Koided7476d02014-02-22 15:59:04 -080047 @Override
48 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -070049 return String.format("<%s, %s>", first, second);
Toshio Koided7476d02014-02-22 15:59:04 -080050 }
Sho SHIMIZU69aa0f32014-06-09 15:05:22 -070051
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 Radoslavov9f9d5492013-12-04 18:21:48 -080071}