diff --git a/src/main/java/net/openhft/chronicle/hash/AbstractData.java b/src/main/java/net/openhft/chronicle/hash/AbstractData.java index 56335c995..de2504d90 100644 --- a/src/main/java/net/openhft/chronicle/hash/AbstractData.java +++ b/src/main/java/net/openhft/chronicle/hash/AbstractData.java @@ -34,6 +34,8 @@ public abstract class AbstractData implements Data { /** * Computes value's hash code by applying a hash function to {@code Data}'s bytes * representation. + * + * @implNote delegates to {@link #dataHashCode()}. */ @Override public int hashCode() { @@ -42,6 +44,8 @@ public abstract class AbstractData implements Data { /** * Compares {@code Data}s' bytes representations. + * + * @implNote delegates to {@link #dataEquals(Object)}. */ @Override public boolean equals(Object obj) { @@ -52,23 +56,11 @@ public abstract class AbstractData implements Data { * Delegates to {@code Data}'s object {@code toString()}. If deserialization fails with * exception (e. g. if data bytes are corrupted, and represent not a valid serialized form of * an object), traces the data's bytes and the exception. + * + * @implNote delegates to {@link #dataToString()}. */ @Override public String toString() { - T object; - try { - object = get(); - } catch (Exception e) { - StringBuilder sb = new StringBuilder(); - sb.append("failed to deserialize object from data with bytes: ["); - RandomDataInput bs = bytes(); - for (long off = offset(), lim = offset() + size(); off < lim; off++) { - sb.append(bs.printable(off)); - } - sb.append("], exception: "); - sb.append(e); - return sb.toString(); - } - return object.toString(); + return dataToString(); } } diff --git a/src/main/java/net/openhft/chronicle/hash/Data.java b/src/main/java/net/openhft/chronicle/hash/Data.java index 32dfe5d94..bc7c5de2b 100644 --- a/src/main/java/net/openhft/chronicle/hash/Data.java +++ b/src/main/java/net/openhft/chronicle/hash/Data.java @@ -134,7 +134,7 @@ public interface Data { /** * {@code Data} implementations should override {@link Object#hashCode()} with delegation to - * this method. Computes value's hash code by applying a hash function to {@code Data}'s + * this method. Computes {@code Data}'s hash code by applying a hash function to {@code Data}'s * bytes representation. */ default int dataHashCode() { @@ -150,4 +150,28 @@ public interface Data { obj instanceof Data && Data.bytesEquivalent(this, (Data) obj); } + + /** + * {@code Data} implementations should override {@link Object#toString()} with delegation to + * this method. Delegates to {@code Data}'s object {@code toString()}, i. e. equivalent + * to calling {@code get().toString()} on this {@code Data} instance with some fallback, if + * {@code get()} method throws an exception. + */ + default String dataToString() { + T object; + try { + object = get(); + } catch (Exception e) { + StringBuilder sb = new StringBuilder(); + sb.append("failed to deserialize object from data with bytes: ["); + RandomDataInput bs = bytes(); + for (long off = offset(), lim = offset() + size(); off < lim; off++) { + sb.append(bs.printable(off)); + } + sb.append("], exception: "); + sb.append(e); + return sb.toString(); + } + return object.toString(); + } }