1 /******************************************************************************* 2 * Copyright (c) 2011 Google, Inc. 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * Google, Inc. - initial API and implementation 10 *******************************************************************************/ 11 package org.eclipse.wb.internal.core.utils; 12 13 import com.google.common.base.Objects; 14 15 /** 16 * Pair of two objects. 17 * 18 * @author scheglov_ke 19 * @coverage core.util 20 */ 21 public final class Pair<L, R> { 22 private final L left; 23 private final R right; 24 25 //////////////////////////////////////////////////////////////////////////// 26 // 27 // Constructor 28 // 29 //////////////////////////////////////////////////////////////////////////// Pair(L left, R right)30 public Pair(L left, R right) { 31 this.left = left; 32 this.right = right; 33 } 34 35 //////////////////////////////////////////////////////////////////////////// 36 // 37 // Object 38 // 39 //////////////////////////////////////////////////////////////////////////// 40 @Override equals(Object o)41 public boolean equals(Object o) { 42 if (o == this) { 43 return true; 44 } 45 if (!(o instanceof Pair<?, ?>)) { 46 return false; 47 } 48 Pair<?, ?> other = (Pair<?, ?>) o; 49 return Objects.equal(getLeft(), other.getLeft()) 50 && Objects.equal(getRight(), other.getRight()); 51 } 52 53 @Override hashCode()54 public int hashCode() { 55 int hLeft = getLeft() == null ? 0 : getLeft().hashCode(); 56 int hRight = getRight() == null ? 0 : getRight().hashCode(); 57 return hLeft + 37 * hRight; 58 } 59 60 //////////////////////////////////////////////////////////////////////////// 61 // 62 // Access 63 // 64 //////////////////////////////////////////////////////////////////////////// getLeft()65 public L getLeft() { 66 return left; 67 } 68 getRight()69 public R getRight() { 70 return right; 71 } 72 73 //////////////////////////////////////////////////////////////////////////// 74 // 75 // Factory 76 // 77 //////////////////////////////////////////////////////////////////////////// create(L left, R right)78 public static <L, R> Pair<L, R> create(L left, R right) { 79 return new Pair<L, R>(left, right); 80 } 81 }