This is an automated email from the ASF dual-hosted git repository. erans pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-math.git
commit 22753536f4c0671a797b9cc1fd4e4c18c7c39fa6 Author: Gilles Sadowski <gillese...@gmail.com> AuthorDate: Sun Oct 17 01:46:06 2021 +0200 Class "JdkMath" bridges user code and alternative implementations. It's a "drop-in" replacement for JDK's "Math" as of Java 8 (cf. MATH-1630). "AccurateMath" contains pure Java, acccurate, implementations of "Math" functions. But it is no longer required to implement all of them. --- .../commons/math4/core/jdkmath/AccurateMath.java | 4 -- .../apache/commons/math4/core/jdkmath/JdkMath.java | 10 ++++ .../commons/math4/core/jdkmath/JdkMathTest.java | 70 ++++++++++++++++++++++ .../legacy/core/jdkmath/AccurateMathTest.java | 32 ---------- 4 files changed, 80 insertions(+), 36 deletions(-) diff --git a/commons-math-core/src/main/java/org/apache/commons/math4/core/jdkmath/AccurateMath.java b/commons-math-core/src/main/java/org/apache/commons/math4/core/jdkmath/AccurateMath.java index 8687030..b4af270 100644 --- a/commons-math-core/src/main/java/org/apache/commons/math4/core/jdkmath/AccurateMath.java +++ b/commons-math-core/src/main/java/org/apache/commons/math4/core/jdkmath/AccurateMath.java @@ -29,10 +29,6 @@ import org.apache.commons.numbers.core.Precision; * to the various JVM optimizations that have appeared since Java 5). * However, any change to this class should ensure that the current * accuracy is not lost. - * <p> - * AccurateMath is a drop-in replacement for both Math and StrictMath. - * For example, a call to {@code Math.sin(x)} can be replaced by a call - * to {@code AccurateMath.sin(x)}. * </p> * <p> * AccurateMath speed is achieved by relying heavily on optimizing compilers diff --git a/commons-math-core/src/main/java/org/apache/commons/math4/core/jdkmath/JdkMath.java b/commons-math-core/src/main/java/org/apache/commons/math4/core/jdkmath/JdkMath.java index 37295c8..6525ed7 100644 --- a/commons-math-core/src/main/java/org/apache/commons/math4/core/jdkmath/JdkMath.java +++ b/commons-math-core/src/main/java/org/apache/commons/math4/core/jdkmath/JdkMath.java @@ -29,7 +29,17 @@ import java.util.function.DoubleSupplier; /** * Wrapper for alternative implementations of {@link Math} functions. + * For example, a call to {@code Math.sin(x)} can be replaced by a call + * to {@code JdkMath.sin(x)}. * + * <p> + * This class is a "drop-in" replacement for both Math and StrictMath, + * up to the <em>minimal</em> JDK version required by this library + * (meaning that, although the library can be used on <em>more</em> + * recent JVMs, the {@code JdkMath} class may be missing the methods + * that were absent in older JDKs). + * + * <p> * Based on the value, at class initialization, of the system property * {@code org.apache.commons.math.jdkmath}, this class redirects to a * specific implementation: diff --git a/commons-math-core/src/test/java/org/apache/commons/math4/core/jdkmath/JdkMathTest.java b/commons-math-core/src/test/java/org/apache/commons/math4/core/jdkmath/JdkMathTest.java new file mode 100644 index 0000000..659d32f --- /dev/null +++ b/commons-math-core/src/test/java/org/apache/commons/math4/core/jdkmath/JdkMathTest.java @@ -0,0 +1,70 @@ +/* + * 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. + */ +package org.apache.commons.math4.core.jdkmath; + +import java.util.List; +import java.util.ArrayList; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.lang.reflect.Type; + +import org.junit.Test; +import org.junit.Assert; + +/** + * Tests for {@link JdkMath}. + */ +public class JdkMathTest { + /** Separator. */ + private static final String LINE_SEP = System.lineSeparator(); + + @Test + public void checkMissingMethods() { + final List<String> notFound = compareClassMethods(StrictMath.class, + JdkMath.class); + if (!notFound.isEmpty()) { + final StringBuilder sb = new StringBuilder(); + sb.append("JdkMath is missing the following StrictMath methods:"); + for (String m : notFound) { + sb.append(LINE_SEP).append(m); + } + Assert.fail(sb.toString()); + } + } + + /** + * @param class1 Reference implementation. + * @param class2 Alternate implementation. + * @return the methods defined in {@code class1} that are not in {@code class2}. + */ + private List<String> compareClassMethods(Class<?> class1, + Class<?> class2) { + final List<String> notFound = new ArrayList<>(); + for (Method method1 : class1.getDeclaredMethods()) { + if (Modifier.isPublic(method1.getModifiers())) { + final Type[] params = method1.getGenericParameterTypes(); + try { + class2.getDeclaredMethod(method1.getName(), (Class[]) params); + } catch (NoSuchMethodException e) { + notFound.add(method1.toString()); + } + } + } + + return notFound; + } +} diff --git a/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/jdkmath/AccurateMathTest.java b/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/jdkmath/AccurateMathTest.java index fbfe40c..1c7b8c3 100644 --- a/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/jdkmath/AccurateMathTest.java +++ b/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/jdkmath/AccurateMathTest.java @@ -20,16 +20,12 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.lang.reflect.Type; import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; import org.junit.Assert; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.jupiter.api.Assertions; import org.apache.commons.numbers.core.ArithmeticUtils; @@ -1296,34 +1292,6 @@ public class AccurateMathTest { assertEquals(Float.NEGATIVE_INFINITY, AccurateMath.scalb(-3.4028235E38f, 2147483647), 0F); } - private boolean compareClassMethods(Class<?> class1, Class<?> class2) { - boolean allfound = true; - for (Method method1 : class1.getDeclaredMethods()) { - if (Modifier.isPublic(method1.getModifiers())) { - Type[] params = method1.getGenericParameterTypes(); - try { - class2.getDeclaredMethod(method1.getName(), (Class[]) params); - } catch (NoSuchMethodException e) { - allfound = false; - System.out.println(class2.getSimpleName() + " does not implement: " + method1); - } - } - } - return allfound; - } - - @Test - public void checkMissingAccurateMathClasses() { - boolean ok = compareClassMethods(StrictMath.class, AccurateMath.class); - assertTrue("AccurateMath should implement all StrictMath methods", ok); - } - - @Ignore - @Test - public void checkExtraAccurateMathClasses() { - compareClassMethods(AccurateMath.class, StrictMath.class); - } - @Test public void testSignumDouble() { final double delta = 0.0;