[enhance](*): improve code about optional (#11153)
improve code about optional
This commit is contained in:
@ -70,10 +70,9 @@ public class AlterPolicyStmt extends DdlStmt {
|
||||
.getPolicyMgr().getPoliciesByType(PolicyTypeEnum.STORAGE);
|
||||
Optional<Policy> hasPolicy = policiesByType.stream()
|
||||
.filter(policy -> policy.getPolicyName().equals(this.policyName)).findAny();
|
||||
if (!hasPolicy.isPresent()) {
|
||||
throw new AnalysisException("Unknown storage policy: " + this.policyName);
|
||||
}
|
||||
StoragePolicy storagePolicy = (StoragePolicy) hasPolicy.get();
|
||||
StoragePolicy storagePolicy = (StoragePolicy) hasPolicy.orElseThrow(
|
||||
() -> new AnalysisException("Unknown storage policy: " + this.policyName)
|
||||
);
|
||||
|
||||
// default storage policy use alter storage policy to add s3 resource.
|
||||
if (!policyName.equalsIgnoreCase(StoragePolicy.DEFAULT_STORAGE_POLICY_NAME) && properties.containsKey(
|
||||
|
||||
@ -342,23 +342,15 @@ public class SparkRepository {
|
||||
}
|
||||
|
||||
public SparkLibrary getDppLibrary() {
|
||||
SparkLibrary result = null;
|
||||
Optional<SparkLibrary> library = libraries.stream()
|
||||
.filter(lib -> lib.libType == SparkLibrary.LibType.DPP).findFirst();
|
||||
if (library.isPresent()) {
|
||||
result = library.get();
|
||||
}
|
||||
return result;
|
||||
return library.orElse(null);
|
||||
}
|
||||
|
||||
public SparkLibrary getSpark2xLibrary() {
|
||||
SparkLibrary result = null;
|
||||
Optional<SparkLibrary> library = libraries.stream()
|
||||
.filter(lib -> lib.libType == SparkLibrary.LibType.SPARK2X).findFirst();
|
||||
if (library.isPresent()) {
|
||||
result = library.get();
|
||||
}
|
||||
return result;
|
||||
return library.orElse(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -854,13 +854,10 @@ public abstract class RoutineLoadJob extends AbstractTxnStateChangeCallback impl
|
||||
routineLoadTaskInfoList.stream()
|
||||
.filter(entity -> entity.getTxnId() == txnState.getTransactionId()).findFirst();
|
||||
if (!routineLoadTaskInfoOptional.isPresent()) {
|
||||
switch (transactionStatus) {
|
||||
case COMMITTED:
|
||||
throw new TransactionException("txn " + txnState.getTransactionId()
|
||||
+ " could not be " + transactionStatus
|
||||
+ " while task " + txnState.getLabel() + " has been aborted.");
|
||||
default:
|
||||
break;
|
||||
if (transactionStatus == TransactionStatus.COMMITTED) {
|
||||
throw new TransactionException("txn " + txnState.getTransactionId()
|
||||
+ " could not be " + transactionStatus
|
||||
+ " while task " + txnState.getLabel() + " has been aborted.");
|
||||
}
|
||||
}
|
||||
passCheck = true;
|
||||
|
||||
@ -207,9 +207,7 @@ public class RoutineLoadManager implements Writable {
|
||||
Optional<RoutineLoadJob> optional = routineLoadJobList.parallelStream()
|
||||
.filter(entity -> entity.getName().equals(name))
|
||||
.filter(entity -> !entity.getState().isFinalState()).findFirst();
|
||||
if (optional.isPresent()) {
|
||||
return true;
|
||||
}
|
||||
return optional.isPresent();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
@ -83,7 +83,7 @@ public class PatternGeneratorAnalyzer {
|
||||
List<PatternGenerator> generators = planClassMap.entrySet()
|
||||
.stream()
|
||||
.map(kv -> PatternGenerator.create(this, kv.getKey(), kv.getValue()))
|
||||
.filter(generator -> generator.isPresent())
|
||||
.filter(Optional::isPresent)
|
||||
.map(Optional::get)
|
||||
.sorted((g1, g2) -> {
|
||||
// logical first
|
||||
@ -135,9 +135,8 @@ public class PatternGeneratorAnalyzer {
|
||||
}
|
||||
} else if (typeDeclaration instanceof ClassDeclaration) {
|
||||
ClassDeclaration classDeclaration = (ClassDeclaration) typeDeclaration;
|
||||
if (classDeclaration.extendsType.isPresent()) {
|
||||
analyzeClass(currentParentClasses, typeDeclaration, classDeclaration.extendsType.get());
|
||||
}
|
||||
classDeclaration.extendsType.ifPresent(
|
||||
typeType -> analyzeClass(currentParentClasses, typeDeclaration, typeType));
|
||||
if (!classDeclaration.implementTypes.isEmpty()) {
|
||||
for (TypeType implementType : classDeclaration.implementTypes) {
|
||||
analyzeClass(currentParentClasses, typeDeclaration, implementType);
|
||||
|
||||
@ -49,9 +49,7 @@ public class ClassDeclaration extends TypeDeclaration {
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
if (packageName.isPresent()) {
|
||||
buffer.append("package ").append(packageName.get()).append(";\n\n");
|
||||
}
|
||||
packageName.ifPresent(qualifiedName -> buffer.append("package ").append(qualifiedName).append(";\n\n"));
|
||||
|
||||
if (!imports.isEmpty()) {
|
||||
for (ImportDeclaration importDeclaration : imports) {
|
||||
@ -65,13 +63,9 @@ public class ClassDeclaration extends TypeDeclaration {
|
||||
mod += " ";
|
||||
}
|
||||
buffer.append(mod).append("class ").append(name);
|
||||
if (typeParameters.isPresent()) {
|
||||
buffer.append(typeParameters.get());
|
||||
}
|
||||
typeParameters.ifPresent(buffer::append);
|
||||
buffer.append(" ");
|
||||
if (extendsType.isPresent()) {
|
||||
buffer.append("extends ").append(extendsType.get()).append(" ");
|
||||
}
|
||||
extendsType.ifPresent(typeType -> buffer.append("extends ").append(typeType).append(" "));
|
||||
if (!implementTypes.isEmpty()) {
|
||||
buffer.append("implements ").append(Joiner.on(", ").join(implementTypes)).append(" ");
|
||||
}
|
||||
|
||||
@ -39,9 +39,7 @@ public class EnumDeclaration extends TypeDeclaration {
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
if (packageName.isPresent()) {
|
||||
buffer.append("package ").append(packageName.get()).append(";\n\n");
|
||||
}
|
||||
packageName.ifPresent(qualifiedName -> buffer.append("package ").append(qualifiedName).append(";\n\n"));
|
||||
|
||||
if (!imports.isEmpty()) {
|
||||
for (ImportDeclaration importDeclaration : imports) {
|
||||
|
||||
@ -32,10 +32,8 @@ public class IdentifyTypeArgumentsPair implements JavaAstNode {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (typeArguments.isPresent()) {
|
||||
return identifier + typeArguments.get();
|
||||
} else {
|
||||
return identifier;
|
||||
}
|
||||
return typeArguments
|
||||
.map(arguments -> identifier + arguments)
|
||||
.orElse(identifier);
|
||||
}
|
||||
}
|
||||
|
||||
@ -40,9 +40,7 @@ public class InterfaceDeclaration extends TypeDeclaration {
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
if (packageName.isPresent()) {
|
||||
buffer.append("package ").append(packageName.get()).append(";\n\n");
|
||||
}
|
||||
packageName.ifPresent(qualifiedName -> buffer.append("package ").append(qualifiedName).append(";\n\n"));
|
||||
|
||||
if (!imports.isEmpty()) {
|
||||
for (ImportDeclaration importDeclaration : imports) {
|
||||
@ -55,9 +53,7 @@ public class InterfaceDeclaration extends TypeDeclaration {
|
||||
mod += " ";
|
||||
}
|
||||
buffer.append(mod).append("interface ").append(name);
|
||||
if (typeParameters.isPresent()) {
|
||||
buffer.append(typeParameters.get());
|
||||
}
|
||||
typeParameters.ifPresent(buffer::append);
|
||||
buffer.append(" ");
|
||||
if (!extendsTypes.isEmpty()) {
|
||||
buffer.append("extends ").append(Joiner.on(", ").join(extendsTypes)).append(" ");
|
||||
|
||||
@ -48,9 +48,7 @@ public abstract class TypeDeclaration implements JavaAstNode {
|
||||
|
||||
/** function to concat package name and type name. */
|
||||
public static String getFullQualifiedName(Optional<QualifiedName> packageName, String name) {
|
||||
if (packageName.isPresent()) {
|
||||
return Joiner.on(".").join(packageName.get().identifiers) + "." + name;
|
||||
}
|
||||
return name;
|
||||
return packageName.map(qualifiedName -> Joiner.on(".").join(qualifiedName.identifiers) + "." + name)
|
||||
.orElse(name);
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,10 +32,7 @@ public class TypeParameter implements JavaAstNode {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (typeBound.isPresent()) {
|
||||
return identifier + " extends " + typeBound.get();
|
||||
} else {
|
||||
return identifier;
|
||||
}
|
||||
return typeBound.map(bound -> identifier + " extends " + bound)
|
||||
.orElse(identifier);
|
||||
}
|
||||
}
|
||||
|
||||
@ -31,10 +31,7 @@ public class TypeType implements JavaAstNode {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (primitiveType.isPresent()) {
|
||||
return primitiveType.get();
|
||||
} else {
|
||||
return classOrInterfaceType.get().toString();
|
||||
}
|
||||
return primitiveType
|
||||
.orElseGet(() -> classOrInterfaceType.get().toString());
|
||||
}
|
||||
}
|
||||
|
||||
@ -71,15 +71,11 @@ public class PushPredicateThroughJoin extends OneRewriteRuleFactory {
|
||||
LogicalJoin<GroupPlan, GroupPlan> join = filter.child();
|
||||
|
||||
Expression wherePredicates = filter.getPredicates();
|
||||
Expression onPredicates = BooleanLiteral.TRUE;
|
||||
Expression onPredicates = join.getCondition().orElse(BooleanLiteral.TRUE);
|
||||
|
||||
List<Expression> otherConditions = Lists.newArrayList();
|
||||
List<Expression> eqConditions = Lists.newArrayList();
|
||||
|
||||
if (join.getCondition().isPresent()) {
|
||||
onPredicates = join.getCondition().get();
|
||||
}
|
||||
|
||||
List<Slot> leftInput = join.left().getOutput();
|
||||
List<Slot> rightInput = join.right().getOutput();
|
||||
|
||||
|
||||
@ -41,10 +41,6 @@ public interface LogicalPlan extends Plan {
|
||||
}
|
||||
|
||||
default <C> LogicalPlan optionalMap(Optional<C> ctx, Supplier<LogicalPlan> f) {
|
||||
if (ctx.isPresent()) {
|
||||
return f.get();
|
||||
} else {
|
||||
return this;
|
||||
}
|
||||
return ctx.map(a -> f.get()).orElse(this);
|
||||
}
|
||||
}
|
||||
|
||||
@ -86,9 +86,9 @@ public class PhysicalHashJoin<
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("PhysicalHashJoin ([").append(joinType).append("]");
|
||||
if (condition.isPresent()) {
|
||||
sb.append(", [").append(condition.get()).append("]");
|
||||
}
|
||||
condition.ifPresent(
|
||||
expression -> sb.append(", [").append(expression).append("]")
|
||||
);
|
||||
sb.append(")");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@ -413,11 +413,9 @@ public class PolicyMgr implements Writable {
|
||||
}
|
||||
|
||||
Optional<Policy> policy = findPolicy(storagePolicyName, PolicyTypeEnum.STORAGE);
|
||||
|
||||
if (!policy.isPresent()) {
|
||||
throw new DdlException("Storage policy(" + storagePolicyName + ") dose not exist.");
|
||||
}
|
||||
StoragePolicy storagePolicy = (StoragePolicy) policy.get();
|
||||
StoragePolicy storagePolicy = (StoragePolicy) policy.orElseThrow(
|
||||
() -> new DdlException("Storage policy(" + storagePolicyName + ") dose not exist.")
|
||||
);
|
||||
storagePolicy.modifyProperties(properties);
|
||||
|
||||
// log alter
|
||||
|
||||
@ -444,7 +444,7 @@ public class ConnectContext {
|
||||
public void setDatabase(String db) {
|
||||
currentDb = db;
|
||||
Optional<DatabaseIf> dbInstance = getCurrentDataSource().getDb(db);
|
||||
currentDbId = dbInstance.isPresent() ? dbInstance.get().getId() : -1;
|
||||
currentDbId = dbInstance.map(DatabaseIf::getId).orElse(-1L);
|
||||
}
|
||||
|
||||
public void setExecutor(StmtExecutor executor) {
|
||||
|
||||
Reference in New Issue
Block a user