org.bonitasoft.engine.commons.Pair Maven / Gradle / Ivy
/**
* Copyright (C) 2019 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301, USA.
**/
package org.bonitasoft.engine.commons;
import java.io.Serializable;
import java.util.Map;
/**
* @author Baptiste Mesta
*/
public class Pair implements Serializable, Map.Entry {
private static final long serialVersionUID = 1L;
private L left;
private R right;
public Pair(final L left, final R right) {
this.left = left;
this.right = right;
}
public static Pair of(final L left, final R right) {
return new Pair(left, right);
}
public static Pair pair(final L left, final R right) {
return of(left, right);
}
public L getLeft() {
return left;
}
public R getRight() {
return right;
}
@Override
public final L getKey() {
return getLeft();
}
@Override
public R getValue() {
return getRight();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (left == null ? 0 : left.hashCode());
result = prime * result + (right == null ? 0 : right.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
@SuppressWarnings("unchecked")
final Pair other = (Pair) obj;
if (left == null) {
if (other.left != null) {
return false;
}
} else if (!left.equals(other.left)) {
return false;
}
if (right == null) {
if (other.right != null) {
return false;
}
} else if (!right.equals(other.right)) {
return false;
}
return true;
}
@Override
public String toString() {
return "(" + getLeft() + "," + getRight() + ")";
}
@Override
public R setValue(final R right) {
final R oldRight = this.right;
this.right = right;
return oldRight;
}
public L setKey(final L left) {
final L oldLeft = this.left;
this.left = left;
return oldLeft;
}
}