![JAR search and dependency download from the Maven repository](/logo.png)
autofixture.implementationdetails.InstanceCursor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of autofixturegenerator Show documentation
Show all versions of autofixturegenerator Show documentation
An attempt to reimplement core features of a popular .NET anonymous value generator - AutoFixture - in
Java
package autofixture.implementationdetails;
import java.util.Map;
import java.util.Random;
public class InstanceCursor {
private static final Random RANDOM = new Random();
private Integer value;
private final Class key;
private final Map indicesByClass;
public InstanceCursor(
final Integer value,
final Class key,
final Map indicesByClass) {
this.value = value;
this.key = key;
this.indicesByClass = indicesByClass;
}
public static InstanceCursor randomUpTo(final int length, final Class key, final Map indicesByClass) {
return new InstanceCursor(RANDOM.nextInt(length), key, indicesByClass);
}
public static InstanceCursor from(final Map indicesByClass, final Class key, final int length) {
if (!indicesByClass.containsKey(key)) {
InstanceCursor index = randomUpTo(length, key, indicesByClass);
index.save();
return index;
} else {
InstanceCursor index = indicesByClass.get(key);
index.resetIfExceeds(length);
return index;
}
}
public T selectFrom(
final T[] possibleValues) {
return possibleValues[this.value];
}
public Integer advance() {
return this.value++;
}
public void save() {
this.indicesByClass.put(this.key, this);
}
public void resetIfExceeds(final int length) {
if (this.value >= length) {
this.value = 0;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy