Author: bayard Date: Thu Nov 24 06:20:59 2011 New Revision: 1205734 URL: http://svn.apache.org/viewvc?rev=1205734&view=rev Log: Adding Henning's GitHub pull request with a Triple implementation. LANG-675
Added: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/ImmutableTriple.java (with props) commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/MutableTriple.java (with props) commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/Triple.java (with props) commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/ImmutableTripleTest.java (with props) commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/MutableTripleTest.java (with props) commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/TripleTest.java (with props) Modified: commons/proper/lang/trunk/src/site/changes/changes.xml Added: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/ImmutableTriple.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/ImmutableTriple.java?rev=1205734&view=auto ============================================================================== --- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/ImmutableTriple.java (added) +++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/ImmutableTriple.java Thu Nov 24 06:20:59 2011 @@ -0,0 +1,104 @@ +/* + * 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.tuple; + +/** + * <p>An immutable triple consisting of three {@code Object} elements.</p> + * + * <p>Although the implementation is immutable, there is no restriction on the objects + * that may be stored. If mutable objects are stored in the triple, then the triple + * itself effectively becomes mutable. The class is also not {@code final}, so a subclass + * could add undesirable behaviour.</p> + * + * <p>#ThreadSafe# if all three objects are thread-safe</p> + * + * @param <L> the left element type + * @param <M> the middle element type + * @param <R> the right element type + * + * @version $Id$ + */ +public final class ImmutableTriple<L, M, R> extends Triple<L, M, R> { + + /** Serialization version */ + private static final long serialVersionUID = 1L; + + /** Left object */ + public final L left; + /** Middle object */ + public final M middle; + /** Right object */ + public final R right; + + /** + * <p>Obtains an immutable triple of from three objects inferring the generic types.</p> + * + * <p>This factory allows the triple to be created using inference to + * obtain the generic types.</p> + * + * @param <L> the left element type + * @param <M> the middle element type + * @param <R> the right element type + * @param left the left element, may be null + * @param middle the middle element, may be null + * @param right the right element, may be null + * @return a triple formed from the three parameters, not null + */ + public static <L, M, R> ImmutableTriple<L, M, R> of(L left, M middle, R right) { + return new ImmutableTriple<L, M, R>(left, middle, right); + } + + /** + * Create a new triple instance. + * + * @param left the left value, may be null + * @param middle the middle value, may be null + * @param right the right value, may be null + */ + public ImmutableTriple(L left, M middle, R right) { + super(); + this.left = left; + this.middle = middle; + this.right = right; + } + + //----------------------------------------------------------------------- + /** + * {@inheritDoc} + */ + @Override + public L getLeft() { + return left; + } + + /** + * {@inheritDoc} + */ + @Override + public M getMiddle() { + return middle; + } + + /** + * {@inheritDoc} + */ + @Override + public R getRight() { + return right; + } +} + Propchange: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/ImmutableTriple.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/MutableTriple.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/MutableTriple.java?rev=1205734&view=auto ============================================================================== --- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/MutableTriple.java (added) +++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/MutableTriple.java Thu Nov 24 06:20:59 2011 @@ -0,0 +1,133 @@ +/* + * 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.tuple; + +/** + * <p>A mutable triple consisting of three {@code Object} elements.</p> + * + * <p>Not #ThreadSafe#</p> + * + * @param <L> the left element type + * @param <M> the middle element type + * @param <R> the right element type + * + * @version $Id$ + */ +public class MutableTriple<L, M, R> extends Triple<L, M, R> { + + /** Serialization version */ + private static final long serialVersionUID = 1L; + + /** Left object */ + public L left; + /** Middle object */ + public M middle; + /** Right object */ + public R right; + + /** + * <p>Obtains an mutable triple of three objects inferring the generic types.</p> + * + * <p>This factory allows the triple to be created using inference to + * obtain the generic types.</p> + * + * @param <L> the left element type + * @param <M> the middle element type + * @param <R> the right element type + * @param left the left element, may be null + * @param middle the middle element, may be null + * @param right the right element, may be null + * @return a triple formed from the three parameters, not null + */ + public static <L, M, R> MutableTriple<L, M, R> of(L left, M middle, R right) { + return new MutableTriple<L, M, R>(left, middle, right); + } + + /** + * Create a new triple instance of three nulls. + */ + public MutableTriple() { + super(); + } + + /** + * Create a new triple instance. + * + * @param left the left value, may be null + * @param middle the middle value, may be null + * @param right the right value, may be null + */ + public MutableTriple(L left, M middle, R right) { + super(); + this.left = left; + this.middle = middle; + this.right = right; + } + + //----------------------------------------------------------------------- + /** + * {@inheritDoc} + */ + @Override + public L getLeft() { + return left; + } + + /** + * Sets the left element of the triple. + * + * @param left the new value of the left element, may be null + */ + public void setLeft(L left) { + this.left = left; + } + + /** + * {@inheritDoc} + */ + @Override + public M getMiddle() { + return middle; + } + + /** + * Sets the middle element of the triple. + * + * @param middle the new value of the middle element, may be null + */ + public void setMiddle(M middle) { + this.middle = middle; + } + + /** + * {@inheritDoc} + */ + @Override + public R getRight() { + return right; + } + + /** + * Sets the right element of the triple. + * + * @param right the new value of the right element, may be null + */ + public void setRight(R right) { + this.right = right; + } +} + Propchange: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/MutableTriple.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/Triple.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/Triple.java?rev=1205734&view=auto ============================================================================== --- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/Triple.java (added) +++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/Triple.java Thu Nov 24 06:20:59 2011 @@ -0,0 +1,159 @@ +/* + * 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.tuple; + +import java.io.Serializable; + +import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.builder.CompareToBuilder; + +/** + * <p>A triple consisting of three elements.</p> + * + * <p>This class is an abstract implementation defining the basic API. + * It refers to the elements as 'left', 'middle' and 'right'.</p> + * + * <p>Subclass implementations may be mutable or immutable. + * However, there is no restriction on the type of the stored objects that may be stored. + * If mutable objects are stored in the triple, then the triple itself effectively becomes mutable.</p> + * + * @param <L> the left element type + * @param <M> the middle element type + * @param <R> the right element type + * + * @version $Id$ + */ +public abstract class Triple<L, M, R> implements Comparable<Triple<L, M, R>>, Serializable { + + /** Serialization version */ + private static final long serialVersionUID = 1L; + + /** + * <p>Obtains an immutable triple of from three objects inferring the generic types.</p> + * + * <p>This factory allows the triple to be created using inference to + * obtain the generic types.</p> + * + * @param <L> the left element type + * @param <M> the middle element type + * @param <R> the right element type + * @param left the left element, may be null + * @param middle the middle element, may be null + * @param right the right element, may be null + * @return a triple formed from the three parameters, not null + */ + public static <L, M, R> Triple<L, M, R> of(L left, M middle, R right) { + return new ImmutableTriple<L, M, R>(left, middle, right); + } + + //----------------------------------------------------------------------- + /** + * <p>Gets the left element from this triple.</p> + * + * @return the left element, may be null + */ + public abstract L getLeft(); + + /** + * <p>Gets the middle element from this triple.</p> + * + * @return the middle element, may be null + */ + public abstract M getMiddle(); + + /** + * <p>Gets the right element from this triple.</p> + * + * @return the right element, may be null + */ + public abstract R getRight(); + + //----------------------------------------------------------------------- + /** + * <p>Compares the triple based on the left element, followed by the middle element, + * finally the right element. + * The types must be {@code Comparable}.</p> + * + * @param other the other triple, not null + * @return negative if this is less, zero if equal, positive if greater + */ + public int compareTo(Triple<L, M, R> other) { + return new CompareToBuilder().append(getLeft(), other.getLeft()) + .append(getMiddle(), other.getMiddle()) + .append(getRight(), other.getRight()).toComparison(); + } + + /** + * <p>Compares this triple to another based on the three elements.</p> + * + * @param obj the object to compare to, null returns false + * @return true if the elements of the triple are equal + */ + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj instanceof Triple<?, ?, ?>) { + Triple<?, ?, ?> other = (Triple<?, ?, ?>) obj; + return ObjectUtils.equals(getLeft(), other.getLeft()) + && ObjectUtils.equals(getMiddle(), other.getMiddle()) + && ObjectUtils.equals(getRight(), other.getRight()); + } + return false; + } + + /** + * <p>Returns a suitable hash code.</p> + * + * @return the hash code + */ + @Override + public int hashCode() { + return (getLeft() == null ? 0 : getLeft().hashCode()) ^ + (getMiddle() == null ? 0 : getMiddle().hashCode()) ^ + (getRight() == null ? 0 : getRight().hashCode()); + } + + /** + * <p>Returns a String representation of this triple using the format {@code ($left,$middle,$right)}.</p> + * + * @return a string describing this object, not null + */ + @Override + public String toString() { + return new StringBuilder().append('(').append(getLeft()).append(',').append(getMiddle()).append(',') + .append(getRight()).append(')').toString(); + } + + /** + * <p>Formats the receiver using the given format.</p> + * + * <p>This uses {@link java.util.Formattable} to perform the formatting. Three variables may + * be used to embed the left and right elements. Use {@code %1$s} for the left + * element, {@code %2$s} for the middle and {@code %3$s} for the right element. + * The default format used by {@code toString()} is {@code (%1$s,%2$s,%3$s)}.</p> + * + * @param format the format string, optionally containing {@code %1$s}, {@code %2$s} and {@code %3$s}, not null + * @return the formatted string, not null + */ + public String toString(String format) { + return String.format(format, getLeft(), getMiddle(), getRight()); + } + +} + Propchange: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/Triple.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/proper/lang/trunk/src/site/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/site/changes/changes.xml?rev=1205734&r1=1205733&r2=1205734&view=diff ============================================================================== --- commons/proper/lang/trunk/src/site/changes/changes.xml (original) +++ commons/proper/lang/trunk/src/site/changes/changes.xml Thu Nov 24 06:20:59 2011 @@ -23,6 +23,7 @@ <release version="3.2" date="TBA" description="December release"> <action type="fix" issue="LANG-772">ClassUtils.PACKAGE_SEPARATOR javadoc contains garbage text</action> + <action type="add" issue="LANG-675">Add Triple class (ternary version of Pair)</action> </release> <release version="3.1" date="2011-11-14" description="November release"> <action type="add" issue="LANG-760">Add API StringUtils.toString(byte[] intput, String charsetName)</action> Added: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/ImmutableTripleTest.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/ImmutableTripleTest.java?rev=1205734&view=auto ============================================================================== --- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/ImmutableTripleTest.java (added) +++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/ImmutableTripleTest.java Thu Nov 24 06:20:59 2011 @@ -0,0 +1,113 @@ +/* + * 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.tuple; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +import org.junit.Test; + +/** + * Test the Triple class. + * @version $Id$ + */ +public class ImmutableTripleTest { + + @Test + public void testBasic() throws Exception { + ImmutableTriple<Integer, String, Boolean> triple = new ImmutableTriple<Integer, String, Boolean>(0, "foo", Boolean.TRUE); + assertEquals(0, triple.left.intValue()); + assertEquals(0, triple.getLeft().intValue()); + assertEquals("foo", triple.middle); + assertEquals("foo", triple.getMiddle()); + assertEquals(Boolean.TRUE, triple.right); + assertEquals(Boolean.TRUE, triple.getRight()); + ImmutableTriple<Object, String, Integer> triple2 = new ImmutableTriple<Object, String, Integer>(null, "bar", 42); + assertNull(triple2.left); + assertNull(triple2.getLeft()); + assertEquals("bar", triple2.middle); + assertEquals("bar", triple2.getMiddle()); + assertEquals(new Integer(42), triple2.right); + assertEquals(new Integer(42), triple2.getRight()); + } + + @Test + public void testTripleOf() throws Exception { + ImmutableTriple<Integer, String, Boolean> triple = ImmutableTriple.of(0, "foo", Boolean.FALSE); + assertEquals(0, triple.left.intValue()); + assertEquals(0, triple.getLeft().intValue()); + assertEquals("foo", triple.middle); + assertEquals("foo", triple.getMiddle()); + assertEquals(Boolean.FALSE, triple.right); + assertEquals(Boolean.FALSE, triple.getRight()); + ImmutableTriple<Object, String, Boolean> triple2 = ImmutableTriple.of(null, "bar", Boolean.TRUE); + assertNull(triple2.left); + assertNull(triple2.getLeft()); + assertEquals("bar", triple2.middle); + assertEquals("bar", triple2.getMiddle()); + assertEquals(Boolean.TRUE, triple2.right); + assertEquals(Boolean.TRUE, triple2.getRight()); + } + + @Test + public void testEquals() throws Exception { + assertEquals(ImmutableTriple.of(null, "foo", 42), ImmutableTriple.of(null, "foo", 42)); + assertFalse(ImmutableTriple.of("foo", 0, Boolean.TRUE).equals(ImmutableTriple.of("foo", null, null))); + assertFalse(ImmutableTriple.of("foo", "bar", "baz").equals(ImmutableTriple.of("xyz", "bar", "blo"))); + + ImmutableTriple<String, String, String> p = ImmutableTriple.of("foo", "bar", "baz"); + assertTrue(p.equals(p)); + assertFalse(p.equals(new Object())); + } + + @Test + public void testHashCode() throws Exception { + assertEquals(ImmutableTriple.of(null, "foo", Boolean.TRUE).hashCode(), ImmutableTriple.of(null, "foo", Boolean.TRUE).hashCode()); + } + + @Test + public void testToString() throws Exception { + assertEquals("(null,null,null)", ImmutableTriple.of(null, null, null).toString()); + assertEquals("(null,two,null)", ImmutableTriple.of(null, "two", null).toString()); + assertEquals("(one,null,null)", ImmutableTriple.of("one", null, null).toString()); + assertEquals("(one,two,null)", ImmutableTriple.of("one", "two", null).toString()); + assertEquals("(null,two,three)", ImmutableTriple.of(null, "two", "three").toString()); + assertEquals("(one,null,three)", ImmutableTriple.of("one", null, "three").toString()); + assertEquals("(one,two,three)", MutableTriple.of("one", "two", "three").toString()); + } + + @Test + @SuppressWarnings("unchecked") + public void testSerialization() throws Exception { + ImmutableTriple<Integer, String, Boolean> origTriple = ImmutableTriple.of(0, "foo", Boolean.TRUE); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream out = new ObjectOutputStream(baos); + out.writeObject(origTriple); + ImmutableTriple<Integer, String, Boolean> deserializedTriple = (ImmutableTriple<Integer, String, Boolean>) new ObjectInputStream( + new ByteArrayInputStream(baos.toByteArray())).readObject(); + assertEquals(origTriple, deserializedTriple); + assertEquals(origTriple.hashCode(), deserializedTriple.hashCode()); + } +} + Propchange: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/ImmutableTripleTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/MutableTripleTest.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/MutableTripleTest.java?rev=1205734&view=auto ============================================================================== --- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/MutableTripleTest.java (added) +++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/MutableTripleTest.java Thu Nov 24 06:20:59 2011 @@ -0,0 +1,121 @@ +/* + * 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.tuple; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +import org.junit.Test; + +/** + * Test the MutableTriple class. + * @version $Id$ + */ +public class MutableTripleTest { + + @Test + public void testBasic() throws Exception { + MutableTriple<Integer, String, Boolean> triple = new MutableTriple<Integer, String, Boolean>(0, "foo", Boolean.FALSE); + assertEquals(0, triple.getLeft().intValue()); + assertEquals("foo", triple.getMiddle()); + assertEquals(Boolean.FALSE, triple.getRight()); + MutableTriple<Object, String, String> triple2 = new MutableTriple<Object, String, String>(null, "bar", "hello"); + assertNull(triple2.getLeft()); + assertEquals("bar", triple2.getMiddle()); + assertEquals("hello", triple2.getRight()); + } + + @Test + public void testDefault() throws Exception { + MutableTriple<Integer, String, Boolean> triple = new MutableTriple<Integer, String, Boolean>(); + assertNull(triple.getLeft()); + assertNull(triple.getMiddle()); + assertNull(triple.getRight()); + } + + @Test + public void testMutate() throws Exception { + MutableTriple<Integer, String, Boolean> triple = new MutableTriple<Integer, String, Boolean>(0, "foo", Boolean.TRUE); + triple.setLeft(42); + triple.setMiddle("bar"); + triple.setRight(Boolean.FALSE); + assertEquals(42, triple.getLeft().intValue()); + assertEquals("bar", triple.getMiddle()); + assertFalse(triple.getRight()); + } + + @Test + public void testTripleOf() throws Exception { + MutableTriple<Integer, String, Boolean> triple = MutableTriple.of(0, "foo", Boolean.TRUE); + assertEquals(0, triple.getLeft().intValue()); + assertEquals("foo", triple.getMiddle()); + assertTrue(triple.getRight()); + MutableTriple<Object, String, String> triple2 = MutableTriple.of(null, "bar", "hello"); + assertNull(triple2.getLeft()); + assertEquals("bar", triple2.getMiddle()); + assertEquals("hello", triple2.getRight()); + } + + @Test + public void testEquals() throws Exception { + assertEquals(MutableTriple.of(null, "foo", "baz"), MutableTriple.of(null, "foo", "baz")); + assertFalse(MutableTriple.of("foo", 0, Boolean.TRUE).equals(MutableTriple.of("foo", null, Boolean.TRUE))); + assertFalse(MutableTriple.of("foo", "bar", "baz").equals(MutableTriple.of("xyz", "bar", "baz"))); + assertFalse(MutableTriple.of("foo", "bar", "baz").equals(MutableTriple.of("foo", "bar", "blo"))); + + MutableTriple<String, String, String> p = MutableTriple.of("foo", "bar", "baz"); + assertTrue(p.equals(p)); + assertFalse(p.equals(new Object())); + } + + @Test + public void testHashCode() throws Exception { + assertEquals(MutableTriple.of(null, "foo", "baz").hashCode(), MutableTriple.of(null, "foo", "baz").hashCode()); + } + + @Test + public void testToString() throws Exception { + assertEquals("(null,null,null)", MutableTriple.of(null, null, null).toString()); + assertEquals("(null,two,null)", MutableTriple.of(null, "two", null).toString()); + assertEquals("(one,null,null)", MutableTriple.of("one", null, null).toString()); + assertEquals("(one,two,null)", MutableTriple.of("one", "two", null).toString()); + assertEquals("(null,two,three)", MutableTriple.of(null, "two", "three").toString()); + assertEquals("(one,null,three)", MutableTriple.of("one", null, "three").toString()); + assertEquals("(one,two,three)", MutableTriple.of("one", "two", "three").toString()); + } + + @Test + @SuppressWarnings("unchecked") + public void testSerialization() throws Exception { + MutableTriple<Integer, String, Boolean> origTriple = MutableTriple.of(0, "foo", Boolean.TRUE); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream out = new ObjectOutputStream(baos); + out.writeObject(origTriple); + MutableTriple<Integer, String, Boolean> deserializedTriple = (MutableTriple<Integer, String, Boolean>) new ObjectInputStream( + new ByteArrayInputStream(baos.toByteArray())).readObject(); + assertEquals(origTriple, deserializedTriple); + assertEquals(origTriple.hashCode(), deserializedTriple.hashCode()); + } +} + Propchange: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/MutableTripleTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/TripleTest.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/TripleTest.java?rev=1205734&view=auto ============================================================================== --- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/TripleTest.java (added) +++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/TripleTest.java Thu Nov 24 06:20:59 2011 @@ -0,0 +1,127 @@ +/* + * 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.tuple; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.Calendar; +import java.util.HashSet; + +import org.junit.Test; + +/** + * Test the Triple class. + * + * @version $Id$ + */ +public class TripleTest { + + @Test + public void testTripleOf() throws Exception { + Triple<Integer, String, Boolean> triple = Triple.of(0, "foo", Boolean.TRUE); + assertTrue(triple instanceof ImmutableTriple<?, ?, ?>); + assertEquals(0, ((ImmutableTriple<Integer, String, Boolean>) triple).left.intValue()); + assertEquals("foo", ((ImmutableTriple<Integer, String, Boolean>) triple).middle); + assertEquals(Boolean.TRUE, ((ImmutableTriple<Integer, String, Boolean>) triple).right); + Triple<Object, String, Long> triple2 = Triple.of(null, "bar", 200L); + assertTrue(triple2 instanceof ImmutableTriple<?, ?, ?>); + assertNull(((ImmutableTriple<Object, String, Long>) triple2).left); + assertEquals("bar", ((ImmutableTriple<Object, String, Long>) triple2).middle); + assertEquals(new Long(200L), ((ImmutableTriple<Object, String, Long>) triple2).right); + } + + @Test + public void testCompatibilityBetweenTriples() throws Exception { + Triple<Integer, String, Boolean> triple = ImmutableTriple.of(0, "foo", Boolean.TRUE); + Triple<Integer, String, Boolean> triple2 = MutableTriple.of(0, "foo", Boolean.TRUE); + assertEquals(triple, triple2); + assertEquals(triple.hashCode(), triple2.hashCode()); + HashSet<Triple<Integer, String, Boolean>> set = new HashSet<Triple<Integer, String, Boolean>>(); + set.add(triple); + assertTrue(set.contains(triple2)); + } + + @Test + public void testComparable1() throws Exception { + Triple<String, String, String> triple1 = Triple.of("A", "D", "A"); + Triple<String, String, String> triple2 = Triple.of("B", "C", "A"); + assertTrue(triple1.compareTo(triple1) == 0); + assertTrue(triple1.compareTo(triple2) < 0); + assertTrue(triple2.compareTo(triple2) == 0); + assertTrue(triple2.compareTo(triple1) > 0); + } + + @Test + public void testComparable2() throws Exception { + Triple<String, String, String> triple1 = Triple.of("A", "C", "B"); + Triple<String, String, String> triple2 = Triple.of("A", "D", "B"); + assertTrue(triple1.compareTo(triple1) == 0); + assertTrue(triple1.compareTo(triple2) < 0); + assertTrue(triple2.compareTo(triple2) == 0); + assertTrue(triple2.compareTo(triple1) > 0); + } + + @Test + public void testComparable3() throws Exception { + Triple<String, String, String> triple1 = Triple.of("A", "A", "D"); + Triple<String, String, String> triple2 = Triple.of("A", "B", "C"); + assertTrue(triple1.compareTo(triple1) == 0); + assertTrue(triple1.compareTo(triple2) < 0); + assertTrue(triple2.compareTo(triple2) == 0); + assertTrue(triple2.compareTo(triple1) > 0); + } + + @Test + public void testComparable4() throws Exception { + Triple<String, String, String> triple1 = Triple.of("B", "A", "C"); + Triple<String, String, String> triple2 = Triple.of("B", "A", "D"); + assertTrue(triple1.compareTo(triple1) == 0); + assertTrue(triple1.compareTo(triple2) < 0); + assertTrue(triple2.compareTo(triple2) == 0); + assertTrue(triple2.compareTo(triple1) > 0); + } + + @Test + public void testToString() throws Exception { + Triple<String, String, String> triple = Triple.of("Key", "Something", "Value"); + assertEquals("(Key,Something,Value)", triple.toString()); + } + + @Test + public void testToStringCustom() throws Exception { + Calendar date = Calendar.getInstance(); + date.set(2011, Calendar.APRIL, 25); + Triple<String, String, Calendar> triple = Triple.of("DOB", "string", date); + assertEquals("Test created on " + "04-25-2011", triple.toString("Test created on %3$tm-%3$td-%3$tY")); + } + + @Test + public void testFormattable_simple() throws Exception { + Triple<String, String, String> triple = Triple.of("Key", "Something", "Value"); + assertEquals("(Key,Something,Value)", String.format("%1$s", triple)); + } + + @Test + public void testFormattable_padded() throws Exception { + Triple<String, String, String> triple = Triple.of("Key", "Something", "Value"); + assertEquals(" (Key,Something,Value)", String.format("%1$30s", triple)); + } + +} + Propchange: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/TripleTest.java ------------------------------------------------------------------------------ svn:eol-style = native