Class PrimesCache


  • public class PrimesCache
    extends java.lang.Object
    Class for generating prime numbers cache using Eratosthenes Sieve.
    Version:
    6.1.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static boolean CACHE_EMPTY
      Empty cache status
      (package private) boolean cacheStatus
      Caching process status
      static boolean CACHING_FINISHED
      Cache ready to use
      (package private) double computingTime
      Time in seconds showing how long did it take to finalize prime numbers caching.
      static int DEFAULT_MAX_NUM_IN_CACHE
      Default range of integer to store in cache
      (package private) boolean initSuccessful
      Internal flag marking that primes cache initialization was successful;
      static int IS_NOT_PRIME
      Indicator if given number is not a prime
      static int IS_PRIME
      Indicator if given number is a prime
      (package private) boolean[] isPrime
      Integers table to store number and indicate whether they are prime or not
      (package private) int maxNumInCache
      Primes between 0 ...
      static int NOT_IN_CACHE
      Indicator that the value is not stored in cache
      (package private) int numberOfPrimes
      Number of cached prime numbers
    • Constructor Summary

      Constructors 
      Constructor Description
      PrimesCache()
      Default constructor - setting prime cache for a default range if integers
      PrimesCache​(int maxNumInCache)
      Constructor - setting prime cache for a given range if integers
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void countPrimes()
      Counting found primes
      private void EratosthenesSieve()
      Eratosthenes Sieve implementation
      boolean getCacheStatus()
      Returns cache status
      double getComputingTime()
      Returns computing time of Eratosthenes Sieve
      int getMaxNumInCache()
      Returns cache range.
      int getNumberOfPrimes()
      Returns number of found primes.
      (package private) boolean[] getPrimes()
      Gets underlying primes cache boolean table
      boolean isInitSuccessful()
      Returns true in case when primes cache initialization was successful, otherwise returns false.
      int primeTest​(int n)
      Check whether given number is prime
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_MAX_NUM_IN_CACHE

        public static final int DEFAULT_MAX_NUM_IN_CACHE
        Default range of integer to store in cache
        See Also:
        Constant Field Values
      • CACHE_EMPTY

        public static final boolean CACHE_EMPTY
        Empty cache status
        See Also:
        Constant Field Values
      • CACHING_FINISHED

        public static final boolean CACHING_FINISHED
        Cache ready to use
        See Also:
        Constant Field Values
      • IS_PRIME

        public static final int IS_PRIME
        Indicator if given number is a prime
        See Also:
        Constant Field Values
      • IS_NOT_PRIME

        public static final int IS_NOT_PRIME
        Indicator if given number is not a prime
        See Also:
        Constant Field Values
      • NOT_IN_CACHE

        public static final int NOT_IN_CACHE
        Indicator that the value is not stored in cache
        See Also:
        Constant Field Values
      • maxNumInCache

        int maxNumInCache
        Primes between 0 ... and ... maximumNumberInCache will be cached
      • numberOfPrimes

        int numberOfPrimes
        Number of cached prime numbers
      • computingTime

        double computingTime
        Time in seconds showing how long did it take to finalize prime numbers caching.
      • cacheStatus

        boolean cacheStatus
        Caching process status
      • isPrime

        boolean[] isPrime
        Integers table to store number and indicate whether they are prime or not
      • initSuccessful

        boolean initSuccessful
        Internal flag marking that primes cache initialization was successful;
    • Constructor Detail

      • PrimesCache

        public PrimesCache()
        Default constructor - setting prime cache for a default range if integers
      • PrimesCache

        public PrimesCache​(int maxNumInCache)
        Constructor - setting prime cache for a given range if integers
        Parameters:
        maxNumInCache - Range of integers to be stored in prime cache
    • Method Detail

      • EratosthenesSieve

        private void EratosthenesSieve()
        Eratosthenes Sieve implementation
      • countPrimes

        private void countPrimes()
        Counting found primes
      • getComputingTime

        public double getComputingTime()
        Returns computing time of Eratosthenes Sieve
        Returns:
        Computing time in seconds
      • getCacheStatus

        public boolean getCacheStatus()
        Returns cache status
        Returns:
        PrimesCache.CACHE_EMPTY or PrimesCache.CACHING_FINISHED;
      • getNumberOfPrimes

        public int getNumberOfPrimes()
        Returns number of found primes.
        Returns:
        Number of found primes.
      • getMaxNumInCache

        public int getMaxNumInCache()
        Returns cache range.
        Returns:
        Maximum integera number in cache/
      • primeTest

        public int primeTest​(int n)
        Check whether given number is prime
        Parameters:
        n - Given integer number.
        Returns:
        PrimesCache.IS_PRIME or PrimesCache.IS_NOT_PRIME or PrimesCache.NOT_IN_CACHE
      • isInitSuccessful

        public boolean isInitSuccessful()
        Returns true in case when primes cache initialization was successful, otherwise returns false.
        Returns:
        Returns true in case when primes cache initialization was successful, otherwise returns false.
      • getPrimes

        boolean[] getPrimes()
        Gets underlying primes cache boolean table
        Returns:
        Underlying primes cache boolean table