NUMBERS-20: Port classes to new package. Replace [math] exceptions with IllegalArgumentException. Simplify exception detection in unit tests.
Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/09dc0163 Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/09dc0163 Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/09dc0163 Branch: refs/heads/master Commit: 09dc0163ea14b0bb247d8a4fd0ea17dac9938fa2 Parents: 996a3a4 Author: Ray DeCampo <r...@decampo.org> Authored: Sun Apr 16 13:33:16 2017 -0400 Committer: Ray DeCampo <r...@decampo.org> Committed: Thu Apr 20 18:24:13 2017 -0400 ---------------------------------------------------------------------- commons-numbers-primes/pom.xml | 45 ++++++++++++++++++++ .../apache/commons/numbers/primes/Primes.java | 21 +++++---- .../commons/numbers/primes/SmallPrimes.java | 8 ++-- .../commons/numbers/primes/package-info.java | 2 +- .../commons/numbers/primes/PrimesTest.java | 35 ++++++--------- pom.xml | 1 + 6 files changed, 75 insertions(+), 37 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/09dc0163/commons-numbers-primes/pom.xml ---------------------------------------------------------------------- diff --git a/commons-numbers-primes/pom.xml b/commons-numbers-primes/pom.xml new file mode 100644 index 0000000..758afa3 --- /dev/null +++ b/commons-numbers-primes/pom.xml @@ -0,0 +1,45 @@ +<?xml version="1.0"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" + xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.commons</groupId> + <artifactId>commons-numbers-parent</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> + + <groupId>org.apache.commons</groupId> + <artifactId>commons-numbers-primes</artifactId> + <version>1.0-SNAPSHOT</version> + <name>Apache Commons Numbers Primes</name> + + <description>Utilities related to prime numbers.</description> + + <properties> + <!-- This value must reflect the current name of the base package. --> + <commons.osgi.symbolicName>org.apache.commons.numbers.primes</commons.osgi.symbolicName> + <!-- OSGi --> + <commons.osgi.export>org.apache.commons.numbers.primes</commons.osgi.export> + <!-- Workaround to avoid duplicating config files. --> + <numbers.parent.dir>${basedir}/..</numbers.parent.dir> + </properties> + +</project> http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/09dc0163/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/Primes.java ---------------------------------------------------------------------- diff --git a/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/Primes.java b/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/Primes.java index db75ccc..b47461e 100644 --- a/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/Primes.java +++ b/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/Primes.java @@ -14,11 +14,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.commons.math4.primes; - -import org.apache.commons.math4.exception.MathIllegalArgumentException; -import org.apache.commons.math4.exception.util.LocalizedFormats; +package org.apache.commons.numbers.primes; +import java.text.MessageFormat; import java.util.List; @@ -30,10 +28,13 @@ import java.util.List; * <li>factorization</li> * </ul> * - * @since 3.2 + * @since 1.0 */ public class Primes { + /** Exception message format when an argument is too small. */ + static final String NUMBER_TOO_SMALL = "{0} is smaller than the minimum ({1})"; + /** * Hide utility class. */ @@ -68,11 +69,12 @@ public class Primes { * * @param n a positive number. * @return the smallest prime greater than or equal to n. - * @throws MathIllegalArgumentException if n < 0. + * @throws IllegalArgumentException if n < 0. */ public static int nextPrime(int n) { if (n < 0) { - throw new MathIllegalArgumentException(LocalizedFormats.NUMBER_TOO_SMALL, n, 0); + throw new IllegalArgumentException( + MessageFormat.format(NUMBER_TOO_SMALL, n, 0)); } if (n == 2) { return 2; @@ -112,12 +114,13 @@ public class Primes { * * @param n number to factorize: must be ≥ 2 * @return list of prime factors of n - * @throws MathIllegalArgumentException if n < 2. + * @throws IllegalArgumentException if n < 2. */ public static List<Integer> primeFactors(int n) { if (n < 2) { - throw new MathIllegalArgumentException(LocalizedFormats.NUMBER_TOO_SMALL, n, 2); + throw new IllegalArgumentException( + MessageFormat.format(NUMBER_TOO_SMALL, n, 2)); } return SmallPrimes.trialDivision(n); http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/09dc0163/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/SmallPrimes.java ---------------------------------------------------------------------- diff --git a/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/SmallPrimes.java b/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/SmallPrimes.java index 6f329e3..e92b7e6 100644 --- a/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/SmallPrimes.java +++ b/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/SmallPrimes.java @@ -14,15 +14,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.commons.math4.primes; +package org.apache.commons.numbers.primes; import java.math.BigInteger; import java.util.ArrayList; import java.util.List; -import org.apache.commons.math4.util.FastMath; - /** * Utility methods to work on primes within the <code>int</code> range. * @since 3.2 @@ -125,13 +123,13 @@ class SmallPrimes { * @return the list of prime factors of n */ public static List<Integer> trialDivision(int n){ - final List<Integer> factors = new ArrayList<>(32); + final List<Integer> factors = new ArrayList<Integer>(32); n = smallTrialDivision(n, factors); if (1 == n) { return factors; } // here we are sure that n is either a prime or a semi prime - final int bound = (int) FastMath.sqrt(n); + final int bound = (int) Math.sqrt(n); boundedTrialDivision(n, bound, factors); return factors; } http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/09dc0163/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/package-info.java ---------------------------------------------------------------------- diff --git a/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/package-info.java b/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/package-info.java index 076ec31..9330b83 100644 --- a/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/package-info.java +++ b/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/package-info.java @@ -17,4 +17,4 @@ /** * Methods related to prime numbers like primality test, factor decomposition. */ -package org.apache.commons.math4.primes; +package org.apache.commons.numbers.primes; http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/09dc0163/commons-numbers-primes/src/test/java/org/apache/commons/numbers/primes/PrimesTest.java ---------------------------------------------------------------------- diff --git a/commons-numbers-primes/src/test/java/org/apache/commons/numbers/primes/PrimesTest.java b/commons-numbers-primes/src/test/java/org/apache/commons/numbers/primes/PrimesTest.java index 831a86e..983dd11 100644 --- a/commons-numbers-primes/src/test/java/org/apache/commons/numbers/primes/PrimesTest.java +++ b/commons-numbers-primes/src/test/java/org/apache/commons/numbers/primes/PrimesTest.java @@ -14,16 +14,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.commons.math4.primes; +package org.apache.commons.numbers.primes; +import java.text.MessageFormat; import java.util.HashSet; import java.util.List; -import org.apache.commons.math4.exception.MathIllegalArgumentException; -import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.primes.Primes; -import org.apache.commons.math4.primes.SmallPrimes; import org.junit.Assert; import org.junit.Test; @@ -60,26 +57,20 @@ public class PrimesTest { public static final int[] BELOW_2 = { Integer.MIN_VALUE,-1,0,1}; - void assertPrimeFactorsException(int n, Throwable expected) { + void assertPrimeFactorsException(int n, String expected) { try { Primes.primeFactors(n); Assert.fail("Exception not thrown"); - } catch (Throwable e) { - Assert.assertEquals(expected.getClass(), e.getClass()); - if (expected.getMessage() != null) { - Assert.assertEquals(expected.getMessage(), e.getMessage()); - } + } catch (IllegalArgumentException e) { + Assert.assertEquals(expected, e.getMessage()); } } - void assertNextPrimeException(int n, Throwable expected){ + void assertNextPrimeException(int n, String expected){ try { Primes.nextPrime(n); Assert.fail("Exception not thrown"); - } catch(Throwable e) { - Assert.assertEquals(expected.getClass(), e.getClass()); - if (expected.getMessage() != null) { - Assert.assertEquals(expected.getMessage(), e.getMessage()); - } + } catch(IllegalArgumentException e) { + Assert.assertEquals(expected, e.getMessage()); } } @@ -108,9 +99,9 @@ public class PrimesTest { Assert.assertEquals(Integer.MAX_VALUE, Primes.nextPrime(Integer.MAX_VALUE - 1)); Assert.assertEquals(Integer.MAX_VALUE, Primes.nextPrime(Integer.MAX_VALUE)); - assertNextPrimeException(Integer.MIN_VALUE, new MathIllegalArgumentException(LocalizedFormats.NUMBER_TOO_SMALL,Integer.MIN_VALUE,0)); - assertNextPrimeException(-1, new MathIllegalArgumentException(LocalizedFormats.NUMBER_TOO_SMALL,-1,0)); - assertNextPrimeException(-13, new MathIllegalArgumentException(LocalizedFormats.NUMBER_TOO_SMALL,-13,0)); + assertNextPrimeException(Integer.MIN_VALUE, MessageFormat.format(Primes.NUMBER_TOO_SMALL,Integer.MIN_VALUE,0)); + assertNextPrimeException(-1, MessageFormat.format(Primes.NUMBER_TOO_SMALL,-1,0)); + assertNextPrimeException(-13, MessageFormat.format(Primes.NUMBER_TOO_SMALL,-13,0)); } @Test @@ -140,7 +131,7 @@ public class PrimesTest { } return out; } - static final HashSet<Integer> PRIMES_SET = new HashSet<>(); + static final HashSet<Integer> PRIMES_SET = new HashSet<Integer>(); static { for (int p : PRIMES) { PRIMES_SET.add(p); @@ -157,7 +148,7 @@ public class PrimesTest { @Test public void testPrimeFactors() throws Exception { for (int i : BELOW_2) { - assertPrimeFactorsException(i, new MathIllegalArgumentException(LocalizedFormats.NUMBER_TOO_SMALL,i,2)); + assertPrimeFactorsException(i, MessageFormat.format(Primes.NUMBER_TOO_SMALL,i,2)); } for (int i : NOT_PRIMES) { List<Integer> factors = Primes.primeFactors(i); http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/09dc0163/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 4485994..5403e59 100644 --- a/pom.xml +++ b/pom.xml @@ -560,6 +560,7 @@ <modules> <module>commons-numbers-core</module> <module>commons-numbers-complex</module> + <module>commons-numbers-primes</module> <module>commons-numbers-quaternion</module> <module>commons-numbers-fraction</module> <!-- <module>commons-numbers-continuedfraction</module> -->