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