com.github.triceo.robozonky.common.remote.PaginatedImpl Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2017 The RoboZonky Project
*
* 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 com.github.triceo.robozonky.common.remote;
import java.util.Collection;
import java.util.Collections;
import com.github.triceo.robozonky.api.remote.EntityCollectionApi;
import com.github.triceo.robozonky.internal.api.Settings;
final class PaginatedImpl implements Paginated {
private final PaginatedApi> api;
private final int pageSize;
private final Sort ordering;
private PaginatedResult itemsOnPage;
PaginatedImpl(final PaginatedApi> api) {
this(api, Sort.unspecified(), Settings.INSTANCE.getDefaultApiPageSize()); // for testing purposes
}
public PaginatedImpl(final PaginatedApi> api, final Sort ordering,
final int pageSize) {
this.ordering = ordering;
this.api = api;
this.pageSize = pageSize;
this.nextPage();
}
@Override
public boolean nextPage() {
this.itemsOnPage = api.execute(EntityCollectionApi::items, this.ordering, this.getPageId() + 1, this.pageSize);
return this.itemsOnPage != null && !this.itemsOnPage.getPage().isEmpty();
}
@Override
public Collection getItemsOnPage() {
return this.itemsOnPage == null ? Collections.emptyList() : this.itemsOnPage.getPage();
}
@Override
public int getPageId() {
return this.itemsOnPage == null ? -1 : this.itemsOnPage.getCurrentPageId();
}
@Override
public int getExpectedPageSize() {
return this.pageSize;
}
@Override
public int getTotalItemCount() {
return this.itemsOnPage == null ? 0 : this.itemsOnPage.getTotalResultCount();
}
}