Internal refactoring
Use lambdas instead of anonymous inner classes
This commit is contained in:
parent
cb3cbd356f
commit
faa1a1eea5
|
@ -54,9 +54,12 @@ public abstract class Diff<T> extends Pair<T, T> {
|
|||
* the field name
|
||||
*/
|
||||
protected Diff(final String fieldName) {
|
||||
this.type = ObjectUtils.defaultIfNull(
|
||||
TypeUtils.getTypeArguments(getClass(), Diff.class).get(
|
||||
Diff.class.getTypeParameters()[0]), Object.class);
|
||||
this.type = ObjectUtils.defaultIfNull(TypeUtils.getTypeArguments(getClass(), Diff.class).get(Diff.class.getTypeParameters()[0]), Object.class);
|
||||
this.fieldName = fieldName;
|
||||
}
|
||||
|
||||
Diff(final String fieldName, final Type type) {
|
||||
this.type = type;
|
||||
this.fieldName = fieldName;
|
||||
}
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@ import java.util.ArrayList;
|
|||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
|
@ -70,10 +71,33 @@ import org.apache.commons.lang3.ObjectUtils;
|
|||
*/
|
||||
public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
||||
|
||||
private static final class SDiff<T> extends Diff<T> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private final transient Supplier<T> leftS;
|
||||
private final transient Supplier<T> rightS;
|
||||
|
||||
private SDiff(final String fieldName, final Supplier<T> leftS, final Supplier<T> rightS, final Class<T> type) {
|
||||
super(fieldName, type);
|
||||
this.leftS = leftS;
|
||||
this.rightS = rightS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getLeft() {
|
||||
return leftS.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getRight() {
|
||||
return rightS.get();
|
||||
}
|
||||
|
||||
}
|
||||
static final String TO_STRING_FORMAT = "%s differs from %s";
|
||||
|
||||
private final List<Diff<?>> diffs;
|
||||
private final boolean objectsEquals;
|
||||
private final boolean equals;
|
||||
private final T left;
|
||||
private final T right;
|
||||
private final ToStringStyle style;
|
||||
|
@ -140,7 +164,12 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
this.toStringFormat = DiffBuilder.TO_STRING_FORMAT;
|
||||
this.style = style != null ? style : ToStringStyle.DEFAULT_STYLE;
|
||||
// Don't compare any fields if objects equal
|
||||
this.objectsEquals = testObjectsEquals && Objects.equals(lhs, rhs);
|
||||
this.equals = testObjectsEquals && Objects.equals(lhs, rhs);
|
||||
}
|
||||
|
||||
private <F> DiffBuilder<T> add(final String fieldName, final Supplier<F> left, final Supplier<F> right, final Class<F> type) {
|
||||
diffs.add(new SDiff<>(Objects.requireNonNull(fieldName, "fieldName"), left, right, type));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -157,27 +186,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
* if field name is {@code null}
|
||||
*/
|
||||
public DiffBuilder<T> append(final String fieldName, final boolean lhs, final boolean rhs) {
|
||||
validateFieldNameNotNull(fieldName);
|
||||
|
||||
if (objectsEquals) {
|
||||
return this;
|
||||
}
|
||||
if (lhs != rhs) {
|
||||
diffs.add(new Diff<Boolean>(fieldName) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Boolean getLeft() {
|
||||
return Boolean.valueOf(lhs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean getRight() {
|
||||
return Boolean.valueOf(rhs);
|
||||
}
|
||||
});
|
||||
}
|
||||
return this;
|
||||
return equals || lhs == rhs ? this : add(fieldName, () -> Boolean.valueOf(lhs), () -> Boolean.valueOf(rhs), Boolean.class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -194,26 +203,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
* if field name is {@code null}
|
||||
*/
|
||||
public DiffBuilder<T> append(final String fieldName, final boolean[] lhs, final boolean[] rhs) {
|
||||
validateFieldNameNotNull(fieldName);
|
||||
if (objectsEquals) {
|
||||
return this;
|
||||
}
|
||||
if (!Arrays.equals(lhs, rhs)) {
|
||||
diffs.add(new Diff<Boolean[]>(fieldName) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Boolean[] getLeft() {
|
||||
return ArrayUtils.toObject(lhs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean[] getRight() {
|
||||
return ArrayUtils.toObject(rhs);
|
||||
}
|
||||
});
|
||||
}
|
||||
return this;
|
||||
return equals || Arrays.equals(lhs, rhs) ? this : add(fieldName, () -> ArrayUtils.toObject(lhs), () -> ArrayUtils.toObject(rhs), Boolean[].class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -230,26 +220,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
* if field name is {@code null}
|
||||
*/
|
||||
public DiffBuilder<T> append(final String fieldName, final byte lhs, final byte rhs) {
|
||||
validateFieldNameNotNull(fieldName);
|
||||
if (objectsEquals) {
|
||||
return this;
|
||||
}
|
||||
if (lhs != rhs) {
|
||||
diffs.add(new Diff<Byte>(fieldName) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Byte getLeft() {
|
||||
return Byte.valueOf(lhs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Byte getRight() {
|
||||
return Byte.valueOf(rhs);
|
||||
}
|
||||
});
|
||||
}
|
||||
return this;
|
||||
return equals || lhs == rhs ? this : add(fieldName, () -> Byte.valueOf(lhs), () -> Byte.valueOf(rhs), Byte.class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -266,27 +237,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
* if field name is {@code null}
|
||||
*/
|
||||
public DiffBuilder<T> append(final String fieldName, final byte[] lhs, final byte[] rhs) {
|
||||
validateFieldNameNotNull(fieldName);
|
||||
|
||||
if (objectsEquals) {
|
||||
return this;
|
||||
}
|
||||
if (!Arrays.equals(lhs, rhs)) {
|
||||
diffs.add(new Diff<Byte[]>(fieldName) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Byte[] getLeft() {
|
||||
return ArrayUtils.toObject(lhs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Byte[] getRight() {
|
||||
return ArrayUtils.toObject(rhs);
|
||||
}
|
||||
});
|
||||
}
|
||||
return this;
|
||||
return equals || Arrays.equals(lhs, rhs) ? this : add(fieldName, () -> ArrayUtils.toObject(lhs), () -> ArrayUtils.toObject(rhs), Byte[].class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -303,27 +254,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
* if field name is {@code null}
|
||||
*/
|
||||
public DiffBuilder<T> append(final String fieldName, final char lhs, final char rhs) {
|
||||
validateFieldNameNotNull(fieldName);
|
||||
|
||||
if (objectsEquals) {
|
||||
return this;
|
||||
}
|
||||
if (lhs != rhs) {
|
||||
diffs.add(new Diff<Character>(fieldName) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Character getLeft() {
|
||||
return Character.valueOf(lhs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Character getRight() {
|
||||
return Character.valueOf(rhs);
|
||||
}
|
||||
});
|
||||
}
|
||||
return this;
|
||||
return equals || lhs == rhs ? this : add(fieldName, () -> Character.valueOf(lhs), () -> Character.valueOf(rhs), Character.class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -340,34 +271,14 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
* if field name is {@code null}
|
||||
*/
|
||||
public DiffBuilder<T> append(final String fieldName, final char[] lhs, final char[] rhs) {
|
||||
validateFieldNameNotNull(fieldName);
|
||||
|
||||
if (objectsEquals) {
|
||||
return this;
|
||||
}
|
||||
if (!Arrays.equals(lhs, rhs)) {
|
||||
diffs.add(new Diff<Character[]>(fieldName) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Character[] getLeft() {
|
||||
return ArrayUtils.toObject(lhs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Character[] getRight() {
|
||||
return ArrayUtils.toObject(rhs);
|
||||
}
|
||||
});
|
||||
}
|
||||
return this;
|
||||
return equals || Arrays.equals(lhs, rhs) ? this : add(fieldName, () -> ArrayUtils.toObject(lhs), () -> ArrayUtils.toObject(rhs), Character[].class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Append diffs from another {@link DiffResult}.
|
||||
*
|
||||
* <p>
|
||||
* This method is useful if you want to compare properties which are
|
||||
* Useful this method to compare properties which are
|
||||
* themselves Diffable and would like to know which specific part of
|
||||
* it is different.
|
||||
* </p>
|
||||
|
@ -397,9 +308,8 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
* @since 3.5
|
||||
*/
|
||||
public DiffBuilder<T> append(final String fieldName, final DiffResult<T> diffResult) {
|
||||
validateFieldNameNotNull(fieldName);
|
||||
Objects.requireNonNull(diffResult, "diffResult");
|
||||
if (objectsEquals) {
|
||||
if (equals) {
|
||||
return this;
|
||||
}
|
||||
diffResult.getDiffs().forEach(diff -> append(fieldName + "." + diff.getFieldName(), diff.getLeft(), diff.getRight()));
|
||||
|
@ -420,27 +330,8 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
* if field name is {@code null}
|
||||
*/
|
||||
public DiffBuilder<T> append(final String fieldName, final double lhs, final double rhs) {
|
||||
validateFieldNameNotNull(fieldName);
|
||||
|
||||
if (objectsEquals) {
|
||||
return this;
|
||||
}
|
||||
if (Double.doubleToLongBits(lhs) != Double.doubleToLongBits(rhs)) {
|
||||
diffs.add(new Diff<Double>(fieldName) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Double getLeft() {
|
||||
return Double.valueOf(lhs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getRight() {
|
||||
return Double.valueOf(rhs);
|
||||
}
|
||||
});
|
||||
}
|
||||
return this;
|
||||
return equals || Double.doubleToLongBits(lhs) == Double.doubleToLongBits(rhs) ? this
|
||||
: add(fieldName, () -> Double.valueOf(lhs), () -> Double.valueOf(rhs), Double.class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -457,27 +348,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
* if field name is {@code null}
|
||||
*/
|
||||
public DiffBuilder<T> append(final String fieldName, final double[] lhs, final double[] rhs) {
|
||||
validateFieldNameNotNull(fieldName);
|
||||
|
||||
if (objectsEquals) {
|
||||
return this;
|
||||
}
|
||||
if (!Arrays.equals(lhs, rhs)) {
|
||||
diffs.add(new Diff<Double[]>(fieldName) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Double[] getLeft() {
|
||||
return ArrayUtils.toObject(lhs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double[] getRight() {
|
||||
return ArrayUtils.toObject(rhs);
|
||||
}
|
||||
});
|
||||
}
|
||||
return this;
|
||||
return equals || Arrays.equals(lhs, rhs) ? this : add(fieldName, () -> ArrayUtils.toObject(lhs), () -> ArrayUtils.toObject(rhs), Double[].class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -493,29 +364,9 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
* @throws NullPointerException
|
||||
* if field name is {@code null}
|
||||
*/
|
||||
public DiffBuilder<T> append(final String fieldName, final float lhs,
|
||||
final float rhs) {
|
||||
validateFieldNameNotNull(fieldName);
|
||||
|
||||
if (objectsEquals) {
|
||||
return this;
|
||||
}
|
||||
if (Float.floatToIntBits(lhs) != Float.floatToIntBits(rhs)) {
|
||||
diffs.add(new Diff<Float>(fieldName) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Float getLeft() {
|
||||
return Float.valueOf(lhs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getRight() {
|
||||
return Float.valueOf(rhs);
|
||||
}
|
||||
});
|
||||
}
|
||||
return this;
|
||||
public DiffBuilder<T> append(final String fieldName, final float lhs, final float rhs) {
|
||||
return equals || Float.floatToIntBits(lhs) == Float.floatToIntBits(rhs) ? this
|
||||
: add(fieldName, () -> Float.valueOf(lhs), () -> Float.valueOf(rhs), Float.class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -532,27 +383,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
* if field name is {@code null}
|
||||
*/
|
||||
public DiffBuilder<T> append(final String fieldName, final float[] lhs, final float[] rhs) {
|
||||
validateFieldNameNotNull(fieldName);
|
||||
|
||||
if (objectsEquals) {
|
||||
return this;
|
||||
}
|
||||
if (!Arrays.equals(lhs, rhs)) {
|
||||
diffs.add(new Diff<Float[]>(fieldName) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Float[] getLeft() {
|
||||
return ArrayUtils.toObject(lhs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float[] getRight() {
|
||||
return ArrayUtils.toObject(rhs);
|
||||
}
|
||||
});
|
||||
}
|
||||
return this;
|
||||
return equals || Arrays.equals(lhs, rhs) ? this : add(fieldName, () -> ArrayUtils.toObject(lhs), () -> ArrayUtils.toObject(rhs), Float[].class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -569,27 +400,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
* if field name is {@code null}
|
||||
*/
|
||||
public DiffBuilder<T> append(final String fieldName, final int lhs, final int rhs) {
|
||||
validateFieldNameNotNull(fieldName);
|
||||
|
||||
if (objectsEquals) {
|
||||
return this;
|
||||
}
|
||||
if (lhs != rhs) {
|
||||
diffs.add(new Diff<Integer>(fieldName) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Integer getLeft() {
|
||||
return Integer.valueOf(lhs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getRight() {
|
||||
return Integer.valueOf(rhs);
|
||||
}
|
||||
});
|
||||
}
|
||||
return this;
|
||||
return equals || lhs == rhs ? this : add(fieldName, () -> Integer.valueOf(lhs), () -> Integer.valueOf(rhs), Integer.class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -606,27 +417,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
* if field name is {@code null}
|
||||
*/
|
||||
public DiffBuilder<T> append(final String fieldName, final int[] lhs, final int[] rhs) {
|
||||
validateFieldNameNotNull(fieldName);
|
||||
|
||||
if (objectsEquals) {
|
||||
return this;
|
||||
}
|
||||
if (!Arrays.equals(lhs, rhs)) {
|
||||
diffs.add(new Diff<Integer[]>(fieldName) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Integer[] getLeft() {
|
||||
return ArrayUtils.toObject(lhs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer[] getRight() {
|
||||
return ArrayUtils.toObject(rhs);
|
||||
}
|
||||
});
|
||||
}
|
||||
return this;
|
||||
return equals || Arrays.equals(lhs, rhs) ? this : add(fieldName, () -> ArrayUtils.toObject(lhs), () -> ArrayUtils.toObject(rhs), Integer[].class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -643,27 +434,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
* if field name is {@code null}
|
||||
*/
|
||||
public DiffBuilder<T> append(final String fieldName, final long lhs, final long rhs) {
|
||||
validateFieldNameNotNull(fieldName);
|
||||
|
||||
if (objectsEquals) {
|
||||
return this;
|
||||
}
|
||||
if (lhs != rhs) {
|
||||
diffs.add(new Diff<Long>(fieldName) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Long getLeft() {
|
||||
return Long.valueOf(lhs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getRight() {
|
||||
return Long.valueOf(rhs);
|
||||
}
|
||||
});
|
||||
}
|
||||
return this;
|
||||
return equals || lhs == rhs ? this : add(fieldName, () -> Long.valueOf(lhs), () -> Long.valueOf(rhs), Long.class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -680,27 +451,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
* if field name is {@code null}
|
||||
*/
|
||||
public DiffBuilder<T> append(final String fieldName, final long[] lhs, final long[] rhs) {
|
||||
validateFieldNameNotNull(fieldName);
|
||||
|
||||
if (objectsEquals) {
|
||||
return this;
|
||||
}
|
||||
if (!Arrays.equals(lhs, rhs)) {
|
||||
diffs.add(new Diff<Long[]>(fieldName) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Long[] getLeft() {
|
||||
return ArrayUtils.toObject(lhs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long[] getRight() {
|
||||
return ArrayUtils.toObject(rhs);
|
||||
}
|
||||
});
|
||||
}
|
||||
return this;
|
||||
return equals || Arrays.equals(lhs, rhs) ? this : add(fieldName, () -> ArrayUtils.toObject(lhs), () -> ArrayUtils.toObject(rhs), Long[].class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -717,22 +468,11 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
* if field name is {@code null}
|
||||
*/
|
||||
public DiffBuilder<T> append(final String fieldName, final Object lhs, final Object rhs) {
|
||||
validateFieldNameNotNull(fieldName);
|
||||
if (objectsEquals) {
|
||||
if (equals || lhs == rhs) {
|
||||
return this;
|
||||
}
|
||||
if (lhs == rhs) {
|
||||
return this;
|
||||
}
|
||||
|
||||
final Object objectToTest;
|
||||
if (lhs != null) {
|
||||
objectToTest = lhs;
|
||||
} else {
|
||||
// rhs cannot be null, as lhs != rhs
|
||||
objectToTest = rhs;
|
||||
}
|
||||
|
||||
// rhs cannot be null, as lhs != rhs
|
||||
final Object objectToTest = lhs != null ? lhs : rhs;
|
||||
if (ObjectUtils.isArray(objectToTest)) {
|
||||
if (objectToTest instanceof boolean[]) {
|
||||
return append(fieldName, (boolean[]) lhs, (boolean[]) rhs);
|
||||
|
@ -758,29 +498,13 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
if (objectToTest instanceof short[]) {
|
||||
return append(fieldName, (short[]) lhs, (short[]) rhs);
|
||||
}
|
||||
|
||||
return append(fieldName, (Object[]) lhs, (Object[]) rhs);
|
||||
}
|
||||
|
||||
// Not array type
|
||||
if (Objects.equals(lhs, rhs)) {
|
||||
return this;
|
||||
}
|
||||
|
||||
diffs.add(new Diff<Object>(fieldName) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Object getLeft() {
|
||||
return lhs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRight() {
|
||||
return rhs;
|
||||
}
|
||||
});
|
||||
|
||||
add(fieldName, () -> lhs, () -> rhs, Object.class);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -798,28 +522,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
* if field name is {@code null}
|
||||
*/
|
||||
public DiffBuilder<T> append(final String fieldName, final Object[] lhs, final Object[] rhs) {
|
||||
validateFieldNameNotNull(fieldName);
|
||||
if (objectsEquals) {
|
||||
return this;
|
||||
}
|
||||
|
||||
if (!Arrays.equals(lhs, rhs)) {
|
||||
diffs.add(new Diff<Object[]>(fieldName) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Object[] getLeft() {
|
||||
return lhs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] getRight() {
|
||||
return rhs;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
return equals || Arrays.equals(lhs, rhs) ? this : add(fieldName, () -> lhs, () -> rhs, Object[].class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -836,27 +539,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
* if field name is {@code null}
|
||||
*/
|
||||
public DiffBuilder<T> append(final String fieldName, final short lhs, final short rhs) {
|
||||
validateFieldNameNotNull(fieldName);
|
||||
|
||||
if (objectsEquals) {
|
||||
return this;
|
||||
}
|
||||
if (lhs != rhs) {
|
||||
diffs.add(new Diff<Short>(fieldName) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Short getLeft() {
|
||||
return Short.valueOf(lhs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short getRight() {
|
||||
return Short.valueOf(rhs);
|
||||
}
|
||||
});
|
||||
}
|
||||
return this;
|
||||
return equals || lhs == rhs ? this : add(fieldName, () -> Short.valueOf(lhs), () -> Short.valueOf(rhs), Short.class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -873,27 +556,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
* if field name is {@code null}
|
||||
*/
|
||||
public DiffBuilder<T> append(final String fieldName, final short[] lhs, final short[] rhs) {
|
||||
validateFieldNameNotNull(fieldName);
|
||||
|
||||
if (objectsEquals) {
|
||||
return this;
|
||||
}
|
||||
if (!Arrays.equals(lhs, rhs)) {
|
||||
diffs.add(new Diff<Short[]>(fieldName) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Short[] getLeft() {
|
||||
return ArrayUtils.toObject(lhs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short[] getRight() {
|
||||
return ArrayUtils.toObject(rhs);
|
||||
}
|
||||
});
|
||||
}
|
||||
return this;
|
||||
return equals || Arrays.equals(lhs, rhs) ? this : add(fieldName, () -> ArrayUtils.toObject(lhs), () -> ArrayUtils.toObject(rhs), Short[].class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -908,8 +571,4 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
|
|||
return new DiffResult<>(left, right, diffs, style, toStringFormat);
|
||||
}
|
||||
|
||||
private void validateFieldNameNotNull(final String fieldName) {
|
||||
Objects.requireNonNull(fieldName, "fieldName");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -38,46 +38,36 @@ public class DiffBuilderTest extends AbstractLangTest {
|
|||
private static final class TypeTestClass implements Diffable<TypeTestClass> {
|
||||
private ToStringStyle style = SHORT_STYLE;
|
||||
private boolean booleanField = true;
|
||||
private boolean[] booleanArrayField = {true};
|
||||
private boolean[] booleanArrayField = { true };
|
||||
private byte byteField = (byte) 0xFF;
|
||||
private byte[] byteArrayField = {(byte) 0xFF};
|
||||
private byte[] byteArrayField = { (byte) 0xFF };
|
||||
private char charField = 'a';
|
||||
private char[] charArrayField = {'a'};
|
||||
private char[] charArrayField = { 'a' };
|
||||
private double doubleField = 1.0;
|
||||
private double[] doubleArrayField = {1.0};
|
||||
private double[] doubleArrayField = { 1.0 };
|
||||
private float floatField = 1.0f;
|
||||
private float[] floatArrayField = {1.0f};
|
||||
private float[] floatArrayField = { 1.0f };
|
||||
private int intField = 1;
|
||||
private int[] intArrayField = {1};
|
||||
private int[] intArrayField = { 1 };
|
||||
private long longField = 1L;
|
||||
private long[] longArrayField = {1L};
|
||||
private long[] longArrayField = { 1L };
|
||||
private short shortField = 1;
|
||||
private short[] shortArrayField = {1};
|
||||
private short[] shortArrayField = { 1 };
|
||||
private Object objectField;
|
||||
private Object[] objectArrayField = {null};
|
||||
private Object[] objectArrayField = { null };
|
||||
|
||||
@Override
|
||||
public DiffResult<TypeTestClass> diff(final TypeTestClass obj) {
|
||||
return new DiffBuilder<>(this, obj, style)
|
||||
.append("boolean", booleanField, obj.booleanField)
|
||||
.append("booleanArray", booleanArrayField, obj.booleanArrayField)
|
||||
.append("byte", byteField, obj.byteField)
|
||||
.append("byteArray", byteArrayField, obj.byteArrayField)
|
||||
.append("char", charField, obj.charField)
|
||||
.append("charArray", charArrayField, obj.charArrayField)
|
||||
.append("double", doubleField, obj.doubleField)
|
||||
.append("doubleArray", doubleArrayField, obj.doubleArrayField)
|
||||
.append("float", floatField, obj.floatField)
|
||||
.append("floatArray", floatArrayField, obj.floatArrayField)
|
||||
.append("int", intField, obj.intField)
|
||||
.append("intArray", intArrayField, obj.intArrayField)
|
||||
.append("long", longField, obj.longField)
|
||||
.append("longArray", longArrayField, obj.longArrayField)
|
||||
.append("short", shortField, obj.shortField)
|
||||
.append("shortArray", shortArrayField, obj.shortArrayField)
|
||||
.append("objectField", objectField, obj.objectField)
|
||||
.append("objectArrayField", objectArrayField, obj.objectArrayField)
|
||||
.build();
|
||||
return new DiffBuilder<>(this, obj, style).append("boolean", booleanField, obj.booleanField)
|
||||
.append("booleanArray", booleanArrayField, obj.booleanArrayField).append("byte", byteField, obj.byteField)
|
||||
.append("byteArray", byteArrayField, obj.byteArrayField).append("char", charField, obj.charField)
|
||||
.append("charArray", charArrayField, obj.charArrayField).append("double", doubleField, obj.doubleField)
|
||||
.append("doubleArray", doubleArrayField, obj.doubleArrayField).append("float", floatField, obj.floatField)
|
||||
.append("floatArray", floatArrayField, obj.floatArrayField).append("int", intField, obj.intField)
|
||||
.append("intArray", intArrayField, obj.intArrayField).append("long", longField, obj.longField)
|
||||
.append("longArray", longArrayField, obj.longArrayField).append("short", shortField, obj.shortField)
|
||||
.append("shortArray", shortArrayField, obj.shortArrayField).append("objectField", objectField, obj.objectField)
|
||||
.append("objectArrayField", objectArrayField, obj.objectArrayField).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -101,7 +91,7 @@ public class DiffBuilderTest extends AbstractLangTest {
|
|||
final DiffResult<TypeTestClass> list = class1.diff(class2);
|
||||
assertEquals(1, list.getNumberOfDiffs());
|
||||
final Diff<?> diff = list.getDiffs().get(0);
|
||||
assertEquals(Boolean.class, diff.getType());
|
||||
assertEquals(Boolean.class, diff.getType(), () -> diff.getType().getClass().toString());
|
||||
assertEquals(Boolean.TRUE, diff.getLeft());
|
||||
assertEquals(Boolean.FALSE, diff.getRight());
|
||||
}
|
||||
|
@ -110,14 +100,12 @@ public class DiffBuilderTest extends AbstractLangTest {
|
|||
public void testBooleanArray() {
|
||||
final TypeTestClass class1 = new TypeTestClass();
|
||||
final TypeTestClass class2 = new TypeTestClass();
|
||||
class2.booleanArrayField = new boolean[] {false, false};
|
||||
class2.booleanArrayField = new boolean[] { false, false };
|
||||
final DiffResult<TypeTestClass> list = class1.diff(class2);
|
||||
assertEquals(1, list.getNumberOfDiffs());
|
||||
final Diff<?> diff = list.getDiffs().get(0);
|
||||
assertArrayEquals(ArrayUtils.toObject(class1.booleanArrayField),
|
||||
(Object[]) diff.getLeft());
|
||||
assertArrayEquals(ArrayUtils.toObject(class2.booleanArrayField),
|
||||
(Object[]) diff.getRight());
|
||||
assertArrayEquals(ArrayUtils.toObject(class1.booleanArrayField), (Object[]) diff.getLeft());
|
||||
assertArrayEquals(ArrayUtils.toObject(class2.booleanArrayField), (Object[]) diff.getRight());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -128,6 +116,7 @@ public class DiffBuilderTest extends AbstractLangTest {
|
|||
final DiffResult<TypeTestClass> list = class1.diff(class2);
|
||||
assertEquals(1, list.getNumberOfDiffs());
|
||||
final Diff<?> diff = list.getDiffs().get(0);
|
||||
assertEquals(Byte.class, diff.getType(), () -> diff.getType().getClass().toString());
|
||||
assertEquals(Byte.valueOf(class1.byteField), diff.getLeft());
|
||||
assertEquals(Byte.valueOf(class2.byteField), diff.getRight());
|
||||
}
|
||||
|
@ -136,29 +125,21 @@ public class DiffBuilderTest extends AbstractLangTest {
|
|||
public void testByteArray() {
|
||||
final TypeTestClass class1 = new TypeTestClass();
|
||||
final TypeTestClass class2 = new TypeTestClass();
|
||||
class2.byteArrayField= new byte[] {0x01, 0x02};
|
||||
class2.byteArrayField = new byte[] { 0x01, 0x02 };
|
||||
final DiffResult<TypeTestClass> list = class1.diff(class2);
|
||||
assertEquals(1, list.getNumberOfDiffs());
|
||||
final Diff<?> diff = list.getDiffs().get(0);
|
||||
assertArrayEquals(ArrayUtils.toObject(class1.byteArrayField),
|
||||
(Object[]) diff.getLeft());
|
||||
assertArrayEquals(ArrayUtils.toObject(class2.byteArrayField),
|
||||
(Object[]) diff.getRight());
|
||||
assertArrayEquals(ArrayUtils.toObject(class1.byteArrayField), (Object[]) diff.getLeft());
|
||||
assertArrayEquals(ArrayUtils.toObject(class2.byteArrayField), (Object[]) diff.getRight());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteArrayEqualAsObject() {
|
||||
final DiffResult<String> list = new DiffBuilder<>("String1", "String2", SHORT_STYLE)
|
||||
.append("foo", new boolean[] {false}, new boolean[] {false})
|
||||
.append("foo", new byte[] {0x01}, new byte[] {0x01})
|
||||
.append("foo", new char[] {'a'}, new char[] {'a'})
|
||||
.append("foo", new double[] {1.0}, new double[] {1.0})
|
||||
.append("foo", new float[] {1.0F}, new float[] {1.0F})
|
||||
.append("foo", new int[] {1}, new int[] {1})
|
||||
.append("foo", new long[] {1L}, new long[] {1L})
|
||||
.append("foo", new short[] {1}, new short[] {1})
|
||||
.append("foo", new Object[] {1, "two"}, new Object[] {1, "two"})
|
||||
.build();
|
||||
final DiffResult<String> list = new DiffBuilder<>("String1", "String2", SHORT_STYLE).append("foo", new boolean[] { false }, new boolean[] { false })
|
||||
.append("foo", new byte[] { 0x01 }, new byte[] { 0x01 }).append("foo", new char[] { 'a' }, new char[] { 'a' })
|
||||
.append("foo", new double[] { 1.0 }, new double[] { 1.0 }).append("foo", new float[] { 1.0F }, new float[] { 1.0F })
|
||||
.append("foo", new int[] { 1 }, new int[] { 1 }).append("foo", new long[] { 1L }, new long[] { 1L })
|
||||
.append("foo", new short[] { 1 }, new short[] { 1 }).append("foo", new Object[] { 1, "two" }, new Object[] { 1, "two" }).build();
|
||||
|
||||
assertEquals(0, list.getNumberOfDiffs());
|
||||
}
|
||||
|
@ -179,14 +160,12 @@ public class DiffBuilderTest extends AbstractLangTest {
|
|||
public void testCharArray() {
|
||||
final TypeTestClass class1 = new TypeTestClass();
|
||||
final TypeTestClass class2 = new TypeTestClass();
|
||||
class2.charArrayField = new char[] {'f', 'o', 'o'};
|
||||
class2.charArrayField = new char[] { 'f', 'o', 'o' };
|
||||
final DiffResult<TypeTestClass> list = class1.diff(class2);
|
||||
assertEquals(1, list.getNumberOfDiffs());
|
||||
final Diff<?> diff = list.getDiffs().get(0);
|
||||
assertArrayEquals(ArrayUtils.toObject(class1.charArrayField),
|
||||
(Object[]) diff.getLeft());
|
||||
assertArrayEquals(ArrayUtils.toObject(class2.charArrayField),
|
||||
(Object[]) diff.getRight());
|
||||
assertArrayEquals(ArrayUtils.toObject(class1.charArrayField), (Object[]) diff.getLeft());
|
||||
assertArrayEquals(ArrayUtils.toObject(class2.charArrayField), (Object[]) diff.getRight());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -195,9 +174,7 @@ public class DiffBuilderTest extends AbstractLangTest {
|
|||
final TypeTestClass class2 = new TypeTestClass();
|
||||
class2.intField = 2;
|
||||
|
||||
final DiffResult<TypeTestClass> list = new DiffBuilder<>(class1, class2, SHORT_STYLE)
|
||||
.append("prop1", class1.diff(class2))
|
||||
.build();
|
||||
final DiffResult<TypeTestClass> list = new DiffBuilder<>(class1, class2, SHORT_STYLE).append("prop1", class1.diff(class2)).build();
|
||||
assertEquals(1, list.getNumberOfDiffs());
|
||||
assertEquals("prop1.int", list.getDiffs().get(0).getFieldName());
|
||||
}
|
||||
|
@ -218,14 +195,12 @@ public class DiffBuilderTest extends AbstractLangTest {
|
|||
public void testDoubleArray() {
|
||||
final TypeTestClass class1 = new TypeTestClass();
|
||||
final TypeTestClass class2 = new TypeTestClass();
|
||||
class2.doubleArrayField = new double[] {3.0, 2.9, 2.8};
|
||||
class2.doubleArrayField = new double[] { 3.0, 2.9, 2.8 };
|
||||
final DiffResult<TypeTestClass> list = class1.diff(class2);
|
||||
assertEquals(1, list.getNumberOfDiffs());
|
||||
final Diff<?> diff = list.getDiffs().get(0);
|
||||
assertArrayEquals(ArrayUtils.toObject(class1.doubleArrayField),
|
||||
(Object[]) diff.getLeft());
|
||||
assertArrayEquals(ArrayUtils.toObject(class2.doubleArrayField),
|
||||
(Object[]) diff.getRight());
|
||||
assertArrayEquals(ArrayUtils.toObject(class1.doubleArrayField), (Object[]) diff.getLeft());
|
||||
assertArrayEquals(ArrayUtils.toObject(class2.doubleArrayField), (Object[]) diff.getRight());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -244,14 +219,12 @@ public class DiffBuilderTest extends AbstractLangTest {
|
|||
public void testFloatArray() {
|
||||
final TypeTestClass class1 = new TypeTestClass();
|
||||
final TypeTestClass class2 = new TypeTestClass();
|
||||
class2.floatArrayField = new float[] {3.0F, 2.9F, 2.8F};
|
||||
class2.floatArrayField = new float[] { 3.0F, 2.9F, 2.8F };
|
||||
final DiffResult<TypeTestClass> list = class1.diff(class2);
|
||||
assertEquals(1, list.getNumberOfDiffs());
|
||||
final Diff<?> diff = list.getDiffs().get(0);
|
||||
assertArrayEquals(ArrayUtils.toObject(class1.floatArrayField),
|
||||
(Object[]) diff.getLeft());
|
||||
assertArrayEquals(ArrayUtils.toObject(class2.floatArrayField),
|
||||
(Object[]) diff.getRight());
|
||||
assertArrayEquals(ArrayUtils.toObject(class1.floatArrayField), (Object[]) diff.getLeft());
|
||||
assertArrayEquals(ArrayUtils.toObject(class2.floatArrayField), (Object[]) diff.getRight());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -270,14 +243,12 @@ public class DiffBuilderTest extends AbstractLangTest {
|
|||
public void testIntArray() {
|
||||
final TypeTestClass class1 = new TypeTestClass();
|
||||
final TypeTestClass class2 = new TypeTestClass();
|
||||
class2.intArrayField = new int[] {3, 2, 1};
|
||||
class2.intArrayField = new int[] { 3, 2, 1 };
|
||||
final DiffResult<TypeTestClass> list = class1.diff(class2);
|
||||
assertEquals(1, list.getNumberOfDiffs());
|
||||
final Diff<?> diff = list.getDiffs().get(0);
|
||||
assertArrayEquals(ArrayUtils.toObject(class1.intArrayField),
|
||||
(Object[]) diff.getLeft());
|
||||
assertArrayEquals(ArrayUtils.toObject(class2.intArrayField),
|
||||
(Object[]) diff.getRight());
|
||||
assertArrayEquals(ArrayUtils.toObject(class1.intArrayField), (Object[]) diff.getLeft());
|
||||
assertArrayEquals(ArrayUtils.toObject(class2.intArrayField), (Object[]) diff.getRight());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -296,14 +267,12 @@ public class DiffBuilderTest extends AbstractLangTest {
|
|||
public void testLongArray() {
|
||||
final TypeTestClass class1 = new TypeTestClass();
|
||||
final TypeTestClass class2 = new TypeTestClass();
|
||||
class2.longArrayField = new long[] {3L, 2L, 1L};
|
||||
class2.longArrayField = new long[] { 3L, 2L, 1L };
|
||||
final DiffResult<TypeTestClass> list = class1.diff(class2);
|
||||
assertEquals(1, list.getNumberOfDiffs());
|
||||
final Diff<?> diff = list.getDiffs().get(0);
|
||||
assertArrayEquals(ArrayUtils.toObject(class1.longArrayField),
|
||||
(Object[]) diff.getLeft());
|
||||
assertArrayEquals(ArrayUtils.toObject(class2.longArrayField),
|
||||
(Object[]) diff.getRight());
|
||||
assertArrayEquals(ArrayUtils.toObject(class1.longArrayField), (Object[]) diff.getLeft());
|
||||
assertArrayEquals(ArrayUtils.toObject(class2.longArrayField), (Object[]) diff.getRight());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -342,7 +311,7 @@ public class DiffBuilderTest extends AbstractLangTest {
|
|||
public void testObjectArray() {
|
||||
final TypeTestClass class1 = new TypeTestClass();
|
||||
final TypeTestClass class2 = new TypeTestClass();
|
||||
class2.objectArrayField = new Object[] {"string", 1, 2};
|
||||
class2.objectArrayField = new Object[] { "string", 1, 2 };
|
||||
final DiffResult<TypeTestClass> list = class1.diff(class2);
|
||||
assertEquals(1, list.getNumberOfDiffs());
|
||||
final Diff<?> diff = list.getDiffs().get(0);
|
||||
|
@ -354,8 +323,8 @@ public class DiffBuilderTest extends AbstractLangTest {
|
|||
public void testObjectArrayEqual() {
|
||||
final TypeTestClass class1 = new TypeTestClass();
|
||||
final TypeTestClass class2 = new TypeTestClass();
|
||||
class1.objectArrayField = new Object[] {"string", 1, 2};
|
||||
class2.objectArrayField = new Object[] {"string", 1, 2};
|
||||
class1.objectArrayField = new Object[] { "string", 1, 2 };
|
||||
class2.objectArrayField = new Object[] { "string", 1, 2 };
|
||||
final DiffResult<TypeTestClass> list = class1.diff(class2);
|
||||
assertEquals(0, list.getNumberOfDiffs());
|
||||
}
|
||||
|
@ -412,9 +381,7 @@ public class DiffBuilderTest extends AbstractLangTest {
|
|||
@Test
|
||||
public void testSameObjectIgnoresAppends() {
|
||||
final TypeTestClass testClass = new TypeTestClass();
|
||||
final DiffResult<TypeTestClass> list = new DiffBuilder<>(testClass, testClass, SHORT_STYLE)
|
||||
.append("ignored", false, true)
|
||||
.build();
|
||||
final DiffResult<TypeTestClass> list = new DiffBuilder<>(testClass, testClass, SHORT_STYLE).append("ignored", false, true).build();
|
||||
assertEquals(0, list.getNumberOfDiffs());
|
||||
}
|
||||
|
||||
|
@ -434,23 +401,19 @@ public class DiffBuilderTest extends AbstractLangTest {
|
|||
public void testShortArray() {
|
||||
final TypeTestClass class1 = new TypeTestClass();
|
||||
final TypeTestClass class2 = new TypeTestClass();
|
||||
class2.shortArrayField = new short[] {3, 2, 1};
|
||||
class2.shortArrayField = new short[] { 3, 2, 1 };
|
||||
final DiffResult<TypeTestClass> list = class1.diff(class2);
|
||||
assertEquals(1, list.getNumberOfDiffs());
|
||||
final Diff<?> diff = list.getDiffs().get(0);
|
||||
assertArrayEquals(ArrayUtils.toObject(class1.shortArrayField),
|
||||
(Object[]) diff.getLeft());
|
||||
assertArrayEquals(ArrayUtils.toObject(class2.shortArrayField),
|
||||
(Object[]) diff.getRight());
|
||||
assertArrayEquals(ArrayUtils.toObject(class1.shortArrayField), (Object[]) diff.getLeft());
|
||||
assertArrayEquals(ArrayUtils.toObject(class2.shortArrayField), (Object[]) diff.getRight());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimilarObjectIgnoresAppends() {
|
||||
final TypeTestClass testClass1 = new TypeTestClass();
|
||||
final TypeTestClass testClass2 = new TypeTestClass();
|
||||
final DiffResult<TypeTestClass> list = new DiffBuilder<>(testClass1, testClass2, SHORT_STYLE)
|
||||
.append("ignored", false, true)
|
||||
.build();
|
||||
final DiffResult<TypeTestClass> list = new DiffBuilder<>(testClass1, testClass2, SHORT_STYLE).append("ignored", false, true).build();
|
||||
assertEquals(0, list.getNumberOfDiffs());
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue