Jagger
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
OneByOneLoadBalancer.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2012 Grid Dynamics Consulting Services, Inc, All Rights Reserved
3  * http://www.griddynamics.com
4  *
5  * This library is free software; you can redistribute it and/or modify it under the terms of
6  * the Apache License; either
7  * version 2.0 of the License, or any later version.
8  *
9  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
10  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
11  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
12  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
13  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
15  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
16  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
17  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19  */
20 
21 package com.griddynamics.jagger.invoker;
22 
23 import com.griddynamics.jagger.util.Pair;
24 
25 import java.util.Iterator;
26 import java.util.List;
27 
42 public class OneByOneLoadBalancer<Q, E> extends QueryPoolLoadBalancer<Q, E> {
43 
45  super();
46  }
47 
48  public OneByOneLoadBalancer(Iterable<Q> queryProvider, Iterable<E> endpointProvider){
49  super(queryProvider, endpointProvider);
50  }
51 
59  @Override
60  public Iterator<Pair<Q, E>> provide() {
61  final CircularSupplier<Q> querySupplier = CircularSupplier.create(queryProvider);
62  final CircularSupplier<E> endpointSupplier = CircularSupplier.create(endpointProvider);
63 
64  return new Iterator<Pair<Q, E>>() {
65  @Override
66  public boolean hasNext() {
67  return true;
68  }
69 
70  @Override
71  public Pair<Q, E> next() {
72  boolean exceeded = endpointSupplier.exceeded();
73 
74  E endpoint = endpointSupplier.pop();
75  Q query = exceeded ? querySupplier.pop() : querySupplier.peek();
76 
77  return Pair.of(query, endpoint);
78 
79  }
80 
81  @Override
82  public void remove() {
83  throw new UnsupportedOperationException("Read only iterator");
84  }
85 
86  @Override
87  public String toString() {
88  return "OneByOneLoadBalancer iterator";
89  }
90 
91  };
92  }
93 
94  @Override
95  public String toString() {
96  return "OneByOneLoadBalancer";
97  }
98 }