Skip to content

Commit

Permalink
[LANG-1486] Generify builder classes Diffable, DiffBuilder, and DiffR…
Browse files Browse the repository at this point in the history
…esult

[LANG-1486] Generify builder classes Diffable, DiffBuilder, and DiffResult #452.
  • Loading branch information
garydgregory committed Sep 6, 2019
1 parent b77014c commit 0abfa79
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 78 deletions.
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1477" type="add" dev="jochen">Added Functions.as*, and tests thereof, as suggested by Peter Verhas</action>
<action issue="LANG-1475" type="fix" dev="kinow" due-to="stzx">StringUtils.unwrap incorrect throw StringIndexOutOfBoundsException.</action>
<action issue="LANG-1485" type="add" dev="ggregory" due-to="nicolasbd">Add getters for lhs and rhs objects in DiffResult #451.</action>
<action issue="LANG-1486" type="add" dev="ggregory" due-to="Gary Gregory">Generify builder classes Diffable, DiffBuilder, and DiffResult #452.</action>
</release>

<release version="3.9" date="2019-04-09" description="New features and bug fixes. Requires Java 8, supports Java 9, 10, 11">
Expand Down
57 changes: 29 additions & 28 deletions src/main/java/org/apache/commons/lang3/builder/DiffBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,19 @@
* calling {@link DiffResult#toString(ToStringStyle)}.
* </p>
*
* @since 3.3
* @param <T> type of the left and right object.
* @see Diffable
* @see Diff
* @see DiffResult
* @see ToStringStyle
* @since 3.3
*/
public class DiffBuilder implements Builder<DiffResult> {
public class DiffBuilder<T> implements Builder<DiffResult<T>> {

private final List<Diff<?>> diffs;
private final boolean objectsTriviallyEqual;
private final Object left;
private final Object right;
private final T left;
private final T right;
private final ToStringStyle style;

/**
Expand Down Expand Up @@ -100,7 +101,7 @@ public class DiffBuilder implements Builder<DiffResult> {
* if {@code lhs} or {@code rhs} is {@code null}
* @since 3.4
*/
public DiffBuilder(final Object lhs, final Object rhs,
public DiffBuilder(final T lhs, final T rhs,
final ToStringStyle style, final boolean testTriviallyEqual) {

Validate.isTrue(lhs != null, "lhs cannot be null");
Expand Down Expand Up @@ -141,7 +142,7 @@ public DiffBuilder(final Object lhs, final Object rhs,
* @throws IllegalArgumentException
* if {@code lhs} or {@code rhs} is {@code null}
*/
public DiffBuilder(final Object lhs, final Object rhs,
public DiffBuilder(final T lhs, final T rhs,
final ToStringStyle style) {

this(lhs, rhs, style, true);
Expand All @@ -162,7 +163,7 @@ public DiffBuilder(final Object lhs, final Object rhs,
* @throws IllegalArgumentException
* if field name is {@code null}
*/
public DiffBuilder append(final String fieldName, final boolean lhs,
public DiffBuilder<T> append(final String fieldName, final boolean lhs,
final boolean rhs) {
validateFieldNameNotNull(fieldName);

Expand Down Expand Up @@ -202,7 +203,7 @@ public Boolean getRight() {
* @throws IllegalArgumentException
* if field name is {@code null}
*/
public DiffBuilder append(final String fieldName, final boolean[] lhs,
public DiffBuilder<T> append(final String fieldName, final boolean[] lhs,
final boolean[] rhs) {
validateFieldNameNotNull(fieldName);
if (objectsTriviallyEqual) {
Expand Down Expand Up @@ -241,7 +242,7 @@ public Boolean[] getRight() {
* @throws IllegalArgumentException
* if field name is {@code null}
*/
public DiffBuilder append(final String fieldName, final byte lhs,
public DiffBuilder<T> append(final String fieldName, final byte lhs,
final byte rhs) {
validateFieldNameNotNull(fieldName);
if (objectsTriviallyEqual) {
Expand Down Expand Up @@ -280,7 +281,7 @@ public Byte getRight() {
* @throws IllegalArgumentException
* if field name is {@code null}
*/
public DiffBuilder append(final String fieldName, final byte[] lhs,
public DiffBuilder<T> append(final String fieldName, final byte[] lhs,
final byte[] rhs) {
validateFieldNameNotNull(fieldName);

Expand Down Expand Up @@ -320,7 +321,7 @@ public Byte[] getRight() {
* @throws IllegalArgumentException
* if field name is {@code null}
*/
public DiffBuilder append(final String fieldName, final char lhs,
public DiffBuilder<T> append(final String fieldName, final char lhs,
final char rhs) {
validateFieldNameNotNull(fieldName);

Expand Down Expand Up @@ -360,7 +361,7 @@ public Character getRight() {
* @throws IllegalArgumentException
* if field name is {@code null}
*/
public DiffBuilder append(final String fieldName, final char[] lhs,
public DiffBuilder<T> append(final String fieldName, final char[] lhs,
final char[] rhs) {
validateFieldNameNotNull(fieldName);

Expand Down Expand Up @@ -400,7 +401,7 @@ public Character[] getRight() {
* @throws IllegalArgumentException
* if field name is {@code null}
*/
public DiffBuilder append(final String fieldName, final double lhs,
public DiffBuilder<T> append(final String fieldName, final double lhs,
final double rhs) {
validateFieldNameNotNull(fieldName);

Expand Down Expand Up @@ -440,7 +441,7 @@ public Double getRight() {
* @throws IllegalArgumentException
* if field name is {@code null}
*/
public DiffBuilder append(final String fieldName, final double[] lhs,
public DiffBuilder<T> append(final String fieldName, final double[] lhs,
final double[] rhs) {
validateFieldNameNotNull(fieldName);

Expand Down Expand Up @@ -480,7 +481,7 @@ public Double[] getRight() {
* @throws IllegalArgumentException
* if field name is {@code null}
*/
public DiffBuilder append(final String fieldName, final float lhs,
public DiffBuilder<T> append(final String fieldName, final float lhs,
final float rhs) {
validateFieldNameNotNull(fieldName);

Expand Down Expand Up @@ -520,7 +521,7 @@ public Float getRight() {
* @throws IllegalArgumentException
* if field name is {@code null}
*/
public DiffBuilder append(final String fieldName, final float[] lhs,
public DiffBuilder<T> append(final String fieldName, final float[] lhs,
final float[] rhs) {
validateFieldNameNotNull(fieldName);

Expand Down Expand Up @@ -560,7 +561,7 @@ public Float[] getRight() {
* @throws IllegalArgumentException
* if field name is {@code null}
*/
public DiffBuilder append(final String fieldName, final int lhs,
public DiffBuilder<T> append(final String fieldName, final int lhs,
final int rhs) {
validateFieldNameNotNull(fieldName);

Expand Down Expand Up @@ -600,7 +601,7 @@ public Integer getRight() {
* @throws IllegalArgumentException
* if field name is {@code null}
*/
public DiffBuilder append(final String fieldName, final int[] lhs,
public DiffBuilder<T> append(final String fieldName, final int[] lhs,
final int[] rhs) {
validateFieldNameNotNull(fieldName);

Expand Down Expand Up @@ -640,7 +641,7 @@ public Integer[] getRight() {
* @throws IllegalArgumentException
* if field name is {@code null}
*/
public DiffBuilder append(final String fieldName, final long lhs,
public DiffBuilder<T> append(final String fieldName, final long lhs,
final long rhs) {
validateFieldNameNotNull(fieldName);

Expand Down Expand Up @@ -680,7 +681,7 @@ public Long getRight() {
* @throws IllegalArgumentException
* if field name is {@code null}
*/
public DiffBuilder append(final String fieldName, final long[] lhs,
public DiffBuilder<T> append(final String fieldName, final long[] lhs,
final long[] rhs) {
validateFieldNameNotNull(fieldName);

Expand Down Expand Up @@ -720,7 +721,7 @@ public Long[] getRight() {
* @throws IllegalArgumentException
* if field name is {@code null}
*/
public DiffBuilder append(final String fieldName, final short lhs,
public DiffBuilder<T> append(final String fieldName, final short lhs,
final short rhs) {
validateFieldNameNotNull(fieldName);

Expand Down Expand Up @@ -760,7 +761,7 @@ public Short getRight() {
* @throws IllegalArgumentException
* if field name is {@code null}
*/
public DiffBuilder append(final String fieldName, final short[] lhs,
public DiffBuilder<T> append(final String fieldName, final short[] lhs,
final short[] rhs) {
validateFieldNameNotNull(fieldName);

Expand Down Expand Up @@ -800,7 +801,7 @@ public Short[] getRight() {
* @throws IllegalArgumentException
* if field name is {@code null}
*/
public DiffBuilder append(final String fieldName, final Object lhs,
public DiffBuilder<T> append(final String fieldName, final Object lhs,
final Object rhs) {
validateFieldNameNotNull(fieldName);
if (objectsTriviallyEqual) {
Expand Down Expand Up @@ -884,7 +885,7 @@ public Object getRight() {
* @throws IllegalArgumentException
* if field name is {@code null}
*/
public DiffBuilder append(final String fieldName, final Object[] lhs,
public DiffBuilder<T> append(final String fieldName, final Object[] lhs,
final Object[] rhs) {
validateFieldNameNotNull(fieldName);
if (objectsTriviallyEqual) {
Expand Down Expand Up @@ -946,8 +947,8 @@ public Object[] getRight() {
* if field name is {@code null}
* @since 3.5
*/
public DiffBuilder append(final String fieldName,
final DiffResult diffResult) {
public DiffBuilder<T> append(final String fieldName,
final DiffResult<T> diffResult) {
validateFieldNameNotNull(fieldName);
Validate.isTrue(diffResult != null, "Diff result cannot be null");
if (objectsTriviallyEqual) {
Expand All @@ -972,8 +973,8 @@ public DiffBuilder append(final String fieldName,
* objects.
*/
@Override
public DiffResult build() {
return new DiffResult(left, right, diffs, style);
public DiffResult<T> build() {
return new DiffResult<>(left, right, diffs, style);
}

private void validateFieldNameNotNull(final String fieldName) {
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/org/apache/commons/lang3/builder/DiffResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@
* <p>
* Use a {@link DiffBuilder} to build a {@code DiffResult} comparing two objects.
* </p>
* @param <T> type of the left and right object.
*
* @since 3.3
*/
public class DiffResult implements Iterable<Diff<?>> {
public class DiffResult<T> implements Iterable<Diff<?>> {

/**
* <p>
Expand All @@ -48,8 +49,8 @@ public class DiffResult implements Iterable<Diff<?>> {
private static final String DIFFERS_STRING = "differs from";

private final List<Diff<?>> diffs;
private final Object lhs;
private final Object rhs;
private final T lhs;
private final T rhs;
private final ToStringStyle style;

/**
Expand All @@ -71,7 +72,7 @@ public class DiffResult implements Iterable<Diff<?>> {
* @throws IllegalArgumentException
* if {@code lhs}, {@code rhs} or {@code diffs} is {@code null}
*/
DiffResult(final Object lhs, final Object rhs, final List<Diff<?>> diffs,
DiffResult(final T lhs, final T rhs, final List<Diff<?>> diffs,
final ToStringStyle style) {
Validate.isTrue(lhs != null, "Left hand object cannot be null");
Validate.isTrue(rhs != null, "Right hand object cannot be null");
Expand All @@ -94,7 +95,7 @@ public class DiffResult implements Iterable<Diff<?>> {
* @return the left object of the diff
* @since 3.10
*/
public Object getLeft() {
public T getLeft() {
return this.lhs;
}

Expand All @@ -104,7 +105,7 @@ public Object getLeft() {
* @return the right object of the diff
* @since 3.10
*/
public Object getRight() {
public T getRight() {
return this.rhs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ public interface Diffable<T> {
* @return a list of differences
* @throws NullPointerException if the specified object is {@code null}
*/
DiffResult diff(T obj);
DiffResult<T> diff(T obj);
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,19 @@
* {@code DiffResult.toString()} method. This style choice can be overridden by
* calling {@link DiffResult#toString(ToStringStyle)}.
* </p>
* @param <T>
* type of the left and right object to diff.
* @see Diffable
* @see Diff
* @see DiffResult
* @see ToStringStyle
* @since 3.6
*/
public class ReflectionDiffBuilder implements Builder<DiffResult> {
public class ReflectionDiffBuilder<T> implements Builder<DiffResult<T>> {

private final Object left;
private final Object right;
private final DiffBuilder diffBuilder;
private final DiffBuilder<T> diffBuilder;

/**
* <p>
Expand All @@ -81,8 +83,6 @@ public class ReflectionDiffBuilder implements Builder<DiffResult> {
* not evaluate any calls to {@code append(...)} and will return an empty
* {@link DiffResult} when {@link #build()} is executed.
* </p>
* @param <T>
* type of the objects to diff
* @param lhs
* {@code this} object
* @param rhs
Expand All @@ -93,14 +93,14 @@ public class ReflectionDiffBuilder implements Builder<DiffResult> {
* @throws IllegalArgumentException
* if {@code lhs} or {@code rhs} is {@code null}
*/
public <T> ReflectionDiffBuilder(final T lhs, final T rhs, final ToStringStyle style) {
public ReflectionDiffBuilder(final T lhs, final T rhs, final ToStringStyle style) {
this.left = lhs;
this.right = rhs;
diffBuilder = new DiffBuilder(lhs, rhs, style);
diffBuilder = new DiffBuilder<>(lhs, rhs, style);
}

@Override
public DiffResult build() {
public DiffResult<T> build() {
if (left.equals(right)) {
return diffBuilder.build();
}
Expand Down
Loading

0 comments on commit 0abfa79

Please sign in to comment.