Author: ggregory Date: Mon Apr 11 18:07:54 2011 New Revision: 1091156 URL: http://svn.apache.org/viewvc?rev=1091156&view=rev Log: Add Pair.toString(String)
Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Pair.java commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/PairTest.java Modified: 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=1091156&r1=1091155&r2=1091156&view=diff ============================================================================== --- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Pair.java (original) +++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Pair.java Mon Apr 11 18:07:54 2011 @@ -17,6 +17,7 @@ package org.apache.commons.lang3; import java.io.Serializable; +import java.util.Formatter; import java.util.Map; import org.apache.commons.lang3.builder.CompareToBuilder; @@ -163,4 +164,12 @@ public abstract class Pair<L, R> impleme return builder.toString(); } + /** + * Returns a String representation in the given format. + * @param format a {@link Formatter} String. + * @return a string for this object + */ + public String toString(String format) { + return String.format(format, getClass().getSimpleName(), getLeftElement(), getRightElement()); + } } Modified: 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=1091156&r1=1091155&r2=1091156&view=diff ============================================================================== --- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/PairTest.java (original) +++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/PairTest.java Mon Apr 11 18:07:54 2011 @@ -96,4 +96,15 @@ public class PairTest { assertEquals("(Key,Value)", pair.toString()); } + @Test + public void testToStringFormat() throws Exception { + Pair<String, String> pair = Pair.of("Key", "Value"); + assertEquals("ImmutablePair", pair.toString("%1$s")); + assertEquals("Key", pair.toString("%2$s")); + assertEquals("Value", pair.toString("%3$s")); + assertEquals("Key: Value", pair.toString("%2$s: %3$s")); + pair = Pair.of(null, null); + assertEquals("null: null", pair.toString("%2$s: %3$s")); + } + }