incorporate review comments for BAEL-747 (#1603)

This commit is contained in:
Mohamed Sanaulla 2017-04-07 17:31:39 +03:00 committed by maibin
parent 69965db187
commit d87e0663fc
8 changed files with 26 additions and 20 deletions

View File

@ -2,10 +2,10 @@ package com.baeldung.algorithms.primechecker;
import java.math.BigInteger; import java.math.BigInteger;
public class BigIntegerPrimeChecker implements PrimeChecker{ public class BigIntegerPrimeChecker implements PrimeChecker<Long>{
@Override @Override
public boolean isPrime(int number) { public boolean isPrime(Long number) {
BigInteger bigInt = BigInteger.valueOf(number); BigInteger bigInt = BigInteger.valueOf(number);
return bigInt.isProbablePrime(100); return bigInt.isProbablePrime(100);
} }

View File

@ -1,12 +1,15 @@
package com.baeldung.algorithms.primechecker; package com.baeldung.algorithms.primechecker;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class BruteForcePrimeChecker implements PrimeChecker{ public class BruteForcePrimeChecker implements PrimeChecker<Integer>{
@Override @Override
public boolean isPrime(int number) { public boolean isPrime(Integer number) {
return IntStream.range(2, number).noneMatch(n -> (number % n == 0));
return number > 2 ? IntStream.range(2, number)
.noneMatch(n -> (number % n == 0)) : false;
} }

View File

@ -1,13 +1,14 @@
package com.baeldung.algorithms.primechecker; package com.baeldung.algorithms.primechecker;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class OptimisedPrimeChecker implements PrimeChecker{ public class OptimisedPrimeChecker implements PrimeChecker<Integer>{
@Override @Override
public boolean isPrime(int number) { public boolean isPrime(Integer number) {
return IntStream.range(2, (int)Math.sqrt(number) + 1) return number > 2 ? IntStream.rangeClosed(2, (int) Math.sqrt(number))
.noneMatch(n -> (number % n == 0)); .noneMatch(n -> (number % n == 0)) : false;
} }

View File

@ -1,6 +1,6 @@
package com.baeldung.algorithms.primechecker; package com.baeldung.algorithms.primechecker;
public interface PrimeChecker { public interface PrimeChecker <T> {
public boolean isPrime( int number ); public boolean isPrime( T number );
} }

View File

@ -2,10 +2,10 @@ package com.baeldung.algorithms.primechecker;
import org.apache.commons.math3.primes.Primes; import org.apache.commons.math3.primes.Primes;
public class PrimesPrimeChecker implements PrimeChecker{ public class PrimesPrimeChecker implements PrimeChecker<Integer>{
@Override @Override
public boolean isPrime(int number) { public boolean isPrime(Integer number) {
return Primes.isPrime(number); return Primes.isPrime(number);
} }

View File

@ -9,18 +9,20 @@ import com.baeldung.algorithms.primechecker.PrimeChecker;
public class BigIntegerPrimeCheckerTest { public class BigIntegerPrimeCheckerTest {
PrimeChecker primeChecker = new BigIntegerPrimeChecker(); BigIntegerPrimeChecker primeChecker = new BigIntegerPrimeChecker();
@Test @Test
public void givenPrimeNumber_whenCheckIsPrime_thenTrue(){ public void givenPrimeNumber_whenCheckIsPrime_thenTrue(){
assertTrue(primeChecker.isPrime(13)); assertTrue(primeChecker.isPrime(13l));
assertTrue(primeChecker.isPrime(1009)); assertTrue(primeChecker.isPrime(1009L));
assertTrue(primeChecker.isPrime(74207281L));
} }
@Test @Test
public void givenNonPrimeNumber_whenCheckIsPrime_thenFalse(){ public void givenNonPrimeNumber_whenCheckIsPrime_thenFalse(){
assertTrue(!primeChecker.isPrime(50)); assertTrue(!primeChecker.isPrime(50L));
assertTrue(!primeChecker.isPrime(1001)); assertTrue(!primeChecker.isPrime(1001L));
assertTrue(!primeChecker.isPrime(74207282L));
} }
} }

View File

@ -9,7 +9,7 @@ import com.baeldung.algorithms.primechecker.PrimeChecker;
public class OptimisedPrimeCheckerTest { public class OptimisedPrimeCheckerTest {
PrimeChecker primeChecker = new OptimisedPrimeChecker(); OptimisedPrimeChecker primeChecker = new OptimisedPrimeChecker();
@Test @Test
public void givenPrimeNumber_whenCheckIsPrime_thenTrue(){ public void givenPrimeNumber_whenCheckIsPrime_thenTrue(){

View File

@ -8,7 +8,7 @@ import com.baeldung.algorithms.primechecker.PrimeChecker;
import com.baeldung.algorithms.primechecker.PrimesPrimeChecker; import com.baeldung.algorithms.primechecker.PrimesPrimeChecker;
public class PrimesPrimeCheckerTest { public class PrimesPrimeCheckerTest {
PrimeChecker primeChecker = new PrimesPrimeChecker(); PrimesPrimeChecker primeChecker = new PrimesPrimeChecker();
@Test @Test
public void givenPrimeNumber_whenCheckIsPrime_thenTrue() { public void givenPrimeNumber_whenCheckIsPrime_thenTrue() {