autofixture.implementationdetails.CircularList Maven / Gradle / Ivy
package autofixture.implementationdetails;
import autofixture.interfaces.InstanceType;
import java.util.ArrayList;
import java.util.Random;
public class CircularList {
private static final Random RANDOM = new Random();
private final T[] enumConstants;
private int currentIndex = 0;
public CircularList(final T[] values) {
this.enumConstants = values.clone();
currentIndex = RANDOM.nextInt(values.length);
}
public static CircularList createFromEnum(final InstanceType type) {
final TListElement[] enumConstants = type.getEnumConstants();
return new CircularList<>(enumConstants);
}
public static CircularList fromCharactersIn(final String string) {
final ArrayList chars = new ArrayList<>();
string.chars().forEachOrdered(i -> chars.add((char) i));
return new CircularList<>(chars.toArray(new Character[chars.size()]));
}
public T next() {
if (currentIndex >= enumConstants.length) {
currentIndex = 0;
}
final T nextElement = enumConstants[currentIndex];
currentIndex++;
return nextElement;
}
}