From 88d44051a14d67e9cc27f2033682cbe5ed51f8b2 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Thu, 25 Feb 2010 03:04:19 +0000 Subject: [PATCH] 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 --- .../java/org/apache/commons/lang3/Pair.java | 94 +++++++++++++++++++ .../org/apache/commons/lang3/PairTest.java | 77 +++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 src/main/java/org/apache/commons/lang3/Pair.java create mode 100644 src/test/java/org/apache/commons/lang3/PairTest.java diff --git a/src/main/java/org/apache/commons/lang3/Pair.java b/src/main/java/org/apache/commons/lang3/Pair.java new file mode 100644 index 000000000..88492ac73 --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/Pair.java @@ -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 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. + * @param + * @param + * @param left + * @param right + * @return Pair(left, right) + */ + public static Pair of(L left, R right) { + return new Pair(left, right); + } +} diff --git a/src/test/java/org/apache/commons/lang3/PairTest.java b/src/test/java/org/apache/commons/lang3/PairTest.java new file mode 100644 index 000000000..41e079742 --- /dev/null +++ b/src/test/java/org/apache/commons/lang3/PairTest.java @@ -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 pair = new Pair(0, "foo"); + assertEquals(0, pair.left.intValue()); + assertEquals("foo", pair.right); + Pair pair2 = new Pair(null, "bar"); + assertNull(pair2.left); + assertEquals("bar", pair2.right); + } + + @Test + public void testPairOf() throws Exception { + Pair pair = Pair.of(0, "foo"); + assertEquals(0, pair.left.intValue()); + assertEquals("foo", pair.right); + Pair 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 origPair = Pair.of(0, "foo"); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream out = new ObjectOutputStream(baos); + out.writeObject(origPair); + Pair deserializedPair = (Pair) new ObjectInputStream( + new ByteArrayInputStream(baos.toByteArray())).readObject(); + assertEquals(origPair, deserializedPair); + assertEquals(origPair.hashCode(), deserializedPair.hashCode()); + } +}