Author: bayard
Date: Thu Feb 25 03:04:19 2010
New Revision: 916098
URL: http://svn.apache.org/viewvc?rev=916098&view=rev
Log:
Applying Matt's patch with my modifications from LANG-588. Adds a Pair class to
Lang.
Added:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Pair.java
(with props)
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/PairTest.java
(with props)
Added:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Pair.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Pair.java?rev=916098&view=auto
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Pair.java
(added)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Pair.java
Thu Feb 25 03:04:19 2010
@@ -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);
+ }
+}
Propchange:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Pair.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/PairTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/PairTest.java?rev=916098&view=auto
==============================================================================
---
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/PairTest.java
(added)
+++
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/PairTest.java
Thu Feb 25 03:04:19 2010
@@ -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());
+ }
+}
Propchange:
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/PairTest.java
------------------------------------------------------------------------------
svn:eol-style = native