This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git

commit b1b409ae4096664a33787bf52be32d9d2eb0cb31
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Mon Dec 11 09:26:14 2023 -0500

    [LANG-1724] Customise text pattern in DiffResult#toString()
    
    Internal refactoring toward solution
---
 .../apache/commons/lang3/builder/DiffBuilder.java  | 77 ++++++++--------------
 .../apache/commons/lang3/builder/DiffResult.java   | 30 ++++-----
 .../commons/lang3/builder/DiffResultTest.java      | 28 ++++----
 3 files changed, 53 insertions(+), 82 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/builder/DiffBuilder.java 
b/src/main/java/org/apache/commons/lang3/builder/DiffBuilder.java
index 1bacfd846..b0e6c3aa3 100644
--- a/src/main/java/org/apache/commons/lang3/builder/DiffBuilder.java
+++ b/src/main/java/org/apache/commons/lang3/builder/DiffBuilder.java
@@ -70,11 +70,14 @@ import org.apache.commons.lang3.ObjectUtils;
  */
 public class DiffBuilder<T> implements Builder<DiffResult<T>> {
 
+    static final String TO_STRING_FORMAT = "%s differs from %s";
+
     private final List<Diff<?>> diffs;
     private final boolean objectsTriviallyEqual;
     private final T left;
     private final T right;
     private final ToStringStyle style;
+    private final String toStringFormat;
 
     /**
      * Constructs a builder for the specified objects with the specified style.
@@ -100,10 +103,8 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<T>> {
      * @throws NullPointerException
      *             if {@code lhs} or {@code rhs} is {@code null}
      */
-    public DiffBuilder(final T lhs, final T rhs,
-            final ToStringStyle style) {
-
-            this(lhs, rhs, style, true);
+    public DiffBuilder(final T lhs, final T rhs, final ToStringStyle style) {
+        this(lhs, rhs, style, true);
     }
 
     /**
@@ -132,17 +133,12 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<T>> {
      *             if {@code lhs} or {@code rhs} is {@code null}
      * @since 3.4
      */
-    public DiffBuilder(final T lhs, final T rhs,
-            final ToStringStyle style, final boolean testTriviallyEqual) {
-
-        Objects.requireNonNull(lhs, "lhs");
-        Objects.requireNonNull(rhs, "rhs");
-
+    public DiffBuilder(final T lhs, final T rhs, final ToStringStyle style, 
final boolean testTriviallyEqual) {
+        this.left = Objects.requireNonNull(lhs, "lhs");
+        this.right = Objects.requireNonNull(rhs, "rhs");
         this.diffs = new ArrayList<>();
-        this.left = lhs;
-        this.right = rhs;
-        this.style = style;
-
+        this.toStringFormat = DiffBuilder.TO_STRING_FORMAT;
+        this.style = style != null ? style : ToStringStyle.DEFAULT_STYLE;
         // Don't compare any fields if objects equal
         this.objectsTriviallyEqual = testTriviallyEqual && Objects.equals(lhs, 
rhs);
     }
@@ -160,8 +156,7 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<T>> {
      * @throws NullPointerException
      *             if field name is {@code null}
      */
-    public DiffBuilder<T> append(final String fieldName, final boolean lhs,
-            final boolean rhs) {
+    public DiffBuilder<T> append(final String fieldName, final boolean lhs, 
final boolean rhs) {
         validateFieldNameNotNull(fieldName);
 
         if (objectsTriviallyEqual) {
@@ -198,8 +193,7 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<T>> {
      * @throws NullPointerException
      *             if field name is {@code null}
      */
-    public DiffBuilder<T> append(final String fieldName, final boolean[] lhs,
-            final boolean[] rhs) {
+    public DiffBuilder<T> append(final String fieldName, final boolean[] lhs, 
final boolean[] rhs) {
         validateFieldNameNotNull(fieldName);
         if (objectsTriviallyEqual) {
             return this;
@@ -235,8 +229,7 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<T>> {
      * @throws NullPointerException
      *             if field name is {@code null}
      */
-    public DiffBuilder<T> append(final String fieldName, final byte lhs,
-            final byte rhs) {
+    public DiffBuilder<T> append(final String fieldName, final byte lhs, final 
byte rhs) {
         validateFieldNameNotNull(fieldName);
         if (objectsTriviallyEqual) {
             return this;
@@ -272,8 +265,7 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<T>> {
      * @throws NullPointerException
      *             if field name is {@code null}
      */
-    public DiffBuilder<T> append(final String fieldName, final byte[] lhs,
-            final byte[] rhs) {
+    public DiffBuilder<T> append(final String fieldName, final byte[] lhs, 
final byte[] rhs) {
         validateFieldNameNotNull(fieldName);
 
         if (objectsTriviallyEqual) {
@@ -310,8 +302,7 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<T>> {
      * @throws NullPointerException
      *             if field name is {@code null}
      */
-    public DiffBuilder<T> append(final String fieldName, final char lhs,
-            final char rhs) {
+    public DiffBuilder<T> append(final String fieldName, final char lhs, final 
char rhs) {
         validateFieldNameNotNull(fieldName);
 
         if (objectsTriviallyEqual) {
@@ -348,8 +339,7 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<T>> {
      * @throws NullPointerException
      *             if field name is {@code null}
      */
-    public DiffBuilder<T> append(final String fieldName, final char[] lhs,
-            final char[] rhs) {
+    public DiffBuilder<T> append(final String fieldName, final char[] lhs, 
final char[] rhs) {
         validateFieldNameNotNull(fieldName);
 
         if (objectsTriviallyEqual) {
@@ -429,8 +419,7 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<T>> {
      * @throws NullPointerException
      *             if field name is {@code null}
      */
-    public DiffBuilder<T> append(final String fieldName, final double lhs,
-            final double rhs) {
+    public DiffBuilder<T> append(final String fieldName, final double lhs, 
final double rhs) {
         validateFieldNameNotNull(fieldName);
 
         if (objectsTriviallyEqual) {
@@ -467,8 +456,7 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<T>> {
      * @throws NullPointerException
      *             if field name is {@code null}
      */
-    public DiffBuilder<T> append(final String fieldName, final double[] lhs,
-            final double[] rhs) {
+    public DiffBuilder<T> append(final String fieldName, final double[] lhs, 
final double[] rhs) {
         validateFieldNameNotNull(fieldName);
 
         if (objectsTriviallyEqual) {
@@ -543,8 +531,7 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<T>> {
      * @throws NullPointerException
      *             if field name is {@code null}
      */
-    public DiffBuilder<T> append(final String fieldName, final float[] lhs,
-            final float[] rhs) {
+    public DiffBuilder<T> append(final String fieldName, final float[] lhs, 
final float[] rhs) {
         validateFieldNameNotNull(fieldName);
 
         if (objectsTriviallyEqual) {
@@ -581,8 +568,7 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<T>> {
      * @throws NullPointerException
      *             if field name is {@code null}
      */
-    public DiffBuilder<T> append(final String fieldName, final int lhs,
-            final int rhs) {
+    public DiffBuilder<T> append(final String fieldName, final int lhs, final 
int rhs) {
         validateFieldNameNotNull(fieldName);
 
         if (objectsTriviallyEqual) {
@@ -619,8 +605,7 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<T>> {
      * @throws NullPointerException
      *             if field name is {@code null}
      */
-    public DiffBuilder<T> append(final String fieldName, final int[] lhs,
-            final int[] rhs) {
+    public DiffBuilder<T> append(final String fieldName, final int[] lhs, 
final int[] rhs) {
         validateFieldNameNotNull(fieldName);
 
         if (objectsTriviallyEqual) {
@@ -657,8 +642,7 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<T>> {
      * @throws NullPointerException
      *             if field name is {@code null}
      */
-    public DiffBuilder<T> append(final String fieldName, final long lhs,
-            final long rhs) {
+    public DiffBuilder<T> append(final String fieldName, final long lhs, final 
long rhs) {
         validateFieldNameNotNull(fieldName);
 
         if (objectsTriviallyEqual) {
@@ -695,8 +679,7 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<T>> {
      * @throws NullPointerException
      *             if field name is {@code null}
      */
-    public DiffBuilder<T> append(final String fieldName, final long[] lhs,
-            final long[] rhs) {
+    public DiffBuilder<T> append(final String fieldName, final long[] lhs, 
final long[] rhs) {
         validateFieldNameNotNull(fieldName);
 
         if (objectsTriviallyEqual) {
@@ -733,8 +716,7 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<T>> {
      * @throws NullPointerException
      *             if field name is {@code null}
      */
-    public DiffBuilder<T> append(final String fieldName, final Object lhs,
-            final Object rhs) {
+    public DiffBuilder<T> append(final String fieldName, final Object lhs, 
final Object rhs) {
         validateFieldNameNotNull(fieldName);
         if (objectsTriviallyEqual) {
             return this;
@@ -815,8 +797,7 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<T>> {
      * @throws NullPointerException
      *             if field name is {@code null}
      */
-    public DiffBuilder<T> append(final String fieldName, final Object[] lhs,
-            final Object[] rhs) {
+    public DiffBuilder<T> append(final String fieldName, final Object[] lhs, 
final Object[] rhs) {
         validateFieldNameNotNull(fieldName);
         if (objectsTriviallyEqual) {
             return this;
@@ -854,8 +835,7 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<T>> {
      * @throws NullPointerException
      *             if field name is {@code null}
      */
-    public DiffBuilder<T> append(final String fieldName, final short lhs,
-            final short rhs) {
+    public DiffBuilder<T> append(final String fieldName, final short lhs, 
final short rhs) {
         validateFieldNameNotNull(fieldName);
 
         if (objectsTriviallyEqual) {
@@ -892,8 +872,7 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<T>> {
      * @throws NullPointerException
      *             if field name is {@code null}
      */
-    public DiffBuilder<T> append(final String fieldName, final short[] lhs,
-            final short[] rhs) {
+    public DiffBuilder<T> append(final String fieldName, final short[] lhs, 
final short[] rhs) {
         validateFieldNameNotNull(fieldName);
 
         if (objectsTriviallyEqual) {
@@ -926,7 +905,7 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<T>> {
      */
     @Override
     public DiffResult<T> build() {
-        return new DiffResult<>(left, right, diffs, style);
+        return new DiffResult<>(left, right, diffs, style, toStringFormat);
     }
 
     private void validateFieldNameNotNull(final String fieldName) {
diff --git a/src/main/java/org/apache/commons/lang3/builder/DiffResult.java 
b/src/main/java/org/apache/commons/lang3/builder/DiffResult.java
index 695783f28..28c30a6f2 100644
--- a/src/main/java/org/apache/commons/lang3/builder/DiffResult.java
+++ b/src/main/java/org/apache/commons/lang3/builder/DiffResult.java
@@ -44,12 +44,11 @@ public class DiffResult<T> implements Iterable<Diff<?>> {
      */
     public static final String OBJECTS_SAME_STRING = StringUtils.EMPTY;
 
-    private static final String DIFFERS_STRING = "differs from";
-
     private final List<Diff<?>> diffList;
     private final T lhs;
     private final T rhs;
     private final ToStringStyle style;
+    private final String toStringFormat;
 
     /**
      * Creates a {@link DiffResult} containing the differences between two
@@ -65,23 +64,16 @@ public class DiffResult<T> implements Iterable<Diff<?>> {
      *            the style to use for the {@link #toString()} method. May be
      *            {@code null}, in which case
      *            {@link ToStringStyle#DEFAULT_STYLE} is used
-     * @throws NullPointerException if {@code lhs}, {@code rhs} or {@code 
diffs} is {@code null}
+     * @param toStringFormat
+     *            Two-argument format string for {@link String#format(String, 
Object...)}, for example {@code "%s differs from %s"}.
+     * @throws NullPointerException if {@code lhs}, {@code rhs} or {@code 
diffs} are {@code null}.
      */
-    DiffResult(final T lhs, final T rhs, final List<Diff<?>> diffList,
-            final ToStringStyle style) {
-        Objects.requireNonNull(lhs, "lhs");
-        Objects.requireNonNull(rhs, "rhs");
-        Objects.requireNonNull(diffList, "diffList");
-
-        this.diffList = diffList;
-        this.lhs = lhs;
-        this.rhs = rhs;
-
-        if (style == null) {
-            this.style = ToStringStyle.DEFAULT_STYLE;
-        } else {
-            this.style = style;
-        }
+    DiffResult(final T lhs, final T rhs, final List<Diff<?>> diffList, final 
ToStringStyle style, final String toStringFormat) {
+        this.diffList = Objects.requireNonNull(diffList, "diffList");
+        this.lhs = Objects.requireNonNull(lhs, "lhs");
+        this.rhs = Objects.requireNonNull(rhs, "rhs");
+        this.style = Objects.requireNonNull(style, "style");
+        this.toStringFormat = Objects.requireNonNull(toStringFormat, 
"toStringFormat");
     }
 
     /**
@@ -198,6 +190,6 @@ public class DiffResult<T> implements Iterable<Diff<?>> {
             rhsBuilder.append(diff.getFieldName(), diff.getRight());
         });
 
-        return String.format("%s %s %s", lhsBuilder.build(), DIFFERS_STRING, 
rhsBuilder.build());
+        return String.format(toStringFormat, lhsBuilder.build(), 
rhsBuilder.build());
     }
 }
diff --git a/src/test/java/org/apache/commons/lang3/builder/DiffResultTest.java 
b/src/test/java/org/apache/commons/lang3/builder/DiffResultTest.java
index 18ccbb551..06331f85f 100644
--- a/src/test/java/org/apache/commons/lang3/builder/DiffResultTest.java
+++ b/src/test/java/org/apache/commons/lang3/builder/DiffResultTest.java
@@ -52,11 +52,18 @@ public class DiffResultTest extends AbstractLangTest {
                     .build();
         }
     }
+    private static final ToStringStyle SHORT_STYLE = 
ToStringStyle.SHORT_PREFIX_STYLE;
+
     private static final SimpleClass SIMPLE_FALSE = new SimpleClass(false);
 
     private static final SimpleClass SIMPLE_TRUE = new SimpleClass(true);
 
-    private static final ToStringStyle SHORT_STYLE = 
ToStringStyle.SHORT_PREFIX_STYLE;
+    @Test
+    public void testDefaultStyle() {
+        final DiffResult<SimpleClass> diffResult = new 
DiffResult<>(SIMPLE_TRUE, SIMPLE_FALSE, 
SIMPLE_TRUE.diff(SIMPLE_FALSE).getDiffs(),
+                ToStringStyle.DEFAULT_STYLE, DiffBuilder.TO_STRING_FORMAT);
+        assertEquals(ToStringStyle.DEFAULT_STYLE, 
diffResult.getToStringStyle());
+    }
 
     @Test
     public void testIterator() {
@@ -66,7 +73,7 @@ public class DiffResultTest extends AbstractLangTest {
         final List<Diff<?>> diffs = lhs.diff(rhs).getDiffs();
         final Iterator<Diff<?>> expectedIterator = diffs.iterator();
 
-        final DiffResult<SimpleClass> list = new DiffResult<>(lhs, rhs, diffs, 
SHORT_STYLE);
+        final DiffResult<SimpleClass> list = new DiffResult<>(lhs, rhs, diffs, 
SHORT_STYLE, DiffBuilder.TO_STRING_FORMAT);
         final Iterator<Diff<?>> iterator = list.iterator();
 
         while (iterator.hasNext()) {
@@ -81,7 +88,7 @@ public class DiffResultTest extends AbstractLangTest {
         final SimpleClass right = new SimpleClass(false);
 
         final List<Diff<?>> diffs = left.diff(right).getDiffs();
-        final DiffResult diffResult = new DiffResult(left, right, diffs, 
SHORT_STYLE);
+        final DiffResult diffResult = new DiffResult(left, right, diffs, 
SHORT_STYLE, DiffBuilder.TO_STRING_FORMAT);
 
         assertEquals(left, diffResult.getLeft());
         assertEquals(right, diffResult.getRight());
@@ -94,7 +101,7 @@ public class DiffResultTest extends AbstractLangTest {
 
         final List<Diff<?>> diffs = lhs.diff(rhs).getDiffs();
 
-        final DiffResult<SimpleClass> list = new DiffResult<>(lhs, rhs, diffs, 
SHORT_STYLE);
+        final DiffResult<SimpleClass> list = new DiffResult<>(lhs, rhs, diffs, 
SHORT_STYLE, DiffBuilder.TO_STRING_FORMAT);
         assertEquals(diffs, list.getDiffs());
         assertEquals(1, list.getNumberOfDiffs());
         assertThrows(UnsupportedOperationException.class, () -> 
list.getDiffs().remove(0));
@@ -110,26 +117,19 @@ public class DiffResultTest extends AbstractLangTest {
     @Test
     public void testNullLhs() {
         assertThrows(NullPointerException.class,
-            () -> new DiffResult<>(null, SIMPLE_FALSE, 
SIMPLE_TRUE.diff(SIMPLE_FALSE).getDiffs(), SHORT_STYLE));
+            () -> new DiffResult<>(null, SIMPLE_FALSE, 
SIMPLE_TRUE.diff(SIMPLE_FALSE).getDiffs(), SHORT_STYLE, 
DiffBuilder.TO_STRING_FORMAT));
     }
 
     @Test
     public void testNullList() {
         assertThrows(NullPointerException.class,
-            () -> new DiffResult<>(SIMPLE_TRUE, SIMPLE_FALSE, null, 
SHORT_STYLE));
+            () -> new DiffResult<>(SIMPLE_TRUE, SIMPLE_FALSE, null, 
SHORT_STYLE, null));
     }
 
     @Test
     public void testNullRhs() {
         assertThrows(NullPointerException.class,
-            () -> new DiffResult<>(SIMPLE_TRUE, null, 
SIMPLE_TRUE.diff(SIMPLE_FALSE).getDiffs(), SHORT_STYLE));
-    }
-
-    @Test
-    public void testNullStyle() {
-        final DiffResult<SimpleClass> diffResult = new 
DiffResult<>(SIMPLE_TRUE, SIMPLE_FALSE, SIMPLE_TRUE
-                .diff(SIMPLE_FALSE).getDiffs(), null);
-        assertEquals(ToStringStyle.DEFAULT_STYLE, 
diffResult.getToStringStyle());
+            () -> new DiffResult<>(SIMPLE_TRUE, null, 
SIMPLE_TRUE.diff(SIMPLE_FALSE).getDiffs(), SHORT_STYLE, 
DiffBuilder.TO_STRING_FORMAT));
     }
 
     @Test

Reply via email to