2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.rest.util;
22 import java.util.Collections;
23 import java.util.List;
25 import org.onap.aai.exceptions.AAIException;
26 import org.onap.aai.query.builder.Pageable;
28 public class PaginationUtil {
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);
37 // If the list size is greater than 1 or if pagination is not needed, return the original list.
38 return aggregateVertexList;
41 public static boolean hasValidPaginationParams(Pageable pageable) {
42 return pageable.getPage() >= 0 && pageable.getPageSize() > 0;
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) {