commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r1095836 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/tuple/Pair.java test/java/org/apache/commons/lang3/tuple/PairTest.java
Date Thu, 21 Apr 2011 22:25:00 GMT
Author: mbenson
Date: Thu Apr 21 22:25:00 2011
New Revision: 1095836

URL: http://svn.apache.org/viewvc?rev=1095836&view=rev
Log:
Pair implements java.util.Formattable and defers toString() handling thereto.

Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/Pair.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/PairTest.java

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/Pair.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/Pair.java?rev=1095836&r1=1095835&r2=1095836&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/Pair.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/tuple/Pair.java Thu Apr
21 22:25:00 2011
@@ -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 @@ import org.apache.commons.lang3.builder.
  * @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 abstract class Pair<L, R> impleme
     }
 
     /**
-     * <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>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>
+     * <p>Format this {@link Pair}.  Basic format is in the form: (L,R).</p>
      * 
-     * @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);
     }
 
 }

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/PairTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/PairTest.java?rev=1095836&r1=1095835&r2=1095836&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/PairTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/tuple/PairTest.java Thu
Apr 21 22:25:00 2011
@@ -96,15 +96,4 @@ 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"));
-    }
-
 }



Mime
View raw message