DiffResult.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.lang3.builder;

  18. import java.util.Collections;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.Objects;

  22. import org.apache.commons.lang3.StringUtils;

  23. /**
  24.  * A {@link DiffResult} contains a collection of the differences between two
  25.  * {@link Diffable} objects. Typically these differences are displayed using
  26.  * {@link #toString()} method, which returns a string describing the fields that
  27.  * differ between the objects.
  28.  *
  29.  * <p>
  30.  * Use a {@link DiffBuilder} to build a {@link DiffResult} comparing two objects.
  31.  * </p>
  32.  * @param <T> type of the left and right object.
  33.  * @since 3.3
  34.  */
  35. public class DiffResult<T> implements Iterable<Diff<?>> {

  36.     /**
  37.      * The {@link String} returned when the objects have no differences:
  38.      * {@value}
  39.      */
  40.     public static final String OBJECTS_SAME_STRING = StringUtils.EMPTY;

  41.     private final List<Diff<?>> diffList;
  42.     private final T lhs;
  43.     private final T rhs;
  44.     private final ToStringStyle style;
  45.     private final String toStringFormat;

  46.     /**
  47.      * Creates a {@link DiffResult} containing the differences between two
  48.      * objects.
  49.      *
  50.      * @param lhs
  51.      *            the left-hand side object
  52.      * @param rhs
  53.      *            the right-hand side object
  54.      * @param diffList
  55.      *            the list of differences, may be empty
  56.      * @param style
  57.      *            the style to use for the {@link #toString()} method. May be
  58.      *            {@code null}, in which case
  59.      *            {@link ToStringStyle#DEFAULT_STYLE} is used
  60.      * @param toStringFormat
  61.      *            Two-argument format string for {@link String#format(String, Object...)}, for example {@code "%s differs from %s"}.
  62.      * @throws NullPointerException if {@code lhs}, {@code rhs} or {@code diffs} are {@code null}.
  63.      */
  64.     DiffResult(final T lhs, final T rhs, final List<Diff<?>> diffList, final ToStringStyle style, final String toStringFormat) {
  65.         this.diffList = Objects.requireNonNull(diffList, "diffList");
  66.         this.lhs = Objects.requireNonNull(lhs, "lhs");
  67.         this.rhs = Objects.requireNonNull(rhs, "rhs");
  68.         this.style = Objects.requireNonNull(style, "style");
  69.         this.toStringFormat = Objects.requireNonNull(toStringFormat, "toStringFormat");
  70.     }

  71.     /**
  72.      * Gets an unmodifiable list of {@link Diff}s. The list may be empty if
  73.      * there were no differences between the objects.
  74.      *
  75.      * @return an unmodifiable list of {@link Diff}s
  76.      */
  77.     public List<Diff<?>> getDiffs() {
  78.         return Collections.unmodifiableList(diffList);
  79.     }

  80.     /**
  81.      * Gets the object the right object has been compared to.
  82.      *
  83.      * @return the left object of the diff
  84.      * @since 3.10
  85.      */
  86.     public T getLeft() {
  87.         return this.lhs;
  88.     }

  89.     /**
  90.      * Gets the number of differences between the two objects.
  91.      *
  92.      * @return the number of differences
  93.      */
  94.     public int getNumberOfDiffs() {
  95.         return diffList.size();
  96.     }

  97.     /**
  98.      * Gets the object the left object has been compared to.
  99.      *
  100.      * @return the right object of the diff
  101.      * @since 3.10
  102.      */
  103.     public T getRight() {
  104.         return this.rhs;
  105.     }

  106.     /**
  107.      * Gets the style used by the {@link #toString()} method.
  108.      *
  109.      * @return the style
  110.      */
  111.     public ToStringStyle getToStringStyle() {
  112.         return style;
  113.     }

  114.     /**
  115.      * Returns an iterator over the {@link Diff} objects contained in this list.
  116.      *
  117.      * @return the iterator
  118.      */
  119.     @Override
  120.     public Iterator<Diff<?>> iterator() {
  121.         return diffList.iterator();
  122.     }

  123.     /**
  124.      * Builds a {@link String} description of the differences contained within
  125.      * this {@link DiffResult}. A {@link ToStringBuilder} is used for each object
  126.      * and the style of the output is governed by the {@link ToStringStyle}
  127.      * passed to the constructor.
  128.      *
  129.      * <p>
  130.      * If there are no differences stored in this list, the method will return
  131.      * {@link #OBJECTS_SAME_STRING}. Otherwise, using the example given in
  132.      * {@link Diffable} and {@link ToStringStyle#SHORT_PREFIX_STYLE}, an output
  133.      * might be:
  134.      * </p>
  135.      *
  136.      * <pre>
  137.      * Person[name=John Doe,age=32] differs from Person[name=Joe Bloggs,age=26]
  138.      * </pre>
  139.      *
  140.      * <p>
  141.      * This indicates that the objects differ in name and age, but not in
  142.      * smoking status.
  143.      * </p>
  144.      *
  145.      * <p>
  146.      * To use a different {@link ToStringStyle} for an instance of this class,
  147.      * use {@link #toString(ToStringStyle)}.
  148.      * </p>
  149.      *
  150.      * @return a {@link String} description of the differences.
  151.      */
  152.     @Override
  153.     public String toString() {
  154.         return toString(style);
  155.     }

  156.     /**
  157.      * Builds a {@link String} description of the differences contained within
  158.      * this {@link DiffResult}, using the supplied {@link ToStringStyle}.
  159.      *
  160.      * @param style
  161.      *            the {@link ToStringStyle} to use when outputting the objects
  162.      *
  163.      * @return a {@link String} description of the differences.
  164.      */
  165.     public String toString(final ToStringStyle style) {
  166.         if (diffList.isEmpty()) {
  167.             return OBJECTS_SAME_STRING;
  168.         }

  169.         final ToStringBuilder lhsBuilder = new ToStringBuilder(lhs, style);
  170.         final ToStringBuilder rhsBuilder = new ToStringBuilder(rhs, style);

  171.         diffList.forEach(diff -> {
  172.             lhsBuilder.append(diff.getFieldName(), diff.getLeft());
  173.             rhsBuilder.append(diff.getFieldName(), diff.getRight());
  174.         });

  175.         return String.format(toStringFormat, lhsBuilder.build(), rhsBuilder.build());
  176.     }
  177. }