001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.lang3.builder;
018
019import java.util.Collections;
020import java.util.Iterator;
021import java.util.List;
022import java.util.Objects;
023
024import org.apache.commons.lang3.StringUtils;
025
026/**
027 * A {@link DiffResult} contains a collection of the differences between two
028 * {@link Diffable} objects. Typically these differences are displayed using
029 * {@link #toString()} method, which returns a string describing the fields that
030 * differ between the objects.
031 *
032 * <p>
033 * Use a {@link DiffBuilder} to build a {@link DiffResult} comparing two objects.
034 * </p>
035 * @param <T> type of the left and right object.
036 *
037 * @since 3.3
038 */
039public class DiffResult<T> implements Iterable<Diff<?>> {
040
041    /**
042     * The {@link String} returned when the objects have no differences:
043     * {@value}
044     */
045    public static final String OBJECTS_SAME_STRING = StringUtils.EMPTY;
046
047    private final List<Diff<?>> diffList;
048    private final T lhs;
049    private final T rhs;
050    private final ToStringStyle style;
051    private final String toStringFormat;
052
053    /**
054     * Creates a {@link DiffResult} containing the differences between two
055     * objects.
056     *
057     * @param lhs
058     *            the left-hand side object
059     * @param rhs
060     *            the right-hand side object
061     * @param diffList
062     *            the list of differences, may be empty
063     * @param style
064     *            the style to use for the {@link #toString()} method. May be
065     *            {@code null}, in which case
066     *            {@link ToStringStyle#DEFAULT_STYLE} is used
067     * @param toStringFormat
068     *            Two-argument format string for {@link String#format(String, Object...)}, for example {@code "%s differs from %s"}.
069     * @throws NullPointerException if {@code lhs}, {@code rhs} or {@code diffs} are {@code null}.
070     */
071    DiffResult(final T lhs, final T rhs, final List<Diff<?>> diffList, final ToStringStyle style, final String toStringFormat) {
072        this.diffList = Objects.requireNonNull(diffList, "diffList");
073        this.lhs = Objects.requireNonNull(lhs, "lhs");
074        this.rhs = Objects.requireNonNull(rhs, "rhs");
075        this.style = Objects.requireNonNull(style, "style");
076        this.toStringFormat = Objects.requireNonNull(toStringFormat, "toStringFormat");
077    }
078
079    /**
080     * Returns an unmodifiable list of {@link Diff}s. The list may be empty if
081     * there were no differences between the objects.
082     *
083     * @return an unmodifiable list of {@link Diff}s
084     */
085    public List<Diff<?>> getDiffs() {
086        return Collections.unmodifiableList(diffList);
087    }
088
089    /**
090     * Returns the object the right object has been compared to.
091     *
092     * @return the left object of the diff
093     * @since 3.10
094     */
095    public T getLeft() {
096        return this.lhs;
097    }
098
099    /**
100     * Returns the number of differences between the two objects.
101     *
102     * @return the number of differences
103     */
104    public int getNumberOfDiffs() {
105        return diffList.size();
106    }
107
108    /**
109     * Returns the object the left object has been compared to.
110     *
111     * @return the right object of the diff
112     * @since 3.10
113     */
114    public T getRight() {
115        return this.rhs;
116    }
117
118    /**
119     * Returns the style used by the {@link #toString()} method.
120     *
121     * @return the style
122     */
123    public ToStringStyle getToStringStyle() {
124        return style;
125    }
126
127    /**
128     * Returns an iterator over the {@link Diff} objects contained in this list.
129     *
130     * @return the iterator
131     */
132    @Override
133    public Iterator<Diff<?>> iterator() {
134        return diffList.iterator();
135    }
136
137    /**
138     * Builds a {@link String} description of the differences contained within
139     * this {@link DiffResult}. A {@link ToStringBuilder} is used for each object
140     * and the style of the output is governed by the {@link ToStringStyle}
141     * passed to the constructor.
142     *
143     * <p>
144     * If there are no differences stored in this list, the method will return
145     * {@link #OBJECTS_SAME_STRING}. Otherwise, using the example given in
146     * {@link Diffable} and {@link ToStringStyle#SHORT_PREFIX_STYLE}, an output
147     * might be:
148     * </p>
149     *
150     * <pre>
151     * Person[name=John Doe,age=32] differs from Person[name=Joe Bloggs,age=26]
152     * </pre>
153     *
154     * <p>
155     * This indicates that the objects differ in name and age, but not in
156     * smoking status.
157     * </p>
158     *
159     * <p>
160     * To use a different {@link ToStringStyle} for an instance of this class,
161     * use {@link #toString(ToStringStyle)}.
162     * </p>
163     *
164     * @return a {@link String} description of the differences.
165     */
166    @Override
167    public String toString() {
168        return toString(style);
169    }
170
171    /**
172     * Builds a {@link String} description of the differences contained within
173     * this {@link DiffResult}, using the supplied {@link ToStringStyle}.
174     *
175     * @param style
176     *            the {@link ToStringStyle} to use when outputting the objects
177     *
178     * @return a {@link String} description of the differences.
179     */
180    public String toString(final ToStringStyle style) {
181        if (diffList.isEmpty()) {
182            return OBJECTS_SAME_STRING;
183        }
184
185        final ToStringBuilder lhsBuilder = new ToStringBuilder(lhs, style);
186        final ToStringBuilder rhsBuilder = new ToStringBuilder(rhs, style);
187
188        diffList.forEach(diff -> {
189            lhsBuilder.append(diff.getFieldName(), diff.getLeft());
190            rhsBuilder.append(diff.getFieldName(), diff.getRight());
191        });
192
193        return String.format(toStringFormat, lhsBuilder.build(), rhsBuilder.build());
194    }
195}