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

net.ericaro.neoitertools.generators.RangeGenerator Maven / Gradle / Ivy

The newest version!
package net.ericaro.neoitertools.generators;

import java.security.InvalidParameterException;
import java.util.NoSuchElementException;

import net.ericaro.neoitertools.Generator;

/**
 * This is a versatile Generator containing arithmetic progressions. It is most
 * often used in for loops. The full form returns an iterator over Integers
 * [start, start + step, start + 2 * step, ...].
 * 
    *
  • If step is positive, the last element is the largest start + i * step * less than stop;
  • *
  • if step is negative, the last element is the smallest start + i * step * greater than stop.
  • *
  • step must not be zero (or else InvalidParameterException is raised).
  • * Example: * * @author eric * @see * RangeGenerator's wiki page * @see neoitertools site * */ public class RangeGenerator implements Generator { private final int step; private final int end; int i; public RangeGenerator(int start, int end) { this(start, end, 1); } public RangeGenerator(int end) { this(0, end, 1); } public RangeGenerator(int start, int end, int step) { if (step == 0) throw new InvalidParameterException("step must be != 0"); this.step = step; this.end = end; i = start; } public Integer next() { if (step > 0 ? i >= end : i <= end) throw new NoSuchElementException(); int j = i; i += step; return j; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy