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;
022
023import org.apache.commons.lang3.Validate;
024
025/**
026 * <p>
027 * A {@code 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 * </p>
032 * <p>
033 * Use a {@link DiffBuilder} to build a {@code DiffResult} comparing two objects.
034 * </p>
035 *
036 * @since 3.3
037 */
038public class DiffResult implements Iterable<Diff<?>> {
039
040    /**
041     * <p>
042     * The {@code String} returned when the objects have no differences:
043     * {@value}
044     * </p>
045     */
046    public static final String OBJECTS_SAME_STRING = "";
047
048    private static final String DIFFERS_STRING = "differs from";
049
050    private final List<Diff<?>> diffs;
051    private final Object lhs;
052    private final Object rhs;
053    private final ToStringStyle style;
054
055    /**
056     * <p>
057     * Creates a {@link DiffResult} containing the differences between two
058     * objects.
059     * </p>
060     *
061     * @param lhs
062     *            the left hand object
063     * @param rhs
064     *            the right hand object
065     * @param diffs
066     *            the list of differences, may be empty
067     * @param style
068     *            the style to use for the {@link #toString()} method. May be
069     *            {@code null}, in which case
070     *            {@link ToStringStyle#DEFAULT_STYLE} is used
071     * @throws IllegalArgumentException
072     *             if {@code lhs}, {@code rhs} or {@code diffs} is {@code null}
073     */
074    DiffResult(final Object lhs, final Object rhs, final List<Diff<?>> diffs,
075            final ToStringStyle style) {
076        Validate.isTrue(lhs != null, "Left hand object cannot be null");
077        Validate.isTrue(rhs != null, "Right hand object cannot be null");
078        Validate.isTrue(diffs != null, "List of differences cannot be null");
079
080        this.diffs = diffs;
081        this.lhs = lhs;
082        this.rhs = rhs;
083
084        if (style == null) {
085            this.style = ToStringStyle.DEFAULT_STYLE;
086        } else {
087            this.style = style;
088        }
089    }
090
091    /**
092     * <p>
093     * Returns an unmodifiable list of {@code Diff}s. The list may be empty if
094     * there were no differences between the objects.
095     * </p>
096     *
097     * @return an unmodifiable list of {@code Diff}s
098     */
099    public List<Diff<?>> getDiffs() {
100        return Collections.unmodifiableList(diffs);
101    }
102
103    /**
104     * <p>
105     * Returns the number of differences between the two objects.
106     * </p>
107     *
108     * @return the number of differences
109     */
110    public int getNumberOfDiffs() {
111        return diffs.size();
112    }
113
114    /**
115     * <p>
116     * Returns the style used by the {@link #toString()} method.
117     * </p>
118     *
119     * @return the style
120     */
121    public ToStringStyle getToStringStyle() {
122        return style;
123    }
124
125    /**
126     * <p>
127     * Builds a {@code String} description of the differences contained within
128     * this {@code DiffResult}. A {@link ToStringBuilder} is used for each object
129     * and the style of the output is governed by the {@code ToStringStyle}
130     * passed to the constructor.
131     * </p>
132     *
133     * <p>
134     * If there are no differences stored in this list, the method will return
135     * {@link #OBJECTS_SAME_STRING}. Otherwise, using the example given in
136     * {@link Diffable} and {@link ToStringStyle#SHORT_PREFIX_STYLE}, an output
137     * might be:
138     * </p>
139     *
140     * <pre>
141     * Person[name=John Doe,age=32] differs from Person[name=Joe Bloggs,age=26]
142     * </pre>
143     *
144     * <p>
145     * This indicates that the objects differ in name and age, but not in
146     * smoking status.
147     * </p>
148     *
149     * <p>
150     * To use a different {@code ToStringStyle} for an instance of this class,
151     * use {@link #toString(ToStringStyle)}.
152     * </p>
153     *
154     * @return a {@code String} description of the differences.
155     */
156    @Override
157    public String toString() {
158        return toString(style);
159    }
160
161    /**
162     * <p>
163     * Builds a {@code String} description of the differences contained within
164     * this {@code DiffResult}, using the supplied {@code ToStringStyle}.
165     * </p>
166     *
167     * @param style
168     *            the {@code ToStringStyle} to use when outputting the objects
169     *
170     * @return a {@code String} description of the differences.
171     */
172    public String toString(final ToStringStyle style) {
173        if (diffs.isEmpty()) {
174            return OBJECTS_SAME_STRING;
175        }
176
177        final ToStringBuilder lhsBuilder = new ToStringBuilder(lhs, style);
178        final ToStringBuilder rhsBuilder = new ToStringBuilder(rhs, style);
179
180        for (final Diff<?> diff : diffs) {
181            lhsBuilder.append(diff.getFieldName(), diff.getLeft());
182            rhsBuilder.append(diff.getFieldName(), diff.getRight());
183        }
184
185        return String.format("%s %s %s", lhsBuilder.build(), DIFFERS_STRING,
186                rhsBuilder.build());
187    }
188
189    /**
190     * <p>
191     * Returns an iterator over the {@code Diff} objects contained in this list.
192     * </p>
193     *
194     * @return the iterator
195     */
196    @Override
197    public Iterator<Diff<?>> iterator() {
198        return diffs.iterator();
199    }
200}