Add Data.dataToString() method, already mentioned in tutorial

This commit is contained in:
leventov
2016-03-19 18:04:22 +07:00
parent 0ec0eed5eb
commit 9530ec6795
2 changed files with 32 additions and 16 deletions
@@ -134,7 +134,7 @@ public interface Data<T> {
/**
* {@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
* <i>bytes</i> representation.
*/
default int dataHashCode() {
@@ -150,4 +150,28 @@ public interface Data<T> {
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 <i>object</i> {@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();
}
}