Automatic toString() for LockBasedStorageManager

This commit is contained in:
Andrey Breslav
2014-01-27 21:40:00 +04:00
parent f95017241b
commit 707255eef6
2 changed files with 24 additions and 8 deletions
@@ -426,6 +426,13 @@ public class StorageManagerTest extends TestCase {
assertEquals(2, c.getCount());
}
// toString()
public void testToString() throws Exception {
assertTrue("Should mention the setUp() method of this class: " + m.toString(),
m.toString().contains(getClass().getSimpleName() + ".setUp("));
}
// Utilities
private static <K, V> Function0<V> apply(final Function1<K, V> f, final K x) {
@@ -31,27 +31,36 @@ import java.util.concurrent.locks.ReentrantLock;
public class LockBasedStorageManager implements StorageManager {
public static final StorageManager NO_LOCKS = new LockBasedStorageManager(NoLock.INSTANCE) {
public static final StorageManager NO_LOCKS = new LockBasedStorageManager("NO_LOCKS", NoLock.INSTANCE) {
@NotNull
@Override
protected <T> RecursionDetectedResult<T> recursionDetectedDefault() {
return RecursionDetectedResult.fallThrough();
}
@Override
public String toString() {
return "NO_LOCKS";
}
};
protected final Lock lock;
private final String debugText;
public LockBasedStorageManager() {
this(new ReentrantLock());
this(getPointOfConstruction(), new ReentrantLock());
}
private LockBasedStorageManager(@NotNull Lock lock) {
private static String getPointOfConstruction() {
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
// we need to skip frames for getStackTrace(), this method and the constructor that's calling it
if (trace.length <= 3) return "<unknown creating class>";
return trace[3].toString();
}
private LockBasedStorageManager(@NotNull String debugText, @NotNull Lock lock) {
this.lock = lock;
this.debugText = debugText;
}
@Override
public String toString() {
return getClass().getSimpleName() + "@" + Integer.toHexString(hashCode()) + " (" + debugText + ')';
}
@NotNull