Skip to content

Commit 8878b91

Browse files
committed
Refactor ClientRepository to enhance code readability
1 parent f51aee9 commit 8878b91

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

src/main/java/de/adorsys/keycloak/config/repository/ClientRepository.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,10 @@ public ClientRepository(RealmRepository realmRepository) {
6161
public Optional<ClientRepresentation> searchByClientId(String realmName, String clientId) {
6262
List<ClientRepresentation> foundClients = getResource(realmName).findByClientId(Objects.requireNonNull(clientId));
6363

64-
Optional<ClientRepresentation> client;
65-
if (foundClients.isEmpty()) {
66-
client = Optional.empty();
67-
} else {
68-
client = Optional.of(foundClients.get(0));
69-
}
70-
71-
return client;
64+
return switch(foundClients) {
65+
case List<ClientRepresentation> list when list.isEmpty() -> Optional.empty();
66+
case List<ClientRepresentation> list -> Optional.of(list.get(0));
67+
};
7268
}
7369

7470
public Optional<ClientRepresentation> searchByName(String realmName, String name) {
@@ -89,21 +85,23 @@ public Optional<ClientRepresentation> searchByName(String realmName, String name
8985
public ClientRepresentation getByClientId(String realmName, String clientId) {
9086
Optional<ClientRepresentation> foundClients = searchByClientId(realmName, clientId);
9187

92-
if (foundClients.isEmpty()) {
93-
throw new KeycloakRepositoryException("Cannot find client by clientId '%s'", clientId);
94-
}
95-
96-
return foundClients.get();
88+
return switch(foundClients) {
89+
case Optional<ClientRepresentation> foundClient when foundClient.isEmpty() ->
90+
throw new KeycloakRepositoryException("Cannot find client by clientId '%s'", clientId);
91+
case Optional<ClientRepresentation> foundClient ->
92+
foundClient.get();
93+
};
9794
}
9895

9996
public ClientRepresentation getByName(String realmName, String name) {
10097
Optional<ClientRepresentation> foundClients = searchByName(realmName, name);
10198

102-
if (foundClients.isEmpty()) {
99+
return switch(foundClients) {
100+
case Optional<ClientRepresentation> foundClient when foundClient.isEmpty() ->
103101
throw new KeycloakRepositoryException("Cannot find client by name '%s'", name);
104-
}
105-
106-
return foundClients.get();
102+
case Optional<ClientRepresentation> foundClient ->
103+
foundClient.get();
104+
};
107105
}
108106

109107
public ResourceServerRepresentation getAuthorizationConfigById(String realmName, String id) {

0 commit comments

Comments
 (0)