Improved: Replace Callable objects with lambda expressions

(OFBIZ-10488)

Since Java 8 it is possible to instantiate functional interfaces 
(interfaces with one non-static method) with lambda expressions instead 
of anonymous classes.

This replaces the creation of callable objects with lambda expressions.

Thanks: Mathieu Lirzin

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1848514 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jacques Le Roux
2018-12-09 13:04:23 +00:00
parent 7c67b85698
commit 13f8b82f51
11 changed files with 148 additions and 190 deletions
@@ -281,12 +281,9 @@ public class GenericDelegator implements Delegator {
}
protected Callable<Void> createHelperCallable(final String groupName) {
return new Callable<Void>() {
@Override
public Void call() {
initializeOneGenericHelper(groupName);
return null;
}
return () -> {
initializeOneGenericHelper(groupName);
return null;
};
}
@@ -312,11 +309,7 @@ public class GenericDelegator implements Delegator {
return;
}
Callable<EntityEcaHandler<?>> creator = new Callable<EntityEcaHandler<?>>() {
public EntityEcaHandler<?> call() {
return createEntityEcaHandler();
}
};
Callable<EntityEcaHandler<?>> creator = () -> createEntityEcaHandler();
FutureTask<EntityEcaHandler<?>> futureTask = new FutureTask<>(creator);
if (this.entityEcaHandler.compareAndSet(null, futureTask)) {
// This needs to use BATCH, as the service engine might add it's own items into a thread pool.
@@ -2617,11 +2610,7 @@ public class GenericDelegator implements Delegator {
return;
}
Callable<DistributedCacheClear> creator = new Callable<DistributedCacheClear>() {
public DistributedCacheClear call() {
return createDistributedCacheClear();
}
};
Callable<DistributedCacheClear> creator = () -> createDistributedCacheClear();
FutureTask<DistributedCacheClear> futureTask = new FutureTask<>(creator);
if (distributedCacheClear.compareAndSet(null, futureTask)) {
ExecutionPool.GLOBAL_BATCH.submit(futureTask);
@@ -1198,26 +1198,22 @@ public class EntityTestSuite extends EntityTestCase {
final AtomicBoolean nullSeqIdReturned = new AtomicBoolean(false);
List<Future<Void>> futures = new ArrayList<>();
Callable<Void> getSeqIdTask = new Callable<Void>() {
public Void call() throws Exception {
Long seqId = sequencer.getNextSeqId(sequenceName, 1, null);
if (seqId == null) {
nullSeqIdReturned.set(true);
return null;
}
Long existingValue = seqIds.putIfAbsent(seqId, seqId);
if (existingValue != null) {
duplicateFound.set(true);
}
return null;
}
};
Callable<Void> refreshTask = new Callable<Void>() {
public Void call() throws Exception {
sequencer.forceBankRefresh(sequenceName, 1);
return null;
}
};
Callable<Void> getSeqIdTask = () -> {
Long seqId = sequencer.getNextSeqId(sequenceName, 1, null);
if (seqId == null) {
nullSeqIdReturned.set(true);
return null;
}
Long existingValue = seqIds.putIfAbsent(seqId, seqId);
if (existingValue != null) {
duplicateFound.set(true);
}
return null;
};
Callable<Void> refreshTask = () -> {
sequencer.forceBankRefresh(sequenceName, 1);
return null;
};
double probabilityOfRefresh = 0.1;
for (int i = 1; i <= 1000; i++) {
Callable<Void> randomTask = Math.random() < probabilityOfRefresh ? refreshTask : getSeqIdTask;
@@ -206,11 +206,9 @@ public final class EntityCrypto {
newValue.set("keyName", hashedKeyName);
try {
TransactionUtil.doNewTransaction(new Callable<Void>() {
public Void call() throws Exception {
delegator.create(newValue);
return null;
}
TransactionUtil.doNewTransaction(() -> {
delegator.create(newValue);
return null;
}, "storing encrypted key", 0, true);
} catch (GenericEntityException e) {
throw new EntityCryptoException(e);