Applying Matt's patch with my modifications from LANG-588. Adds a Pair class to Lang.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@916098 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2010-02-25 03:04:19 +00:00
parent e0ddfa767c
commit 88d44051a1
2 changed files with 171 additions and 0 deletions

View File

@ -0,0 +1,94 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang3;
import java.io.IOException;
import java.io.Serializable;
import org.apache.commons.lang3.builder.HashCodeBuilder;
/**
* A basic immutable Object pair.
* @since Lang 3.0
* @author Matt Benson
*/
public final class Pair<L, R> implements Serializable {
/** Serialization version */
private static final long serialVersionUID = 4954918890077093841L;
/** Left object */
public final L left;
/** Right object */
public final R right;
/**
* Create a new Pair instance.
* @param left
* @param right
*/
public Pair(L left, R right) {
this.left = left;
this.right = right;
}
/**
* {@inheritDoc}
*/
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof Pair<?, ?> == false) {
return false;
}
Pair<?, ?> other = (Pair<?, ?>) obj;
return ObjectUtils.equals(left, other.left) && ObjectUtils.equals(right, other.right);
}
/**
* {@inheritDoc}
*/
public int hashCode() {
return new HashCodeBuilder().append(left).append(right).toHashCode();
}
/**
* Returns a String representation of the Pair in the form: (L,R)
*/
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("(");
builder.append(left);
builder.append(",");
builder.append(right);
builder.append(")");
return builder.toString();
}
/**
* Static creation method for a Pair<L, R>.
* @param <L>
* @param <R>
* @param left
* @param right
* @return Pair<L, R>(left, right)
*/
public static <L, R> Pair<L, R> of(L left, R right) {
return new Pair<L, R>(left, right);
}
}

View File

@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang3;
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.junit.Test;
/**
* Test the Pair class.
* @author Matt Benson
*/
public class PairTest {
@Test
public void testBasic() throws Exception {
Pair<Integer, String> pair = new Pair<Integer, String>(0, "foo");
assertEquals(0, pair.left.intValue());
assertEquals("foo", pair.right);
Pair<Object, String> pair2 = new Pair<Object, String>(null, "bar");
assertNull(pair2.left);
assertEquals("bar", pair2.right);
}
@Test
public void testPairOf() throws Exception {
Pair<Integer, String> pair = Pair.of(0, "foo");
assertEquals(0, pair.left.intValue());
assertEquals("foo", pair.right);
Pair<Object, String> pair2 = Pair.of(null, "bar");
assertNull(pair2.left);
assertEquals("bar", pair2.right);
}
@Test
public void testEquals() throws Exception {
assertEquals(Pair.of(null, "foo"), Pair.of(null, "foo"));
assertFalse(Pair.of("foo", 0).equals(Pair.of("foo", null)));
}
@Test
public void testHashCode() throws Exception {
assertEquals(Pair.of(null, "foo").hashCode(), Pair.of(null, "foo").hashCode());
}
@Test
@SuppressWarnings("unchecked")
public void testSerialization() throws Exception {
Pair<Integer, String> origPair = Pair.of(0, "foo");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(baos);
out.writeObject(origPair);
Pair<Integer, String> deserializedPair = (Pair<Integer, String>) new ObjectInputStream(
new ByteArrayInputStream(baos.toByteArray())).readObject();
assertEquals(origPair, deserializedPair);
assertEquals(origPair.hashCode(), deserializedPair.hashCode());
}
}