All Downloads are FREE. Search and download functionalities are using the official Maven repository.

autofixture.implementationdetails.CircularList Maven / Gradle / Ivy

There is a newer version: 2.1.10
Show newest version
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;
  }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy