io.datarouter.scanner.PagingScanner Maven / Gradle / Ivy
/*
* Copyright © 2009 HotPads ([email protected])
*
* 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 io.datarouter.scanner;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
/**
* Base class for things that page through results by passing the last item as an exclusive start key for the next page
*/
public abstract class PagingScanner extends BaseScanner>{
protected final int pageSize;
protected PagingScanner(int pageSize){
this.pageSize = pageSize;
}
/**
* Subclass should transform the last seen item into a key for the next request
*/
protected abstract Optional nextParam(T lastSeenItem);
protected abstract List nextPage(Optional resumeFrom);
@Override
public boolean advance(){
if(current != null && current.size() < pageSize){
return false;
}
T lastSeenItem = getLast(current);
Optional resumeFrom = nextParam(lastSeenItem);
current = nextPage(resumeFrom);
return notEmpty(current);
}
private static boolean notEmpty(Collection collection){
return collection != null && !collection.isEmpty();
}
private static T getLast(List list){
if(notEmpty(list)){
return list.getLast();
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy