Class Sieve

  • All Implemented Interfaces:
    java.lang.Iterable<java.lang.Integer>, java.util.Enumeration<java.lang.Integer>, java.util.Iterator<java.lang.Integer>

    public class Sieve
    extends java.lang.Object
    implements java.lang.Iterable<java.lang.Integer>, java.util.Iterator<java.lang.Integer>, java.util.Enumeration<java.lang.Integer>
    This class is a generator of primes using the "Sieve of Eratosthenes".
    Since:
    19.07.2015 22:36:14
    Author:
    Nico Danneberg
    See Also:
    Sieve of Eratosthenes @ Wikipedia, Enumeration, Iterable, Iterator
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int current
      The current prime found in the sieve.
      private int[] data
      A local storages that works as the sieve.
      private int max
      The upper border of the sieve.
    • Constructor Summary

      Constructors 
      Constructor Description
      Sieve​(int max)
      Creates a new sieve with the given maximum range.
    • Method Summary

      Modifier and Type Method Description
      java.util.Enumeration<java.lang.Integer> elements()
      Resets the current sieve and returns a reference as Enumeration.
      boolean hasMoreElements()
      Checks if there are more primes in the sieve in relation to the current position in the collection of all primes.
      boolean hasNext()
      Checks if there are more primes in the sieve in relation to the current position in the collection of all primes.
      java.util.Iterator<java.lang.Integer> iterator()
      Resets the current sieve and returns a reference as Iterator.
      java.lang.Integer next()
      Tries to find the next prime in the sieve.
      java.lang.Integer nextElement()
      Tries to find the next prime in the sieve.
      void reset()
      Resets the sieve to its initial values.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Enumeration

        asIterator
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
      • Methods inherited from interface java.util.Iterator

        forEachRemaining, remove
    • Field Detail

      • data

        private int[] data
        A local storages that works as the sieve.
      • current

        private int current
        The current prime found in the sieve.
      • max

        private int max
        The upper border of the sieve.
    • Constructor Detail

      • Sieve

        public Sieve​(int max)
        Creates a new sieve with the given maximum range.
        Parameters:
        max - the size of this sieve / the maximum possible prime
    • Method Detail

      • elements

        public java.util.Enumeration<java.lang.Integer> elements()
        Resets the current sieve and returns a reference as Enumeration.
        Returns:
        An enumeration over all primes in this sieve.
        See Also:
        Enumeration
      • hasMoreElements

        public boolean hasMoreElements()
        Checks if there are more primes in the sieve in relation to the current position in the collection of all primes.
        Specified by:
        hasMoreElements in interface java.util.Enumeration<java.lang.Integer>
        Returns:
        true, if there is at least one more prime
        See Also:
        hasNext()
      • hasNext

        public boolean hasNext()
        Checks if there are more primes in the sieve in relation to the current position in the collection of all primes.
        Specified by:
        hasNext in interface java.util.Iterator<java.lang.Integer>
        Returns:
        true, if there is at least one more prime
        See Also:
        hasMoreElements()
      • iterator

        public java.util.Iterator<java.lang.Integer> iterator()
        Resets the current sieve and returns a reference as Iterator.
        Specified by:
        iterator in interface java.lang.Iterable<java.lang.Integer>
        Returns:
        An iterator over all primes in this sieve.
        See Also:
        Iterator
      • next

        public java.lang.Integer next()
                               throws java.util.NoSuchElementException
        Tries to find the next prime in the sieve.
        Specified by:
        next in interface java.util.Iterator<java.lang.Integer>
        Returns:
        The next prime in the list of elements in this sieve beginning at 2. If the end of all elements is reached an exception is thrown.
        Throws:
        java.util.NoSuchElementException
        See Also:
        nextElement()
      • nextElement

        public java.lang.Integer nextElement()
                                      throws java.util.NoSuchElementException
        Tries to find the next prime in the sieve.
        Specified by:
        nextElement in interface java.util.Enumeration<java.lang.Integer>
        Returns:
        The next prime in the list of elements in this sieve beginning at 2. If the end of all elements is reached an exception is thrown.
        Throws:
        java.util.NoSuchElementException
        See Also:
        next()
      • reset

        public void reset()
        Resets the sieve to its initial values.