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

edu.isi.nlp.collections.ShufflingCollection Maven / Gradle / Ivy

The newest version!
package edu.isi.nlp.collections;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.collect.ImmutableList;
import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Iterator;
import java.util.Random;

/**
 * A collection whose iteration order is randomized on every attempt to iterate over it.
 *
 * @author Ryan Gabbard
 */
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 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 - 2025 Weber Informatics LLC | Privacy Policy