listNetworks: moved pagination post processing to StringUtils class

Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/ca8d3672
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/ca8d3672
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/ca8d3672

Branch: refs/heads/master
Commit: ca8d3672e8d319f7bc6db516e58e3dbb7b225487
Parents: 9a94bc2
Author: Alena Prokharchyk <alena.prokharc...@citrix.com>
Authored: Mon Jul 28 13:51:50 2014 -0700
Committer: Alena Prokharchyk <alena.prokharc...@citrix.com>
Committed: Mon Jul 28 14:43:55 2014 -0700

----------------------------------------------------------------------
 .../com/cloud/network/NetworkServiceImpl.java   | 32 +++++---------------
 utils/src/com/cloud/utils/StringUtils.java      | 28 +++++++++++++++++
 2 files changed, 35 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/ca8d3672/server/src/com/cloud/network/NetworkServiceImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/NetworkServiceImpl.java 
b/server/src/com/cloud/network/NetworkServiceImpl.java
index b154fd9..4ac1bea 100755
--- a/server/src/com/cloud/network/NetworkServiceImpl.java
+++ b/server/src/com/cloud/network/NetworkServiceImpl.java
@@ -40,9 +40,6 @@ import javax.ejb.Local;
 import javax.inject.Inject;
 import javax.naming.ConfigurationException;
 
-import com.cloud.network.security.SecurityGroupService;
-import org.apache.log4j.Logger;
-
 import org.apache.cloudstack.acl.ControlledEntity.ACLType;
 import org.apache.cloudstack.acl.SecurityChecker.AccessType;
 import org.apache.cloudstack.api.command.admin.network.CreateNetworkCmdByAdmin;
@@ -57,6 +54,7 @@ import org.apache.cloudstack.context.CallContext;
 import 
org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
 import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
 import 
org.apache.cloudstack.network.element.InternalLoadBalancerElementService;
+import org.apache.log4j.Logger;
 
 import com.cloud.api.ApiDBUtils;
 import com.cloud.configuration.Config;
@@ -129,6 +127,7 @@ import com.cloud.network.rules.FirewallRule.Purpose;
 import com.cloud.network.rules.FirewallRuleVO;
 import com.cloud.network.rules.RulesManager;
 import com.cloud.network.rules.dao.PortForwardingRulesDao;
+import com.cloud.network.security.SecurityGroupService;
 import com.cloud.network.vpc.NetworkACL;
 import com.cloud.network.vpc.PrivateIpVO;
 import com.cloud.network.vpc.Vpc;
@@ -1624,33 +1623,16 @@ public class NetworkServiceImpl extends ManagerBase 
implements  NetworkService {
         }
 
         //Now apply pagination
-        //Most likely pageSize will never exceed int value, and we need 
integer to partition the listToReturn
-        boolean notNull = cmd.getStartIndex() != null && cmd.getPageSizeVal() 
!= null;
-        if (notNull && cmd.getStartIndex() <= Integer.MAX_VALUE && 
cmd.getStartIndex() >= Integer.MIN_VALUE && cmd.getPageSizeVal() <= 
Integer.MAX_VALUE
-                && cmd.getPageSizeVal() >= Integer.MIN_VALUE) {
-            int index = cmd.getStartIndex().intValue() == 0 ? 0 : 
cmd.getStartIndex().intValue() / cmd.getPageSizeVal().intValue();
-            List<NetworkVO> wPagination = new ArrayList<NetworkVO>();
-            List<List<NetworkVO>> partitions = 
partitionNetworks(networksToReturn, cmd.getPageSizeVal().intValue());
-            if (index < partitions.size()) {
-                wPagination = partitions.get(index);
-            }
-            return new Pair<List<? extends Network>, Integer>(wPagination, 
networksToReturn.size());
+        List<?> wPagination = StringUtils.applyPagination(networksToReturn, 
cmd.getStartIndex(), cmd.getPageSizeVal());
+        if (wPagination != null) {
+            @SuppressWarnings("unchecked")
+            Pair<List<? extends Network>, Integer> listWPagination = new 
Pair<List<? extends Network>, Integer>((List<NetworkVO>) wPagination, 
networksToReturn.size());
+            return listWPagination;
         }
 
         return new Pair<List<? extends Network>, Integer>(networksToReturn, 
networksToReturn.size());
     }
 
-    private static List<List<NetworkVO>> partitionNetworks(List<NetworkVO> 
originalList, int chunkSize) {
-        List<List<NetworkVO>> listOfChunks = new ArrayList<List<NetworkVO>>();
-        for (int i = 0; i < originalList.size() / chunkSize; i++) {
-            listOfChunks.add(originalList.subList(i * chunkSize, i * chunkSize 
+ chunkSize));
-        }
-        if (originalList.size() % chunkSize != 0) {
-            listOfChunks.add(originalList.subList(originalList.size() - 
originalList.size() % chunkSize, originalList.size()));
-        }
-        return listOfChunks;
-    }
-
     private SearchCriteria<NetworkVO> 
buildNetworkSearchCriteria(SearchBuilder<NetworkVO> sb, String keyword, Long 
id, Boolean isSystem, Long zoneId, String guestIpType,
             String trafficType, Long physicalNetworkId, String aclType, 
boolean skipProjectNetworks, Boolean restartRequired, Boolean specifyIpRanges, 
Long vpcId,
             Map<String, String> tags, Boolean display) {

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/ca8d3672/utils/src/com/cloud/utils/StringUtils.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/StringUtils.java 
b/utils/src/com/cloud/utils/StringUtils.java
index 3b8cf71..f4c3f9e 100644
--- a/utils/src/com/cloud/utils/StringUtils.java
+++ b/utils/src/com/cloud/utils/StringUtils.java
@@ -268,4 +268,32 @@ public class StringUtils {
         }
         return s;
     }
+
+    public static List<?> applyPagination(List<?> originalList, Long 
startIndex, Long pageSizeVal) {
+        // Most likely pageSize will never exceed int value, and we need 
integer to partition the listToReturn
+        boolean applyPagination = startIndex != null && pageSizeVal != null
+                && startIndex <= Integer.MAX_VALUE && startIndex >= 
Integer.MIN_VALUE && pageSizeVal <= Integer.MAX_VALUE
+                && pageSizeVal >= Integer.MIN_VALUE;
+        List<?> listWPagination = null;
+        if (applyPagination) {
+            listWPagination = new ArrayList<>();
+            int index = startIndex.intValue() == 0 ? 0 : startIndex.intValue() 
/ pageSizeVal.intValue();
+            List<List<?>> partitions = StringUtils.partitionList(originalList, 
pageSizeVal.intValue());
+            if (index < partitions.size()) {
+                listWPagination = partitions.get(index);
+            }
+        }
+        return listWPagination;
+    }
+
+    private static List<List<?>> partitionList(List<?> originalList, int 
chunkSize) {
+        List<List<?>> listOfChunks = new ArrayList<List<?>>();
+        for (int i = 0; i < originalList.size() / chunkSize; i++) {
+            listOfChunks.add(originalList.subList(i * chunkSize, i * chunkSize 
+ chunkSize));
+        }
+        if (originalList.size() % chunkSize != 0) {
+            listOfChunks.add(originalList.subList(originalList.size() - 
originalList.size() % chunkSize, originalList.size()));
+        }
+        return listOfChunks;
+    }
 }

Reply via email to