/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.lang3.builder; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Objects; import org.apache.commons.lang3.StringUtils; /** * A {@link DiffResult} contains a collection of the differences between two * {@link Diffable} objects. Typically these differences are displayed using * {@link #toString()} method, which returns a string describing the fields that * differ between the objects. * *
* Use a {@link DiffBuilder} to build a {@link DiffResult} comparing two objects. *
* @param* If there are no differences stored in this list, the method will return * {@link #OBJECTS_SAME_STRING}. Otherwise, using the example given in * {@link Diffable} and {@link ToStringStyle#SHORT_PREFIX_STYLE}, an output * might be: *
* *
* Person[name=John Doe,age=32] differs from Person[name=Joe Bloggs,age=26]
*
*
* * This indicates that the objects differ in name and age, but not in * smoking status. *
* ** To use a different {@link ToStringStyle} for an instance of this class, * use {@link #toString(ToStringStyle)}. *
* * @return a {@link String} description of the differences. */ @Override public String toString() { return toString(style); } /** * Builds a {@link String} description of the differences contained within * this {@link DiffResult}, using the supplied {@link ToStringStyle}. * * @param style * the {@link ToStringStyle} to use when outputting the objects * * @return a {@link String} description of the differences. */ public String toString(final ToStringStyle style) { if (diffList.isEmpty()) { return OBJECTS_SAME_STRING; } final ToStringBuilder lhsBuilder = new ToStringBuilder(lhs, style); final ToStringBuilder rhsBuilder = new ToStringBuilder(rhs, style); diffList.forEach(diff -> { lhsBuilder.append(diff.getFieldName(), diff.getLeft()); rhsBuilder.append(diff.getFieldName(), diff.getRight()); }); return String.format(toStringFormat, lhsBuilder.build(), rhsBuilder.build()); } }