You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2024/04/30 10:02:52 UTC
(sis) 01/02: Add a place-holder for JDK 15 methods, and make the intend a little bit clearer in Fraction.
This is an automated email from the ASF dual-hosted git repository.
desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git
commit c453e8869a3560fc2172eeeb8dfd5c786ee6aa8e
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Mon Apr 29 15:07:10 2024 +0200
Add a place-holder for JDK 15 methods, and make the intend a little bit clearer in Fraction.
---
.../sis/storage/geotiff/writer/GeoEncoder.java | 4 +-
.../main/org/apache/sis/math/Fraction.java | 42 +++++++++----------
.../main/org/apache/sis/pending/jdk/JDK15.java | 48 ++++++++++++++++++++++
.../main/org/apache/sis/util/privy/Strings.java | 3 +-
4 files changed, 71 insertions(+), 26 deletions(-)
diff --git a/endorsed/src/org.apache.sis.storage.geotiff/main/org/apache/sis/storage/geotiff/writer/GeoEncoder.java b/endorsed/src/org.apache.sis.storage.geotiff/main/org/apache/sis/storage/geotiff/writer/GeoEncoder.java
index 292b17d116..9658dcbe1b 100644
--- a/endorsed/src/org.apache.sis.storage.geotiff/main/org/apache/sis/storage/geotiff/writer/GeoEncoder.java
+++ b/endorsed/src/org.apache.sis.storage.geotiff/main/org/apache/sis/storage/geotiff/writer/GeoEncoder.java
@@ -69,6 +69,7 @@ import org.apache.sis.storage.geotiff.base.GeoCodes;
import org.apache.sis.storage.geotiff.base.Resources;
import org.apache.sis.storage.event.StoreListeners;
import org.apache.sis.metadata.iso.citation.Citations;
+import org.apache.sis.pending.jdk.JDK15;
/**
@@ -677,8 +678,7 @@ public final class GeoEncoder {
* {@return the values to write in the "GeoTIFF ASCII strings" tag}.
*/
public List<String> asciiParams() {
- if (asciiParams.length() == 0) return null; // TODO: replace by isEmpty() with JDK15.
- return List.of(asciiParams.toString());
+ return JDK15.isEmpty(asciiParams) ? null : List.of(asciiParams.toString());
}
/**
diff --git a/endorsed/src/org.apache.sis.util/main/org/apache/sis/math/Fraction.java b/endorsed/src/org.apache.sis.util/main/org/apache/sis/math/Fraction.java
index 5dda60a894..9563bc4d9d 100644
--- a/endorsed/src/org.apache.sis.util/main/org/apache/sis/math/Fraction.java
+++ b/endorsed/src/org.apache.sis.util/main/org/apache/sis/math/Fraction.java
@@ -17,10 +17,12 @@
package org.apache.sis.math;
import java.io.Serializable;
+import static java.lang.Math.multiplyFull;
import org.apache.sis.util.ArgumentChecks;
import org.apache.sis.util.resources.Errors;
import org.apache.sis.util.collection.WeakHashSet;
import org.apache.sis.util.privy.Numerics;
+import org.apache.sis.pending.jdk.JDK15;
import static org.apache.sis.pending.jdk.JDK19.DOUBLE_PRECISION;
@@ -239,16 +241,12 @@ public final class Fraction extends Number implements Comparable<Fraction>, Seri
* Above result still slightly smaller in magnitude than {@code Long.MIN_VALUE}.
*/
private static Fraction simplify(final Fraction f, long num, long den) {
- if (num == Long.MIN_VALUE || den == Long.MIN_VALUE) {
- // TODO: replace by use of Math.absExact(…) in JDK15.
- throw new ArithmeticException(Errors.format(Errors.Keys.IntegerOverflow_1, Long.SIZE));
- }
if (num == 0) {
den = Long.signum(den); // Simplify 0/x as 0/±1 or 0/0.
} else if (den == 0) {
- num = Long.signum(num); // Simplify x/0 as ±1/0
+ num = Long.signum(num); // Simplify x/0 as ±1/0.
} else if (den % num == 0) {
- den /= num; // Simplify x/xy as 1/y
+ den /= num; // Simplify x/xy as 1/y.
if (den < 0) {
den = -den; // Math.negateExact(long) not needed - see javadoc.
num = -1;
@@ -256,11 +254,11 @@ public final class Fraction extends Number implements Comparable<Fraction>, Seri
num = 1;
}
} else {
- long a = Math.abs(num);
- long gcd = Math.abs(den);
+ long a = JDK15.absExact(num);
+ long gcd = JDK15.absExact(den);
long remainder = a % gcd;
if (remainder == 0) {
- num /= den; // Simplify xy/x as y/1
+ num /= den; // Simplify xy/x as y/1.
den = 1;
} else {
do { // Search for greatest common divisor with Euclid's algorithm.
@@ -326,10 +324,9 @@ public final class Fraction extends Number implements Comparable<Fraction>, Seri
* @throws ArithmeticException if the result overflows.
*/
public Fraction add(final Fraction other) {
- // Intermediate result must be computed in a type wider that the 'numerator' and 'denominator' type.
- final long td = this .denominator;
- final long od = other.denominator;
- return simplify(this, Math.addExact(od * numerator, td * other.numerator), od * td);
+ long a = multiplyFull(other.denominator, numerator);
+ long b = multiplyFull(denominator, other.numerator);
+ return simplify(this, Math.addExact(a, b), multiplyFull(other.denominator, denominator));
}
/**
@@ -342,10 +339,9 @@ public final class Fraction extends Number implements Comparable<Fraction>, Seri
* @see #negate()
*/
public Fraction subtract(final Fraction other) {
- // Intermediate result must be computed in a type wider that the 'numerator' and 'denominator' type.
- final long td = this .denominator;
- final long od = other.denominator;
- return simplify(this, Math.subtractExact(od * numerator, td * other.numerator), od * td);
+ long a = multiplyFull(other.denominator, numerator);
+ long b = multiplyFull(denominator, other.numerator);
+ return simplify(this, Math.subtractExact(a, b), multiplyFull(other.denominator, denominator));
}
/**
@@ -356,8 +352,8 @@ public final class Fraction extends Number implements Comparable<Fraction>, Seri
* @throws ArithmeticException if the result overflows.
*/
public Fraction multiply(final Fraction other) {
- return simplify(this, Math.multiplyFull(numerator, other.numerator),
- Math.multiplyFull(denominator, other.denominator));
+ return simplify(this, multiplyFull(numerator, other.numerator),
+ multiplyFull(denominator, other.denominator));
}
/**
@@ -370,8 +366,8 @@ public final class Fraction extends Number implements Comparable<Fraction>, Seri
* @see #inverse()
*/
public Fraction divide(final Fraction other) {
- return simplify(this, Math.multiplyFull(numerator, other.denominator),
- Math.multiplyFull(denominator, other.numerator));
+ return simplify(this, multiplyFull(numerator, other.denominator),
+ multiplyFull(denominator, other.numerator));
}
/**
@@ -553,8 +549,8 @@ public final class Fraction extends Number implements Comparable<Fraction>, Seri
*/
@Override
public int compareTo(final Fraction other) {
- return Long.signum(Math.multiplyFull(numerator, other.denominator)
- - Math.multiplyFull(other.numerator, denominator));
+ return Long.signum(multiplyFull(numerator, other.denominator)
+ - multiplyFull(other.numerator, denominator));
}
/**
diff --git a/endorsed/src/org.apache.sis.util/main/org/apache/sis/pending/jdk/JDK15.java b/endorsed/src/org.apache.sis.util/main/org/apache/sis/pending/jdk/JDK15.java
new file mode 100644
index 0000000000..87fd71dc18
--- /dev/null
+++ b/endorsed/src/org.apache.sis.util/main/org/apache/sis/pending/jdk/JDK15.java
@@ -0,0 +1,48 @@
+/*
+ * 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.sis.pending.jdk;
+
+
+/**
+ * Place holder for some functionalities defined in a JDK more recent than Java 11.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ */
+public final class JDK15 {
+ /**
+ * Do not allow instantiation of this class.
+ */
+ private JDK15() {
+ }
+
+ /**
+ * Placeholder for {@link Math#absExact(int)}.
+ */
+ public static long absExact(long value) {
+ if (value == Long.MIN_VALUE) {
+ throw new ArithmeticException();
+ }
+ return Math.abs(value);
+ }
+
+ /**
+ * Placeholder for {@link CharSequence#isEmpty()}.
+ */
+ public static boolean isEmpty(CharSequence s) {
+ return s.length() == 0;
+ }
+}
diff --git a/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/privy/Strings.java b/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/privy/Strings.java
index b25d5eadf3..8a62a55846 100644
--- a/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/privy/Strings.java
+++ b/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/privy/Strings.java
@@ -24,6 +24,7 @@ import org.apache.sis.util.Classes;
import org.apache.sis.util.ArraysExt;
import org.apache.sis.util.Characters;
import org.apache.sis.util.CharSequences;
+import org.apache.sis.pending.jdk.JDK15;
/**
@@ -292,7 +293,7 @@ public final class Strings extends Static {
int count = 0;
for (int i=0; i<lines.length; i++) {
CharSequence line = CharSequences.trimWhitespaces(lines[i]);
- if (line.length() != 0) lines[count++] = line; // TODO: use !line.isEmpty() with JDK15.
+ if (!JDK15.isEmpty(line)) lines[count++] = line;
}
switch (count) {
case 0: break;