Pair implements java.util.Formattable and defers toString() handling thereto.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1095836 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
2fb208b8d8
commit
9d2f0b9a18
|
@ -17,11 +17,13 @@
|
|||
package org.apache.commons.lang3.tuple;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Formattable;
|
||||
import java.util.Formatter;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.lang3.builder.CompareToBuilder;
|
||||
import org.apache.commons.lang3.util.FormattableUtils;
|
||||
|
||||
/**
|
||||
* <p>A pair consisting of two elements.</p>
|
||||
|
@ -40,14 +42,15 @@
|
|||
* @since Lang 3.0
|
||||
* @version $Id$
|
||||
*/
|
||||
public abstract class Pair<L, R> implements Map.Entry<L, R>, Comparable<Pair<L, R>>, Serializable {
|
||||
public abstract class Pair<L, R> implements Map.Entry<L, R>, Comparable<Pair<L, R>>, Formattable, Serializable {
|
||||
|
||||
/** Serialization version */
|
||||
private static final long serialVersionUID = 4954918890077093841L;
|
||||
|
||||
/**
|
||||
* The default format for the toString method.
|
||||
* Basic format pattern.
|
||||
*/
|
||||
private static final String DEFAULT_FORMAT_STRING = "(%2$s,%3$s)";
|
||||
private static final String DEFAULT_FORMAT_STRING = "(%1$s,%2$s)";
|
||||
|
||||
/**
|
||||
* <p>Obtains an immutable pair of from two objects inferring the generic types.</p>
|
||||
|
@ -154,31 +157,27 @@ public int hashCode() {
|
|||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a String representation of the Pair in the form: (L,R).</p>
|
||||
* <p>Returns a String representation of this Pair as completed by
|
||||
* {@link #formatTo(Formatter, int, int, int)}.</p>
|
||||
*
|
||||
* @return a string describing this object, not null
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return toString(DEFAULT_FORMAT_STRING);
|
||||
return FormattableUtils.toString(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a String representation in the given format.</p>
|
||||
* <p>Format this {@link Pair}. Basic format is in the form: (L,R).</p>
|
||||
*
|
||||
* <p>The format specified uses the syntax from {@link Formatter}.
|
||||
* There are three arguments available:</p>
|
||||
* <ol>
|
||||
* <li>The simple class name</li>
|
||||
* <li>The left object</li>
|
||||
* <li>The right object</li>
|
||||
* </ol>
|
||||
*
|
||||
* @param format the format suitable for use with {@code Formatter}, not null
|
||||
* @return a string describing for this object, not null
|
||||
* @param formatter target
|
||||
* @param flags for output format
|
||||
* @param width of output
|
||||
* @param precision of output
|
||||
*/
|
||||
public String toString(String format) {
|
||||
return String.format(format, getClass().getSimpleName(), getLeft(), getRight());
|
||||
public void formatTo(Formatter formatter, int flags, int width, int precision) {
|
||||
FormattableUtils.append(String.format(DEFAULT_FORMAT_STRING, getLeft(), getRight()),
|
||||
formatter, flags, width, precision);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -96,15 +96,4 @@ public void testToString() throws Exception {
|
|||
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