Skip to content

Commit 266b30c

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Use some static imports.
RELNOTES=n/a PiperOrigin-RevId: 687437051
1 parent 3a8463a commit 266b30c

File tree

66 files changed

+353
-364
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+353
-364
lines changed

android/guava-testlib/test/com/google/common/util/concurrent/testing/TestingExecutorsTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.common.util.concurrent.testing;
1818

19+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
1920
import static org.junit.Assert.assertThrows;
2021

2122
import com.google.common.collect.ImmutableList;
@@ -26,7 +27,6 @@
2627
import java.util.concurrent.ExecutionException;
2728
import java.util.concurrent.Future;
2829
import java.util.concurrent.ScheduledFuture;
29-
import java.util.concurrent.TimeUnit;
3030
import junit.framework.TestCase;
3131

3232
/**
@@ -47,7 +47,7 @@ public void run() {
4747
}
4848
};
4949
ScheduledFuture<?> future =
50-
TestingExecutors.noOpScheduledExecutor().schedule(task, 10, TimeUnit.MILLISECONDS);
50+
TestingExecutors.noOpScheduledExecutor().schedule(task, 10, MILLISECONDS);
5151
Thread.sleep(20);
5252
assertFalse(taskDone);
5353
assertFalse(future.isDone());
@@ -73,8 +73,7 @@ public Boolean call() {
7373
return taskDone;
7474
}
7575
};
76-
List<Future<Boolean>> futureList =
77-
executor.invokeAll(ImmutableList.of(task), 10, TimeUnit.MILLISECONDS);
76+
List<Future<Boolean>> futureList = executor.invokeAll(ImmutableList.of(task), 10, MILLISECONDS);
7877
Future<Boolean> future = futureList.get(0);
7978
assertFalse(taskDone);
8079
assertTrue(future.isDone());
@@ -92,7 +91,7 @@ public Integer call() {
9291
}
9392
};
9493
Future<Integer> future =
95-
TestingExecutors.sameThreadScheduledExecutor().schedule(task, 10000, TimeUnit.MILLISECONDS);
94+
TestingExecutors.sameThreadScheduledExecutor().schedule(task, 10000, MILLISECONDS);
9695
assertTrue("Should run callable immediately", taskDone);
9796
assertEquals(6, (int) future.get());
9897
}

android/guava-tests/benchmark/com/google/common/util/concurrent/ExecutionListBenchmark.java

Lines changed: 2 additions & 2 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.util.concurrent.MoreExecutors.directExecutor;
20+
import static java.util.concurrent.TimeUnit.SECONDS;
2021

2122
import com.google.caliper.AfterExperiment;
2223
import com.google.caliper.BeforeExperiment;
@@ -38,7 +39,6 @@
3839
import java.util.concurrent.Executor;
3940
import java.util.concurrent.Future;
4041
import java.util.concurrent.ThreadPoolExecutor;
41-
import java.util.concurrent.TimeUnit;
4242
import java.util.concurrent.atomic.AtomicInteger;
4343
import java.util.logging.Level;
4444
import java.util.logging.Logger;
@@ -251,7 +251,7 @@ void setUp() throws Exception {
251251
NUM_THREADS,
252252
NUM_THREADS,
253253
Long.MAX_VALUE,
254-
TimeUnit.SECONDS,
254+
SECONDS,
255255
new ArrayBlockingQueue<Runnable>(1000));
256256
executorService.prestartAllCoreThreads();
257257
final AtomicInteger integer = new AtomicInteger();

android/guava-tests/benchmark/com/google/common/util/concurrent/SingleThreadAbstractFutureBenchmark.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.google.common.util.concurrent;
1818

19+
import static java.util.concurrent.TimeUnit.NANOSECONDS;
20+
import static java.util.concurrent.TimeUnit.SECONDS;
21+
1922
import com.google.caliper.BeforeExperiment;
2023
import com.google.caliper.Benchmark;
2124
import com.google.caliper.Param;
@@ -26,7 +29,6 @@
2629
import java.util.List;
2730
import java.util.concurrent.CancellationException;
2831
import java.util.concurrent.ExecutionException;
29-
import java.util.concurrent.TimeUnit;
3032
import java.util.concurrent.TimeoutException;
3133

3234
/** A benchmark that times how long it takes to add a given number of */
@@ -105,7 +107,7 @@ public long timeGetWith0Timeout(long reps) throws Exception {
105107
long r = 0;
106108
for (int i = 0; i < reps; i++) {
107109
try {
108-
f.get(0, TimeUnit.SECONDS);
110+
f.get(0, SECONDS);
109111
r += 1;
110112
} catch (TimeoutException e) {
111113
r += 2;
@@ -120,7 +122,7 @@ public long timeGetWithSmallTimeout(long reps) throws Exception {
120122
long r = 0;
121123
for (int i = 0; i < reps; i++) {
122124
try {
123-
f.get(500, TimeUnit.NANOSECONDS);
125+
f.get(500, NANOSECONDS);
124126
r += 1;
125127
} catch (TimeoutException e) {
126128
r += 2;

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

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

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
2021
import static org.junit.Assert.assertThrows;
2122

2223
import com.google.common.util.concurrent.testing.MockFutureListener;
23-
import java.util.concurrent.TimeUnit;
2424
import java.util.concurrent.TimeoutException;
2525
import junit.framework.TestCase;
2626

@@ -50,7 +50,7 @@ protected void setUp() throws Exception {
5050

5151
public void testFutureGetBeforeCallback() throws Exception {
5252
// Verify that get throws a timeout exception before the callback is called.
53-
assertThrows(TimeoutException.class, () -> resultFuture.get(1L, TimeUnit.MILLISECONDS));
53+
assertThrows(TimeoutException.class, () -> resultFuture.get(1L, MILLISECONDS));
5454
}
5555

5656
public void testFutureGetThrowsWrappedException() throws Exception {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.google.common.collect.Lists.asList;
2121
import static com.google.common.truth.Truth.assertThat;
2222
import static com.google.common.truth.Truth.assertWithMessage;
23+
import static com.google.common.util.concurrent.ClosingFuture.withoutCloser;
2324
import static com.google.common.util.concurrent.Futures.immediateCancelledFuture;
2425
import static com.google.common.util.concurrent.Futures.immediateFailedFuture;
2526
import static com.google.common.util.concurrent.Futures.immediateFuture;
@@ -693,7 +694,7 @@ public TestCloseable call(DeferredCloser closer) throws Exception {
693694
},
694695
executor)
695696
.transformAsync(
696-
ClosingFuture.withoutCloser(
697+
withoutCloser(
697698
new AsyncFunction<TestCloseable, String>() {
698699
@Override
699700
public ListenableFuture<String> apply(TestCloseable v) throws Exception {
@@ -1490,7 +1491,7 @@ public void testCatchingAsync_preventsFurtherOperations() {
14901491
ClosingFuture<String> unused =
14911492
closingFuture.catchingAsync(
14921493
Exception.class,
1493-
ClosingFuture.withoutCloser(
1494+
withoutCloser(
14941495
new AsyncFunction<Exception, String>() {
14951496
@Override
14961497
public ListenableFuture<String> apply(Exception x) throws Exception {

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

Lines changed: 3 additions & 5 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 java.util.concurrent.TimeUnit.MILLISECONDS;
2021
import static org.junit.Assert.assertThrows;
2122

2223
import com.google.common.testing.TearDown;
@@ -28,7 +29,6 @@
2829
import java.util.concurrent.ExecutorService;
2930
import java.util.concurrent.Executors;
3031
import java.util.concurrent.ScheduledExecutorService;
31-
import java.util.concurrent.TimeUnit;
3232
import java.util.concurrent.TimeoutException;
3333
import junit.framework.TestCase;
3434

@@ -297,8 +297,7 @@ public void testServiceTimeoutOnStartUp() throws Exception {
297297

298298
TimeoutException e =
299299
assertThrows(
300-
TimeoutException.class,
301-
() -> service.startAsync().awaitRunning(1, TimeUnit.MILLISECONDS));
300+
TimeoutException.class, () -> service.startAsync().awaitRunning(1, MILLISECONDS));
302301
assertThat(e.getMessage()).contains(Service.State.STARTING.toString());
303302
}
304303

@@ -370,8 +369,7 @@ protected String serviceName() {
370369
};
371370
TimeoutException e =
372371
assertThrows(
373-
TimeoutException.class,
374-
() -> service.startAsync().awaitRunning(1, TimeUnit.MILLISECONDS));
372+
TimeoutException.class, () -> service.startAsync().awaitRunning(1, MILLISECONDS));
375373
assertThat(e)
376374
.hasMessageThat()
377375
.isEqualTo("Timed out waiting for Foo [STARTING] to reach the RUNNING state.");

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

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@
2020
import static com.google.common.base.StandardSystemProperty.OS_NAME;
2121
import static com.google.common.truth.Truth.assertThat;
2222
import static com.google.common.truth.Truth.assertWithMessage;
23+
import static com.google.common.util.concurrent.Futures.immediateCancelledFuture;
24+
import static com.google.common.util.concurrent.Futures.immediateFailedFuture;
25+
import static com.google.common.util.concurrent.Futures.immediateFuture;
2326
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
27+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
28+
import static java.util.concurrent.TimeUnit.NANOSECONDS;
29+
import static java.util.concurrent.TimeUnit.SECONDS;
2430
import static org.junit.Assert.assertThrows;
2531

2632
import com.google.common.annotations.GwtIncompatible;
@@ -134,7 +140,7 @@ public void testGetWithTimeoutDoneFuture() throws Exception {
134140
set("foo");
135141
}
136142
};
137-
assertEquals("foo", future.get(0, TimeUnit.SECONDS));
143+
assertEquals("foo", future.get(0, SECONDS));
138144
}
139145

140146
public void testEvilFuture_setFuture() throws Exception {
@@ -255,8 +261,7 @@ public String pendingToString() {
255261
assertThat(testFuture.toString())
256262
.matches(
257263
"[^\\[]+\\[status=PENDING, info=\\[cause=\\[Because this test isn't done\\]\\]\\]");
258-
TimeoutException e =
259-
assertThrows(TimeoutException.class, () -> testFuture.get(1, TimeUnit.NANOSECONDS));
264+
TimeoutException e = assertThrows(TimeoutException.class, () -> testFuture.get(1, NANOSECONDS));
260265
assertThat(e.getMessage()).contains("1 nanoseconds");
261266
assertThat(e.getMessage()).contains("Because this test isn't done");
262267
}
@@ -290,13 +295,12 @@ public void testToString_delayedTimeout() throws Exception {
290295
// TODO(b/261217224, b/361604053): Make this test work under newer JDKs.
291296
return;
292297
}
293-
TimedWaiterThread thread =
294-
new TimedWaiterThread(new AbstractFuture<Object>() {}, 2, TimeUnit.SECONDS);
298+
TimedWaiterThread thread = new TimedWaiterThread(new AbstractFuture<Object>() {}, 2, SECONDS);
295299
thread.start();
296300
thread.awaitWaiting();
297301
Thread.class.getMethod("suspend").invoke(thread);
298302
// Sleep for enough time to add 1500 milliseconds of overwait to the get() call.
299-
long toWaitMillis = 3500 - TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - thread.startTime);
303+
long toWaitMillis = 3500 - NANOSECONDS.toMillis(System.nanoTime() - thread.startTime);
300304
Thread.sleep(toWaitMillis);
301305
thread.setPriority(Thread.MAX_PRIORITY);
302306
Thread.class.getMethod("resume").invoke(thread);
@@ -306,12 +310,11 @@ public void testToString_delayedTimeout() throws Exception {
306310
// being resumed. To avoid a flake in this scenario, calculate how long that thread actually
307311
// waited and assert based on that time. Empirically, the race where the thread ends up waiting
308312
// for 5.5 seconds happens about 2% of the time.
309-
boolean longWait = TimeUnit.NANOSECONDS.toSeconds(thread.timeSpentBlocked) >= 5;
313+
boolean longWait = NANOSECONDS.toSeconds(thread.timeSpentBlocked) >= 5;
310314
// Count how long it actually took to return; we'll accept any number between the expected delay
311315
// and the approximate actual delay, to be robust to variance in thread scheduling.
312316
char overWaitNanosFirstDigit =
313-
Long.toString(
314-
thread.timeSpentBlocked - TimeUnit.MILLISECONDS.toNanos(longWait ? 5000 : 3000))
317+
Long.toString(thread.timeSpentBlocked - MILLISECONDS.toNanos(longWait ? 5000 : 3000))
315318
.charAt(0);
316319
if (overWaitNanosFirstDigit < '4') {
317320
overWaitNanosFirstDigit = '9';
@@ -349,12 +352,11 @@ public String pendingToString() {
349352
}
350353

351354
public void testToString_cancelled() throws Exception {
352-
assertThat(Futures.immediateCancelledFuture().toString())
353-
.matches("[^\\[]+\\[status=CANCELLED\\]");
355+
assertThat(immediateCancelledFuture().toString()).matches("[^\\[]+\\[status=CANCELLED\\]");
354356
}
355357

356358
public void testToString_failed() {
357-
assertThat(Futures.immediateFailedFuture(new RuntimeException("foo")).toString())
359+
assertThat(immediateFailedFuture(new RuntimeException("foo")).toString())
358360
.matches("[^\\[]+\\[status=FAILURE, cause=\\[java.lang.RuntimeException: foo\\]\\]");
359361
}
360362

@@ -472,7 +474,7 @@ public void testFutureBash() {
472474
};
473475
Callable<@Nullable Void> setFutureCompleteSuccessfullyRunnable =
474476
new Callable<@Nullable Void>() {
475-
ListenableFuture<String> future = Futures.immediateFuture("setFuture");
477+
ListenableFuture<String> future = immediateFuture("setFuture");
476478

477479
@Override
478480
public @Nullable Void call() {
@@ -485,8 +487,7 @@ public void testFutureBash() {
485487
};
486488
Callable<@Nullable Void> setFutureCompleteExceptionallyRunnable =
487489
new Callable<@Nullable Void>() {
488-
ListenableFuture<String> future =
489-
Futures.immediateFailedFuture(new Exception("setFuture"));
490+
ListenableFuture<String> future = immediateFailedFuture(new Exception("setFuture"));
490491

491492
@Override
492493
public @Nullable Void call() {
@@ -499,7 +500,7 @@ public void testFutureBash() {
499500
};
500501
Callable<@Nullable Void> setFutureCancelRunnable =
501502
new Callable<@Nullable Void>() {
502-
ListenableFuture<String> future = Futures.immediateCancelledFuture();
503+
ListenableFuture<String> future = immediateCancelledFuture();
503504

504505
@Override
505506
public @Nullable Void call() {
@@ -534,7 +535,7 @@ public void run() {
534535
Future<String> future = currentFuture.get();
535536
while (true) {
536537
try {
537-
String result = Uninterruptibles.getUninterruptibly(future, 0, TimeUnit.SECONDS);
538+
String result = Uninterruptibles.getUninterruptibly(future, 0, SECONDS);
538539
finalResults.add(result);
539540
break;
540541
} catch (ExecutionException e) {
@@ -665,7 +666,7 @@ public void run() {
665666
Future<String> future = currentFuture.get();
666667
while (true) {
667668
try {
668-
String result = Uninterruptibles.getUninterruptibly(future, 0, TimeUnit.SECONDS);
669+
String result = Uninterruptibles.getUninterruptibly(future, 0, SECONDS);
669670
finalResults.add(result);
670671
break;
671672
} catch (ExecutionException e) {
@@ -765,7 +766,7 @@ public void testSetFutureCancelBash_withDoneFuture() {
765766
};
766767
Callable<@Nullable Void> setFutureCompleteSuccessfullyRunnable =
767768
new Callable<@Nullable Void>() {
768-
final ListenableFuture<String> future = Futures.immediateFuture("hello");
769+
final ListenableFuture<String> future = immediateFuture("hello");
769770

770771
@Override
771772
public @Nullable Void call() {
@@ -1014,7 +1015,7 @@ public void run() {
10141015
ranImmediately.set(true);
10151016
}
10161017
},
1017-
MoreExecutors.directExecutor());
1018+
directExecutor());
10181019
assertThat(ranImmediately.get()).isTrue();
10191020
}
10201021
};
@@ -1030,7 +1031,7 @@ public void testListenersExecuteImmediately_afterWaiterWakesUp() throws Exceptio
10301031
protected void afterDone() {
10311032
// this simply delays executing listeners
10321033
try {
1033-
Thread.sleep(TimeUnit.SECONDS.toMillis(10));
1034+
Thread.sleep(SECONDS.toMillis(10));
10341035
} catch (InterruptedException ignored) {
10351036
Thread.currentThread().interrupt(); // preserve status
10361037
}
@@ -1053,7 +1054,7 @@ public void run() {
10531054
ranImmediately.set(true);
10541055
}
10551056
},
1056-
MoreExecutors.directExecutor());
1057+
directExecutor());
10571058
assertThat(ranImmediately.get()).isTrue();
10581059
t.interrupt();
10591060
t.join();
@@ -1307,7 +1308,7 @@ private PollingThread(AbstractFuture<?> future) {
13071308
public void run() {
13081309
while (true) {
13091310
try {
1310-
future.get(0, TimeUnit.SECONDS);
1311+
future.get(0, SECONDS);
13111312
return;
13121313
} catch (InterruptedException | ExecutionException e) {
13131314
return;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
21+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
2122
import static org.junit.Assert.assertThrows;
2223

2324
import com.google.common.collect.Lists;
2425
import java.util.List;
2526
import java.util.concurrent.Executor;
26-
import java.util.concurrent.TimeUnit;
2727
import java.util.concurrent.TimeoutException;
2828
import junit.framework.TestCase;
2929

@@ -137,8 +137,7 @@ protected String serviceName() {
137137
};
138138
TimeoutException e =
139139
assertThrows(
140-
TimeoutException.class,
141-
() -> service.startAsync().awaitRunning(1, TimeUnit.MILLISECONDS));
140+
TimeoutException.class, () -> service.startAsync().awaitRunning(1, MILLISECONDS));
142141
assertThat(e)
143142
.hasMessageThat()
144143
.isEqualTo("Timed out waiting for Foo [STARTING] to reach the RUNNING state.");

0 commit comments

Comments
 (0)