Add back Pair#toString(String format).

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1096506 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2011-04-25 16:00:32 +00:00
parent af9cbd35d5
commit 28feb02b2e
2 changed files with 21 additions and 0 deletions

View File

@ -180,4 +180,15 @@ public abstract class Pair<L, R> implements Map.Entry<L, R>, Comparable<Pair<L,
formatter, flags, width, precision);
}
/**
* Formats the receiver using the given string.
*
* @param format
* The format string where <code>%1$</code> is the key (left) and <code>%2$</code> is the value (right).
* @return The formatted string
*/
public Object toString(String format) {
return String.format(format, getLeft(), getRight());
}
}

View File

@ -21,6 +21,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
@ -29,6 +30,7 @@ import org.junit.Test;
/**
* Test the Pair class.
*
* @version $Id$
*/
public class PairTest {
@ -96,4 +98,12 @@ public class PairTest {
assertEquals("(Key,Value)", pair.toString());
}
@Test
public void testToStringCustom() throws Exception {
Calendar date = Calendar.getInstance();
date.set(2011, Calendar.APRIL, 25);
Pair<String, Calendar> pair = Pair.of("DOB", date);
assertEquals("Test created on " + "04-25-2011", pair.toString("Test created on %2$tm-%2$td-%2$tY"));
}
}