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.  *      http://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.  *
  34.  * @since 3.3
  35.  */
  36. public class DiffResult<T> implements Iterable<Diff<?>> {

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

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

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

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

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

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

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

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

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

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

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

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

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

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