Skip to content

Commit bfacdb0

Browse files
committed
Implement some more toString() methods for debugging.
I added these locally while debugging, but they seem potentially generally useful. RELNOTES=n/a ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=301170647
1 parent a749ce9 commit bfacdb0

File tree

6 files changed

+58
-0
lines changed

6 files changed

+58
-0
lines changed

android/guava-tests/test/com/google/common/util/concurrent/SequentialExecutorTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.common.util.concurrent;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static com.google.common.util.concurrent.MoreExecutors.newSequentialExecutor;
2021
import static com.google.common.util.concurrent.Uninterruptibles.awaitUninterruptibly;
2122

2223
import com.google.common.collect.ImmutableList;
@@ -348,4 +349,21 @@ public void run() {
348349
assertThat(expected).hasCauseThat().isInstanceOf(RejectedExecutionException.class);
349350
}
350351
}
352+
353+
public void testToString() {
354+
Executor delegate =
355+
new Executor() {
356+
@Override
357+
public void execute(Runnable task) {}
358+
359+
@Override
360+
public String toString() {
361+
return "theDelegate";
362+
}
363+
};
364+
Executor sequential1 = newSequentialExecutor(delegate);
365+
Executor sequential2 = newSequentialExecutor(delegate);
366+
assertThat(sequential1.toString()).contains("theDelegate");
367+
assertThat(sequential1.toString()).isNotEqualTo(sequential2.toString());
368+
}
351369
}

android/guava/src/com/google/common/util/concurrent/MoreExecutors.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,11 @@ public void run() {
985985
thrownFromDelegate = false;
986986
command.run();
987987
}
988+
989+
@Override
990+
public String toString() {
991+
return command.toString();
992+
}
988993
});
989994
} catch (RejectedExecutionException e) {
990995
if (thrownFromDelegate) {

android/guava/src/com/google/common/util/concurrent/SequentialExecutor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.util.concurrent.SequentialExecutor.WorkerRunningState.QUEUED;
2020
import static com.google.common.util.concurrent.SequentialExecutor.WorkerRunningState.QUEUING;
2121
import static com.google.common.util.concurrent.SequentialExecutor.WorkerRunningState.RUNNING;
22+
import static java.lang.System.identityHashCode;
2223

2324
import com.google.common.annotations.GwtIncompatible;
2425
import com.google.common.base.Preconditions;
@@ -239,4 +240,9 @@ private void workOnQueue() {
239240
}
240241
}
241242
}
243+
244+
@Override
245+
public String toString() {
246+
return "SequentialExecutor@" + identityHashCode(this) + "{" + executor + "}";
247+
}
242248
}

guava-tests/test/com/google/common/util/concurrent/SequentialExecutorTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.common.util.concurrent;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static com.google.common.util.concurrent.MoreExecutors.newSequentialExecutor;
2021
import static com.google.common.util.concurrent.Uninterruptibles.awaitUninterruptibly;
2122

2223
import com.google.common.collect.ImmutableList;
@@ -348,4 +349,21 @@ public void run() {
348349
assertThat(expected).hasCauseThat().isInstanceOf(RejectedExecutionException.class);
349350
}
350351
}
352+
353+
public void testToString() {
354+
Executor delegate =
355+
new Executor() {
356+
@Override
357+
public void execute(Runnable task) {}
358+
359+
@Override
360+
public String toString() {
361+
return "theDelegate";
362+
}
363+
};
364+
Executor sequential1 = newSequentialExecutor(delegate);
365+
Executor sequential2 = newSequentialExecutor(delegate);
366+
assertThat(sequential1.toString()).contains("theDelegate");
367+
assertThat(sequential1.toString()).isNotEqualTo(sequential2.toString());
368+
}
351369
}

guava/src/com/google/common/util/concurrent/MoreExecutors.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,11 @@ public void run() {
10901090
thrownFromDelegate = false;
10911091
command.run();
10921092
}
1093+
1094+
@Override
1095+
public String toString() {
1096+
return command.toString();
1097+
}
10931098
});
10941099
} catch (RejectedExecutionException e) {
10951100
if (thrownFromDelegate) {

guava/src/com/google/common/util/concurrent/SequentialExecutor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.util.concurrent.SequentialExecutor.WorkerRunningState.QUEUED;
2020
import static com.google.common.util.concurrent.SequentialExecutor.WorkerRunningState.QUEUING;
2121
import static com.google.common.util.concurrent.SequentialExecutor.WorkerRunningState.RUNNING;
22+
import static java.lang.System.identityHashCode;
2223

2324
import com.google.common.annotations.GwtIncompatible;
2425
import com.google.common.base.Preconditions;
@@ -239,4 +240,9 @@ private void workOnQueue() {
239240
}
240241
}
241242
}
243+
244+
@Override
245+
public String toString() {
246+
return "SequentialExecutor@" + identityHashCode(this) + "{" + executor + "}";
247+
}
242248
}

0 commit comments

Comments
 (0)