io.micronaut.data.model.Page Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micronaut-data-model Show documentation
Show all versions of micronaut-data-model Show documentation
Data Repository Support for Micronaut
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.data.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.micronaut.context.annotation.DefaultImplementation;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.ReflectiveAccess;
import io.micronaut.core.annotation.TypeHint;
import io.micronaut.serde.annotation.Serdeable;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Inspired by the Spring Data's {@code Page} and GORM's {@code PagedResultList}, this models a type that supports
* pagination operations.
*
* A Page is a result set associated with a particular {@link Pageable} that includes a calculation of the total
* size of number of records.
*
* @param The generic type
* @author graemerocher
* @since 1.0.0
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@TypeHint(Page.class)
@JsonDeserialize(as = DefaultPage.class)
@Serdeable
@DefaultImplementation(DefaultPage.class)
public interface Page extends Slice {
Page> EMPTY = new DefaultPage<>(Collections.emptyList(), Pageable.unpaged(), 0);
/**
* @return The total size of the all records.
*/
long getTotalSize();
/**
* @return The total number of pages
*/
default int getTotalPages() {
int size = getSize();
return size == 0 ? 1 : (int) Math.ceil((double) getTotalSize() / (double) size);
}
/**
* Maps the content with the given function.
*
* @param function The function to apply to each element in the content.
* @param The type returned by the function
* @return A new slice with the mapped content
*/
@Override
default @NonNull Page map(Function function) {
List content = getContent().stream().map(function).collect(Collectors.toList());
return new DefaultPage<>(content, getPageable(), getTotalSize());
}
/**
* Creates a slice from the given content and pageable.
* @param content The content
* @param pageable The pageable
* @param totalSize The total size
* @param The generic type
* @return The slice
*/
@JsonCreator
@ReflectiveAccess
static @NonNull Page of(
@JsonProperty("content") @NonNull List content,
@JsonProperty("pageable") @NonNull Pageable pageable,
@JsonProperty("totalSize") long totalSize) {
return new DefaultPage<>(content, pageable, totalSize);
}
/**
* Creates an empty page object.
* @param The generic type
* @return The slice
*/
@SuppressWarnings("unchecked")
static @NonNull Page empty() {
return (Page) EMPTY;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy