com.bbn.bue.common.collections.ShufflingCollection Maven / Gradle / Ivy
The newest version!
package com.bbn.bue.common.collections;
import com.google.common.collect.ImmutableList;
import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Iterator;
import java.util.Random;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* A collection whose iteration order is randomized on every attempt to iterate over it.
* @param
*/
public final class ShufflingCollection extends AbstractCollection implements Collection {
private final ShufflingIterable data;
private final int size;
private ShufflingCollection(final ShufflingIterable data, final int size) {
this.data = checkNotNull(data);
this.size = size;
checkArgument(size >=0);
}
public static ShufflingCollection shufflingCopy(final Iterable extends T> data,
final Random rng) {
final ImmutableList dataAsList = ImmutableList.copyOf(data);
return new ShufflingCollection(ShufflingIterable.from(dataAsList, rng),
dataAsList.size());
}
@Override
public Iterator iterator() {
return data.iterator();
}
@Override
public int size() {
return size;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy