001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.lang3;
018
019 import java.io.Serializable;
020
021 import org.apache.commons.lang3.builder.HashCodeBuilder;
022
023 /**
024 * A basic immutable Object pair.
025 *
026 * <p>#ThreadSafe# if the objects are threadsafe</p>
027 * @since Lang 3.0
028 * @author Matt Benson
029 * @version $Id: Pair.java 967237 2010-07-23 20:08:57Z mbenson $
030 */
031 public final class Pair<L, R> implements Serializable {
032 /** Serialization version */
033 private static final long serialVersionUID = 4954918890077093841L;
034
035 /** Left object */
036 public final L left;
037
038 /** Right object */
039 public final R right;
040
041 /**
042 * Create a new Pair instance.
043 * @param left
044 * @param right
045 */
046 public Pair(L left, R right) {
047 this.left = left;
048 this.right = right;
049 }
050
051 /**
052 * {@inheritDoc}
053 */
054 @Override
055 public boolean equals(Object obj) {
056 if (obj == this) {
057 return true;
058 }
059 if (obj instanceof Pair<?, ?> == false) {
060 return false;
061 }
062 Pair<?, ?> other = (Pair<?, ?>) obj;
063 return ObjectUtils.equals(left, other.left) && ObjectUtils.equals(right, other.right);
064 }
065
066 /**
067 * {@inheritDoc}
068 */
069 @Override
070 public int hashCode() {
071 return new HashCodeBuilder().append(left).append(right).toHashCode();
072 }
073
074 /**
075 * Returns a String representation of the Pair in the form: (L,R)
076 */
077 @Override
078 public String toString() {
079 StringBuilder builder = new StringBuilder();
080 builder.append("(");
081 builder.append(left);
082 builder.append(",");
083 builder.append(right);
084 builder.append(")");
085 return builder.toString();
086 }
087
088 /**
089 * Static creation method for a Pair<L, R>.
090 * @param <L>
091 * @param <R>
092 * @param left
093 * @param right
094 * @return Pair<L, R>(left, right)
095 */
096 public static <L, R> Pair<L, R> of(L left, R right) {
097 return new Pair<L, R>(left, right);
098 }
099 }