vyommani commented on code in PR #748:
URL: https://github.com/apache/ranger/pull/748#discussion_r2597177244


##########
security-admin/src/main/java/org/apache/ranger/rest/ServiceREST.java:
##########
@@ -4298,6 +4308,40 @@ private RangerPolicyList 
toRangerPolicyList(List<RangerPolicy> policyList, Searc
         return ret;
     }
 
+    private List<RangerPolicy> getRangerPoliciesInRange(List<RangerPolicy> 
policyList, SearchFilter filter) {
+        List<RangerPolicy> retList = null;
+        if (CollectionUtils.isNotEmpty(policyList)) {
+            int totalCount = policyList.size();
+            int startIndex = filter.getBeginIndex();
+            int offset = filter.getOffset();
+            int toIndex = Math.min(startIndex + offset, totalCount);
+            LOG.info("==>totalCount: {}, startIndex:{}, offsetSize: {}, 
toIndex: {}" , totalCount, startIndex, offset, toIndex);
+            String sortType = filter.getSortType();
+            String sortBy = filter.getSortBy();
+
+            if (StringUtils.isNotEmpty(sortBy) && 
StringUtils.isNotEmpty(sortType)) {
+                // By default policyList is sorted by policyId in asc order, 
So handling only desc case.
+                if (SearchFilter.POLICY_ID.equalsIgnoreCase(sortBy)) {
+                    if (SORT_ORDER.DESC.name().equalsIgnoreCase(sortType)) {
+                        policyList.sort(this.getPolicyComparator(sortBy, 
sortType));
+                    }
+                } else if (SearchFilter.POLICY_NAME.equalsIgnoreCase(sortBy)) {
+                    if (SORT_ORDER.ASC.name().equalsIgnoreCase(sortType) || 
SORT_ORDER.DESC.name().equalsIgnoreCase(sortType)) {
+                        policyList.sort(this.getPolicyComparator(sortBy, 
sortType));
+                    } else {
+                        LOG.info("Invalid or Unsupported sortType : {}", 
sortType);
+                    }
+                } else {
+                    LOG.info("Invalid or Unsupported sortBy property : {}" , 
sortBy);
+                }
+            }
+
+            retList = new ArrayList<>(policyList.subList(startIndex, toIndex));
+
+        }
+        return retList;

Review Comment:
   Returning `null` when the input list is empty can lead to 
`NullPointerException` for callers that don't explicitly check for null. I 
suggest returning an empty list instead of `null`.
   
   option 1 
   
   if (CollectionUtils.isEmpty(policyList)) {
           return Collections.emptyList();
       }
   
   or
   
   option  2
   
   return retList != null ? retList : Collections.emptyList();
   
   My suggestion is to use the option 1.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to