OneByOneLoadBalancer extends QueryPoolLoadBalancer<Q, E> { @Override public Iterator<Pair<Q, E>> provide() { final CircularSupplier<Q> querySupplier = CircularSupplier.create(queryProvider); final CircularSupplier<E> endpointSupplier = CircularSupplier.create(endpointProvider); return new Iterator<Pair<Q, E>>() { @Override public boolean hasNext() { return true; } @Override public Pair<Q, E> next() { boolean exceeded = endpointSupplier.exceeded(); E endpoint = endpointSupplier.pop(); Q query = exceeded ? querySupplier.pop() : querySupplier.peek(); return Pair.of(query, endpoint); } @Override public void remove() { throw new UnsupportedOperationException("Read only iterator"); } @Override public String toString() { return "OneByOneLoadBalancer iterator"; } }; } OneByOneLoadBalancer();  OneByOneLoadBalancer(Iterable<Q> queryProvider, Iterable<E> endpointProvider); @Override Iterator<Pair<Q, E>> provide(); @Override String toString();  }
@Test public void shouldProvidePairs() throws Exception { List<Integer> queries = ImmutableList.of(1, 2, 3); List<Integer> endpoints = ImmutableList.of(1, 2); OneByOneLoadBalancer<Integer, Integer> balancer = new OneByOneLoadBalancer<Integer, Integer>(queries, endpoints); Iterator<Pair<Integer,Integer>> chunks = balancer.provide(); assertEquals(chunks.next(), Pair.of(1, 1)); assertEquals(chunks.next(), Pair.of(1, 2)); assertEquals(chunks.next(), Pair.of(2, 1)); assertEquals(chunks.next(), Pair.of(2, 2)); assertEquals(chunks.next(), Pair.of(3, 1)); assertEquals(chunks.next(), Pair.of(3, 2)); }