Skip to content

Commit

Permalink
Add a default implementation for unambiguousToStringOf in Representat…
Browse files Browse the repository at this point in the history
…ion.
  • Loading branch information
joel-costigliola committed Jan 2, 2022
1 parent 35bed61 commit 83c6e07
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/main/java/org/assertj/core/presentation/Representation.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,18 @@ public interface Representation {
String toStringOf(Object object);

/**
* Returns the {@code String} representation of the given object with its type and hexadecimal identity hash code so that
* it can be differentiated from other objects with the same {@link #toStringOf(Object)} representation.
* Override this method to return a {@code String} representation of the given object that is unambigous so that it can
* be differentiated from other objects with the same {@link #toStringOf(Object)} representation.
* <p>
* The default implementation calls {@link #toStringOf(Object)} but the {@link StandardRepresentation} adds
* the object hexadecimal identity hash code.
*
* @param object the object to represent.
* @return the unambiguous {@code toString} representation of the given object.
*/
String unambiguousToStringOf(Object object);
default String unambiguousToStringOf(Object object) {
return toStringOf(object);
}

/**
* In case multiple representations are loaded through {@link ServiceLoader} and they can represent the same types the one with the highest priority is selected.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,13 @@ private static boolean hasOverriddenToStringInSubclassOf(Class<?> objectClass, C
return false;
}

/**
* Returns the {@code String} representation of the given object with its type and hexadecimal identity hash code so that
* it can be differentiated from other objects with the same {@link #toStringOf(Object)} representation.
*
* @param object the object to represent.
* @return the unambiguous {@code toString} representation of the given object.
*/
@Override
public String unambiguousToStringOf(Object obj) {
// some types have already an unambiguous toString, no need to double down
Expand Down

0 comments on commit 83c6e07

Please sign in to comment.