Skip to content

Commit

Permalink
Convert anonymous classes to lambda (apache#4703)
Browse files Browse the repository at this point in the history
* Convert anonymous functions to lambda

* Replacing lambda with anonymous implementation, because lambda cannot be mocked
  • Loading branch information
vzhikserg authored and merlimat committed Jul 21, 2019
1 parent 8a3b3af commit 84364dd
Show file tree
Hide file tree
Showing 28 changed files with 120 additions and 304 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1294,31 +1294,25 @@ public void testOpenRaceCondition() throws Exception {
final Position position = ledger.addEntry("entry-0".getBytes());
Executor executor = Executors.newCachedThreadPool();
final CountDownLatch counter = new CountDownLatch(2);
executor.execute(new Runnable() {
@Override
public void run() {
try {
for (int i = 0; i < N; i++) {
c1.markDelete(position);
}
counter.countDown();
} catch (Exception e) {
e.printStackTrace();
executor.execute(() -> {
try {
for (int i = 0; i < N; i++) {
c1.markDelete(position);
}
counter.countDown();
} catch (Exception e) {
e.printStackTrace();
}
});

executor.execute(new Runnable() {
@Override
public void run() {
try {
for (int i = 0; i < N; i++) {
ledger.openCursor("cursor-" + i);
}
counter.countDown();
} catch (Exception e) {
e.printStackTrace();
executor.execute(() -> {
try {
for (int i = 0; i < N; i++) {
ledger.openCursor("cursor-" + i);
}
counter.countDown();
} catch (Exception e) {
e.printStackTrace();
}
});

Expand Down Expand Up @@ -1832,12 +1826,7 @@ public void operationFailed(MetaStoreException e) {
ManagedLedgerImpl newVersionLedger = (ManagedLedgerImpl) factory.open("backward_test_ledger", conf);
List<LedgerInfo> mlInfo = newVersionLedger.getLedgersInfoAsList();

assertTrue(mlInfo.stream().allMatch(new Predicate<LedgerInfo>() {
@Override
public boolean test(LedgerInfo ledgerInfo) {
return ledgerInfo.hasTimestamp();
}
}));
assertTrue(mlInfo.stream().allMatch(ledgerInfo -> ledgerInfo.hasTimestamp()));
}

@Test
Expand Down Expand Up @@ -2288,15 +2277,8 @@ public void readEntryFailed(ManagedLedgerException exception, Object ctx) {
responseException1.set(exception);
}
}, ctxStr);
ledger.asyncCreateLedger(bk, config, null, new CreateCallback() {
@Override
public void createComplete(int rc, LedgerHandle lh, Object ctx) {

}
}, Collections.emptyMap());
retryStrategically((test) -> {
return responseException1.get() != null;
}, 5, 1000);
ledger.asyncCreateLedger(bk, config, null, (rc, lh, ctx) -> {}, Collections.emptyMap());
retryStrategically((test) -> responseException1.get() != null, 5, 1000);
assertNotNull(responseException1.get());
assertEquals(responseException1.get().getMessage(), BKException.getMessage(BKException.Code.TimeoutException));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,12 @@ public void lock() {
salary.add(1000);
// No thread competition here
// We will test thread competition in unlock()
new Thread(new Runnable() {
@Override
public void run() {
cbm.lock();
if (salary.value() == 1000)
salary.add(2000);
cbm.unlock();
Assert.assertEquals(salary.value(), 3000);
}
new Thread(() -> {
cbm.lock();
if (salary.value() == 1000)
salary.add(2000);
cbm.unlock();
Assert.assertEquals(salary.value(), 3000);
}).start();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,7 @@ private void init(ExecutorService executor) {

private MockZooKeeper(String quorum) throws Exception {
// This constructor is never called
super(quorum, 1, new Watcher() {
@Override
public void process(WatchedEvent event) {
}
});
super(quorum, 1, event -> {});
assert false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,11 @@ public void process(WatchedEvent event) {
log.warn("Election node {} is deleted, attempting re-election...", event.getPath());
if (event.getPath().equals(ELECTION_ROOT)) {
log.info("This should call elect again...");
executor.execute(new Runnable() {
@Override
public void run() {
// If the node is deleted, attempt the re-election
log.info("Broker [{}] is calling re-election from the thread",
pulsar.getSafeWebServiceAddress());
elect();
}
executor.execute(() -> {
// If the node is deleted, attempt the re-election
log.info("Broker [{}] is calling re-election from the thread",
pulsar.getSafeWebServiceAddress());
elect();
});
}
break;
Expand Down Expand Up @@ -148,12 +145,7 @@ public void run() {
log.warn(
"Got exception [{}] while creating election node because it already exists. Attempting re-election...",
nee.getMessage());
executor.execute(new Runnable() {
@Override
public void run() {
elect();
}
});
executor.execute(this::elect);
} catch (Exception e) {
// Kill the broker because this broker's session with zookeeper might be stale. Killing the broker will
// make sure that we get the fresh zookeeper session.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,7 @@ public Map<String, String> deserialize(String key, byte[] content) throws Except
}

// register listener to capture zk-latency
zkStatsListener = new EventListner() {
@Override
public void recordLatency(EventType eventType, long latencyMs) {
pulsarStats.recordZkLatencyTimeValue(eventType, latencyMs);
}
};
zkStatsListener = (eventType, latencyMs) -> pulsarStats.recordZkLatencyTimeValue(eventType, latencyMs);

this.delayedDeliveryTrackerFactory = DelayedDeliveryTrackerLoader
.loadDelayedDeliveryTrackerFactory(pulsar.getConfiguration());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -671,13 +671,10 @@ public void testConcurrentConsumerReceiveWhileReconnect(int batchMessageDelayMs)

final CyclicBarrier barrier = new CyclicBarrier(numConsumersThreads + 1);
for (int i = 0; i < numConsumersThreads; i++) {
executor.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
barrier.await();
consumer.receive();
return null;
}
executor.submit((Callable<Void>) () -> {
barrier.await();
consumer.receive();
return null;
});
}

Expand Down Expand Up @@ -712,13 +709,10 @@ public Void call() throws Exception {

barrier.reset();
for (int i = 0; i < numConsumersThreads; i++) {
executor.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
barrier.await();
consumer.receive();
return null;
}
executor.submit((Callable<Void>) () -> {
barrier.await();
consumer.receive();
return null;
});
}
barrier.await();
Expand All @@ -742,13 +736,10 @@ public Void call() throws Exception {

barrier.reset();
for (int i = 0; i < numConsumersThreads; i++) {
executor.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
barrier.await();
consumer.receive();
return null;
}
executor.submit((Callable<Void>) () -> {
barrier.await();
consumer.receive();
return null;
});
}
barrier.await();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,7 @@ void setup(Method method) throws Exception {

// delete all function temp files
File dir = new File(System.getProperty("java.io.tmpdir"));
File[] foundFiles = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("function");
}
});
File[] foundFiles = dir.listFiles((ignoredDir, name) -> name.startsWith("function"));

for (File file : foundFiles) {
file.delete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,7 @@ void setup(Method method) throws Exception {

// delete all function temp files
File dir = new File(System.getProperty("java.io.tmpdir"));
File[] foundFiles = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("function");
}
});
File[] foundFiles = dir.listFiles((ignoredDir, name) -> name.startsWith("function"));

for (File file : foundFiles) {
file.delete();
Expand Down Expand Up @@ -376,11 +372,7 @@ public void testPulsarFunctionState() throws Exception {

// make sure all temp files are deleted
File dir = new File(System.getProperty("java.io.tmpdir"));
File[] foundFiles = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("function");
}
});
File[] foundFiles = dir.listFiles((dir1, name) -> name.startsWith("function"));

Assert.assertEquals(foundFiles.length, 0, "Temporary files left over: " + Arrays.asList(foundFiles));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,7 @@ void setup(Method method) throws Exception {

// delete all function temp files
File dir = new File(System.getProperty("java.io.tmpdir"));
File[] foundFiles = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("function");
}
});
File[] foundFiles = dir.listFiles((ignoredDir, name) -> name.startsWith("function"));

for (File file : foundFiles) {
file.delete();
Expand Down Expand Up @@ -411,11 +407,7 @@ public void testPulsarFunctionState() throws Exception {

// make sure all temp files are deleted
File dir = new File(System.getProperty("java.io.tmpdir"));
File[] foundFiles = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("function");
}
});
File[] foundFiles = dir.listFiles((dir1, name) -> name.startsWith("function"));

Assert.assertEquals(foundFiles.length, 0, "Temporary files left over: " + Arrays.asList(foundFiles));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,7 @@ void setup(Method method) throws Exception {

// delete all function temp files
File dir = new File(System.getProperty("java.io.tmpdir"));
File[] foundFiles = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("function");
}
});
File[] foundFiles = dir.listFiles((dir1, name) -> name.startsWith("function"));

for (File file : foundFiles) {
file.delete();
Expand All @@ -162,7 +158,7 @@ public boolean accept(File dir, String name) {
log.info("--- Setting up method {} ---", method.getName());

// Start local bookkeeper ensemble
bkEnsemble = new LocalBookkeeperEnsemble(3, ZOOKEEPER_PORT, () -> PortManager.nextFreePort());
bkEnsemble = new LocalBookkeeperEnsemble(3, ZOOKEEPER_PORT, PortManager::nextFreePort);
bkEnsemble.start();

String brokerServiceUrl = "https://127.0.0.1:" + brokerWebServiceTlsPort;
Expand Down Expand Up @@ -492,11 +488,7 @@ private void testE2EPulsarFunction(String jarFilePathUrl) throws Exception {

// make sure all temp files are deleted
File dir = new File(System.getProperty("java.io.tmpdir"));
File[] foundFiles = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("function");
}
});
File[] foundFiles = dir.listFiles((dir1, name) -> name.startsWith("function"));

Assert.assertEquals(foundFiles.length, 0, "Temporary files left over: " + Arrays.asList(foundFiles));
}
Expand Down Expand Up @@ -726,11 +718,7 @@ private void testPulsarSinkStats(String jarFilePathUrl) throws Exception {

// make sure all temp files are deleted
File dir = new File(System.getProperty("java.io.tmpdir"));
File[] foundFiles = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("function");
}
});
File[] foundFiles = dir.listFiles((dir1, name) -> name.startsWith("function"));

Assert.assertEquals(foundFiles.length, 0, "Temporary files left over: " + Arrays.asList(foundFiles));
}
Expand Down Expand Up @@ -869,11 +857,7 @@ private void testPulsarSourceStats(String jarFilePathUrl) throws Exception {

// make sure all temp files are deleted
File dir = new File(System.getProperty("java.io.tmpdir"));
File[] foundFiles = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("function");
}
});
File[] foundFiles = dir.listFiles((dir1, name) -> name.startsWith("function"));

