From f3d4a520ebe981b5375351b447830b8036897d5f Mon Sep 17 00:00:00 2001 From: Sergey Nuyanzin Date: Tue, 22 Aug 2023 17:02:40 +0200 Subject: [PATCH] Make code for exist methods more concise (#1615) Signed-off-by: Sergey Nuyanzin --- .../klaw/helpers/db/rdbms/SelectDataJdbc.java | 67 +++++-------------- 1 file changed, 17 insertions(+), 50 deletions(-) diff --git a/core/src/main/java/io/aiven/klaw/helpers/db/rdbms/SelectDataJdbc.java b/core/src/main/java/io/aiven/klaw/helpers/db/rdbms/SelectDataJdbc.java index 13760e2273..598f9c7f8b 100644 --- a/core/src/main/java/io/aiven/klaw/helpers/db/rdbms/SelectDataJdbc.java +++ b/core/src/main/java/io/aiven/klaw/helpers/db/rdbms/SelectDataJdbc.java @@ -27,6 +27,7 @@ import java.util.TreeSet; import java.util.function.Supplier; import java.util.stream.Collectors; +import java.util.stream.Stream; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Example; @@ -1436,8 +1437,7 @@ public Optional selectProductDetails(String name) { } public boolean existsKafkaComponentsForEnv(String env, int tenantId) { - List> list = - List.of( + return Stream.>of( () -> topicRepo.existsByEnvironmentAndTenantId(env, tenantId), () -> topicRequestsRepo.existsByTenantIdAndEnvironmentAndRequestStatus( @@ -1445,57 +1445,35 @@ public boolean existsKafkaComponentsForEnv(String env, int tenantId) { () -> aclRepo.existsByEnvironmentAndTenantId(env, tenantId), () -> aclRequestsRepo.existsByTenantIdAndEnvironmentAndRequestStatus( - tenantId, env, RequestStatus.CREATED.value)); - for (var elem : list) { - if (elem.get()) { - return true; - } - } - return false; + tenantId, env, RequestStatus.CREATED.value)) + .anyMatch(Supplier::get); } public boolean existsConnectorComponentsForEnv(String env, int tenantId) { - List> list = - List.of( + return Stream.>of( () -> kafkaConnectorRepo.existsByEnvironmentAndTenantId(env, tenantId), () -> kafkaConnectorRequestsRepo.existsConnectorRequestsForEnvTenantIdAndCreatedStatus( - env, tenantId)); - for (var elem : list) { - if (elem.get()) { - return true; - } - } - return false; + env, tenantId)) + .anyMatch(Supplier::get); } public boolean existsSchemaComponentsForEnv(String env, int tenantId) { - List> list = - List.of( - () -> - schemaRequestRepo.existsSchemaRequestByEnvironmentAndTenantIdAndRequestStatus( - env, tenantId, RequestStatus.CREATED.value), - () -> messageSchemaRepo.existsMessageSchemaByEnvironmentAndTenantId(env, tenantId)); - for (var elem : list) { - if (elem.get()) { - return true; - } - } - return false; + return Stream.>of( + () -> schemaRequestRepo.existsSchemaRequestByEnvironmentAndTenantId(env, tenantId), + () -> messageSchemaRepo.existsMessageSchemaByEnvironmentAndTenantId(env, tenantId)) + .anyMatch(Supplier::get); } public boolean existsComponentsCountForTeam(Integer teamId, int tenantId) { - List> list = - List.of( + return Stream.>of( () -> { boolean res = schemaRequestRepo.existsRecordsCountForTeamId(teamId, tenantId); - log.debug("For team {} Active Schema Requests {}", teamId, res); return res; }, () -> { boolean res = messageSchemaRepo.existsRecordsCountForTeamId(teamId, tenantId); - log.debug("For team {} number of Schemas in DB {}", teamId, res); return res; }, @@ -1529,28 +1507,17 @@ public boolean existsComponentsCountForTeam(Integer teamId, int tenantId) { boolean res = aclRequestsRepo.existsRecordsCountForTeamId(teamId, tenantId); log.debug("For team {} number of ACL in DB {}", teamId, res); return res; - }); - for (var elem : list) { - if (elem.get()) { - return true; - } - } - return false; + }) + .anyMatch(Supplier::get); } public boolean existsComponentsCountForUser(String userId, int tenantId) { - List> list = - List.of( + return Stream.>of( () -> schemaRequestRepo.existsRecordsCountForUserId(userId, tenantId), () -> kafkaConnectorRequestsRepo.existsRecordsCountForUserId(userId, tenantId), () -> topicRequestsRepo.existsRecordsCountForUserId(userId, tenantId), - () -> aclRequestsRepo.existsRecordsCountForUserId(userId, tenantId)); - for (var elem : list) { - if (elem.get()) { - return true; - } - } - return false; + () -> aclRequestsRepo.existsRecordsCountForUserId(userId, tenantId)) + .anyMatch(Supplier::get); } public int getAllTopicsCountInAllTenants() {