/* * Copyright 2012 Benjamin Gehrels * * 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 info.gehrels.flockDBClient; import com.twitter.flockdb.thrift.Edge; import com.twitter.flockdb.thrift.EdgeQuery; import com.twitter.flockdb.thrift.EdgeResults; import com.twitter.flockdb.thrift.FlockDB.Iface; import com.twitter.flockdb.thrift.FlockException; import info.gehrels.flockDBClient.FlockAndThriftExceptionHandling.MethodObject; import org.apache.thrift.TException; import java.util.Collections; import java.util.Iterator; import java.util.List; import static info.gehrels.flockDBClient.FlockAndThriftExceptionHandling.handleFlockAndThriftExceptions; public class PagedEdgeList implements Iterable<Edge> { private final Iface backingFlockClient; private final EdgeQuery edgeQuery; private final EdgeResults results; PagedEdgeList(Iface backingFlockClient, EdgeQuery edgeQuery, EdgeResults results) { this.backingFlockClient = backingFlockClient; this.edgeQuery = edgeQuery; this.results = results; } public PagedEdgeList getNextPage() { return getOtherPage(results.next_cursor); } public PagedEdgeList getPreviousPage() { return getOtherPage(results.prev_cursor); } public boolean hasNextPage() { return results.next_cursor != 0; } public boolean hasPreviousPage() { return edgeQuery.getPage().getCursor() != 0; } @Override public Iterator<Edge> iterator() { return this.results.getEdges().iterator(); } private PagedEdgeList getOtherPage(final long otherPagesCursor) { return handleFlockAndThriftExceptions(new MethodObject<PagedEdgeList>() { @Override public PagedEdgeList call() throws TException, FlockException { EdgeQuery nextPageQuery = edgeQuery.setPage(edgeQuery.getPage().setCursor(otherPagesCursor)); List<EdgeResults> results = backingFlockClient.select_edges(Collections.singletonList(nextPageQuery)); return new PagedEdgeList(backingFlockClient, nextPageQuery, results.get(0)); } }); } }