Assert.assertEquals(foundFiles.length, 0, "Temporary files left over: " + Arrays.asList(foundFiles));
}
Expand Down Expand Up @@ -1221,11 +1205,7 @@ public void testPulsarFunctionStats() throws Exception {

// make sure all temp files are deleted
File dir = new File(System.getProperty("java.io.tmpdir"));
File[] foundFiles = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("function");
}
});
File[] foundFiles = dir.listFiles((dir1, name) -> name.startsWith("function"));

Assert.assertEquals(foundFiles.length, 0, "Temporary files left over: " + Arrays.asList(foundFiles));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,9 @@
@Slf4j
public class NarClassLoader extends URLClassLoader {

private static final FileFilter JAR_FILTER = new FileFilter() {
@Override
public boolean accept(File pathname) {
final String nameToTest = pathname.getName().toLowerCase();
return nameToTest.endsWith(".jar") && pathname.isFile();
}
private static final FileFilter JAR_FILTER = pathname -> {
final String nameToTest = pathname.getName().toLowerCase();
return nameToTest.endsWith(".jar") && pathname.isFile();
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,7 @@ public synchronized TimeUnit getRateTimeUnit() {
}

protected ScheduledFuture<?> createTask() {
return executorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
renew();
}
}, this.rateTime, this.rateTime, this.timeUnit);
return executorService.scheduleAtFixedRate(this::renew, this.rateTime, this.rateTime, this.timeUnit);
}

synchronized void renew() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,7 @@ public void testPutIfAbsent() {
public void testComputeIfAbsent() {
ConcurrentLongHashMap<Integer> map = new ConcurrentLongHashMap<>(16, 1);
AtomicInteger counter = new AtomicInteger();
LongFunction<Integer> provider = new LongFunction<Integer>() {
public Integer apply(long key) {
return counter.getAndIncrement();
}
};
LongFunction<Integer> provider = key -> counter.getAndIncrement();

assertEquals(map.computeIfAbsent(0, provider).intValue(), 0);
assertEquals(map.get(0).intValue(), 0);
Expand Down
Loading

0 comments on commit 84364dd

Please sign in to comment.