Add Pair.toString(String)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1091156 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
37fa5d6fa4
commit
29066c6e89
|
@ -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> implements Map.Entry<L, R>, Comparable<Pair<L,
|
|||
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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue