
com.netflix.evcache.util.CircularIterator Maven / Gradle / Ivy
The newest version!
package com.netflix.evcache.util;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
/**
* A circular iterator for ReplicaSets. This ensures that all ReplicaSets are
* equal number of requests.
*
* @author smadappa
*/
public class CircularIterator {
private Entry entry;
private int size = 0;
/**
* Creates an instance of ReplicaSetCircularIterator across all ReplicaSets.
*
* @param allReplicaSets
* Set of all available ReplicaSets.
*/
public CircularIterator(Collection allReplicaSets) {
if (allReplicaSets == null || allReplicaSets.isEmpty()) return;
Entry pEntry = null;
for (Iterator itr = allReplicaSets.iterator(); itr.hasNext();) {
size++;
final T rSet = itr.next();
final Entry newEntry = new Entry(rSet, pEntry);
if (entry == null) entry = newEntry;
pEntry = newEntry;
}
/*
* Connect the first and the last entry to form a circular list
*/
if (pEntry != null) {
entry.next = pEntry;
}
}
/**
* Returns the next ReplicaSet which should get the request.
*
* @return - the next ReplicaSetCircularIterator in the iterator. If there
* are none then null is returned.
*/
public T next() {
if (entry == null) return null;
entry = entry.next;
return entry.element;
}
/**
* Returns the next ReplicaSet excluding the given ReplicaSet which should
* get the request.
*
* @return - the next ReplicaSet in the iterator. If there are none then
* null is returned.
*/
public T next(T ignoreReplicaSet) {
if (entry == null) return null;
entry = entry.next;
if (entry.element.equals(ignoreReplicaSet)) {
return entry.next.element;
} else {
return entry.element;
}
}
public int getSize() {
return size;
}
/**
* The Entry keeps track of the current element and next element in the
* list.
*
* @author smadappa
*
* @param
*/
static class Entry {
private E element;
private Entry next;
/**
* Creates an instance of Entry.
*/
Entry(E element, Entry next) {
this.element = element;
this.next = next;
}
}
public String toString() {
final StringBuilder current = new StringBuilder();
if (entry != null) {
Entry startEntry = entry;
if(startEntry.element.getClass().isArray()) {
for(int i = 0; i < Array.getLength(startEntry.element); i++) {
if(i > 0) current.append(",");
current.append("[").append(i).append(", ").append(Array.get(startEntry.element, i).toString()).append("]");
}
} else {
current.append(startEntry.element);
}
while (!entry.next.equals(startEntry)) {
if(entry.next.element.getClass().isArray()) {
for(int i = 0; i < Array.getLength(entry.next.element); i++) {
if(i > 0) current.append(",");
current.append("[").append(i).append(", ").append(Array.get(entry.next.element, i).toString()).append("]");
}
} else {
current.append(",[").append(entry.next.element).append("]");
}
entry = entry.next;
}
}
return "Server Group Iterator : { size=" + getSize() + "; Server Group=" + current.toString() + "}";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy