Update Janusgraph to 0.5.0 in traversal
[aai/traversal.git] / aai-traversal / src / main / java / org / onap / aai / rest / util / PaginationUtil.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2024 Deutsche Telekom. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.rest.util;
21
22 import java.util.Collections;
23 import java.util.List;
24
25 import org.onap.aai.exceptions.AAIException;
26 import org.onap.aai.query.builder.Pageable;
27
28 public class PaginationUtil {
29
30     public static List<Object> getPaginatedVertexListForAggregateFormat(List<Object> aggregateVertexList, Pageable pageable) throws AAIException {
31         if (aggregateVertexList != null && !aggregateVertexList.isEmpty() && aggregateVertexList.size() == 1) {
32             List<Object> vertexList = (List<Object>) aggregateVertexList.get(0);
33             int fromIndex = pageable.getPage() * pageable.getPageSize();
34             List<Object> page = vertexList.subList(fromIndex, fromIndex + pageable.getPageSize());
35             return Collections.singletonList(page);
36         }
37         // If the list size is greater than 1 or if pagination is not needed, return the original list.
38         return aggregateVertexList;
39     }
40
41     public static boolean hasValidPaginationParams(Pageable pageable) {
42       return pageable.getPage() >= 0 && pageable.getPageSize() > 0;
43     }
44
45     public static long getTotalPages(Pageable pageable, long totalCount) {
46         int pageSize = pageable.getPageSize();
47         long totalPages = totalCount / pageSize;
48         // conditionally add a page for the remainder
49         if (totalCount % pageSize > 0) {
50             totalPages++;
51         }
52         return totalPages;
53     }
54 }