MATSIM
Tuple.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * Tuple.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.utils.collections;
22 
23 import java.io.Serializable;
24 
36 public final class Tuple<A, B> implements Serializable {
37  private static final long serialVersionUID = 1L;
38 
39  public static <A, B> Tuple<A, B> of(final A first, final B second) {
40  return new Tuple<>(first, second);
41  }
42 
46  private final A first;
50  private final B second;
56  public Tuple(final A first, final B second) {
57  this.first = first;
58  this.second = second;
59  }
60 
61  public A getFirst() {
62  return this.first;
63  }
64 
65  public B getSecond() {
66  return this.second;
67  }
68 
72  @Override
73  public boolean equals(final Object other) {
74  if (!(other instanceof Tuple)) return false;
75  Tuple o = (Tuple) other;
76  if (this.first != null && this.second != null && o.first != null && o.second != null) {
77  return (this.first.equals(o.first) && this.second.equals(o.second));
78  }
79  boolean firstEquals = (this.first == null) && (o.first == null);
80  boolean secondEquals = (this.second == null) && (o.second == null);
81  if (!firstEquals && this.first != null && o.first != null) {
82  firstEquals = this.first.equals(o.first);
83  }
84  if (!secondEquals && this.second != null && o.second != null) {
85  secondEquals = this.second.equals(o.second);
86  }
87  return firstEquals && secondEquals;
88  }
89 
93  @Override
94  public int hashCode() {
95  return (this.first == null ? 0 : this.first.hashCode()) +
96  (this.second == null ? 0 : this.second.hashCode());
97  }
98 
102  @Override
103  public String toString() {
104  StringBuilder buffer = new StringBuilder(50);
105  buffer.append("[Tuple: [First: " );
106  buffer.append(this.first.toString());
107  buffer.append("], [Second: ");
108  buffer.append(this.second.toString());
109  buffer.append("]]");
110  return buffer.toString();
111  }
112 
113 }
boolean equals(final Object other)
Definition: Tuple.java:73
static final long serialVersionUID
Definition: Tuple.java:37
static< A, B > Tuple< A, B > of(final A first, final B second)
Definition: Tuple.java:39
Tuple(final A first, final B second)
Definition: Tuple.java:56