This class contains logic to check given number if it is prime one by test if number is divisible by any of given divisors.
It has only one method which is called from jobs. Job passes number and divisors range into it. The class divides given number and tests if a reminder is 0. If so then it returns back divisor as a check result.
Full Source Code
GridPrimeChecker.java
package org.gridgain.examples.primenumbers; import org.gridgain.examples.primenumbers.api.*; import org.gridgain.examples.primenumbers.gridify.*; import org.gridgain.grid.gridify.*; public final class GridPrimeChecker { /** * Enforces singleton. */ private GridPrimeChecker() { // No-op. } /** * This method is used by both, {@link GridPrimeExample} and {@link GridifyPrimeExample}, * examples. Whenever invoked from {@link GridifyPrimeExample}, this method will * be executed on the grid by the virtue of {@link Gridify @Gridify} annotation * attached to it. Note that we specify in the annotation that {@link GridifyPrimeTask} * should be used for grid-enabled execution. * * @param val Value to check for prime. * @param minRage Lower boundary of divisors range. * @param maxRange Upper boundary of divisors range. * @return First divisor found or <tt>null</tt> if no divisor was found. */ @Gridify(taskClass = GridifyPrimeTask.class) public static Long checkPrime(long val, long minRage, long maxRange) { // Loop through all divisors in the range and check if the value passed // in is divisible by any of these divisors. // Note that we also check for thread interruption which may happen // if the job was cancelled from the grid task. for (long divisor = minRage; divisor <= maxRange && Thread.currentThread().isInterrupted() == false; divisor++) { if (divisor != 1 && divisor != val && val % divisor == 0) { return divisor; } } return null; } }
