mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
Add Data.dataToString() method, already mentioned in tutorial
This commit is contained in:
@@ -34,6 +34,8 @@ public abstract class AbstractData<T> implements Data<T> {
|
||||
/**
|
||||
* Computes value's hash code by applying a hash function to {@code Data}'s <i>bytes</i>
|
||||
* representation.
|
||||
*
|
||||
* @implNote delegates to {@link #dataHashCode()}.
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
@@ -42,6 +44,8 @@ public abstract class AbstractData<T> implements Data<T> {
|
||||
|
||||
/**
|
||||
* Compares {@code Data}s' <i>bytes</i> representations.
|
||||
*
|
||||
* @implNote delegates to {@link #dataEquals(Object)}.
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
@@ -52,23 +56,11 @@ public abstract class AbstractData<T> implements Data<T> {
|
||||
* Delegates to {@code Data}'s <i>object</i> {@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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user