You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by il...@apache.org on 2021/12/08 11:07:53 UTC

[syncope] branch 2_1_X updated: Better Optional usage (#297)

This is an automated email from the ASF dual-hosted git repository.

ilgrosso pushed a commit to branch 2_1_X
in repository https://gitbox.apache.org/repos/asf/syncope.git


The following commit(s) were added to refs/heads/2_1_X by this push:
     new f93f5bb  Better Optional usage (#297)
f93f5bb is described below

commit f93f5bbce4c54d01b804bd97ccec4b042adfc10f
Author: Francesco Chicchiriccò <il...@users.noreply.github.com>
AuthorDate: Wed Dec 8 12:05:52 2021 +0100

    Better Optional usage (#297)
---
 .../syncope/core/persistence/jpa/dao/JPAConfDAO.java   | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAConfDAO.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAConfDAO.java
index 2bdb045..e81bf68 100644
--- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAConfDAO.java
+++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAConfDAO.java
@@ -57,23 +57,19 @@ public class JPAConfDAO extends AbstractDAO<Conf> implements ConfDAO {
     @Transactional(readOnly = true)
     @Override
     public List<String> getValuesAsStrings(final String key) {
-        Optional<? extends CPlainAttr> attr = find(key);
-        return attr.isPresent() ? attr.get().getValuesAsStrings() : Collections.<String>emptyList();
+        return find(key).map(CPlainAttr::getValuesAsStrings).orElse(Collections.emptyList());
     }
 
     @Transactional(readOnly = true)
     @Override
     public <T> T find(final String key, final T defaultValue) {
-        Optional<? extends CPlainAttr> result = find(key);
-        if (!result.isPresent()) {
-            return defaultValue;
-        }
-
-        return result.get().getUniqueValue() == null
-                ? result.get().getValues().isEmpty()
+        return find(key).
+                map(attr -> attr.getUniqueValue() == null
+                ? attr.getValues().isEmpty()
                 ? null
-                : result.get().getValues().get(0).<T>getValue()
-                : result.get().getUniqueValue().<T>getValue();
+                : attr.getValues().get(0).<T>getValue()
+                : attr.getUniqueValue().<T>getValue()).
+                orElse(defaultValue);
     }
 
     @Override