FilteredPage.java

/*
 * Copyright 2018 Global Crop Diversity Trust
 *
 * 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
 *
 *   http://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 org.genesys.server.api;

import java.io.Serializable;
import java.util.Iterator;
import java.util.function.Function;

import com.fasterxml.jackson.annotation.JsonInclude;
import org.genesys.blocks.model.filters.SuperModelFilter;
import org.springframework.data.domain.Page;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonUnwrapped;

/**
 * The Class FilteredPage.
 *
 * @param <T> the generic type
 */
public class FilteredPage<T, F extends SuperModelFilter<F, ?>> implements Iterable<T>, Serializable {

	private static final long serialVersionUID = 6965069448240229428L;

	/** The data is serialized on the base JSON level. */
	@JsonUnwrapped
	@JsonIgnoreProperties({ "pageable" })
	public Page<T> page;

	/** The filter. */
	public F filter;

	/** The filter code. */
	@JsonInclude(JsonInclude.Include.NON_EMPTY)
	public String filterCode;

	/**
	 * Instantiates a new filtered page.
	 *
	 * @param filter the filter
	 * @param data the data
	 */
	public FilteredPage(final F filter, final Page<T> data) {
		this.filter = filter;
		this.page = data;
	}

	/**
	 * Instantiates a new filtered page.
	 *
	 * @param filterCode the filter code
	 * @param filter the filter
	 * @param data the data
	 */
	public FilteredPage(final String filterCode, final F filter, final Page<T> data) {
		this.filterCode = filterCode;
		this.filter = filter;
		this.page = data;
	}

	@Override
	public Iterator<T> iterator() {
		return page.iterator();
	}

	public <U> FilteredPage<U, F> map(Function<? super T, ? extends U> mapper) {
		return new FilteredPage<U, F>(this.filterCode, this.filter, this.page.map(mapper));
	}

}