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 77266eaf61024355e5948ffb1c930623814a97a8 Author: Martin Desruisseaux <martin.desruisse...@geomatys.com> AuthorDate: Tue Feb 15 14:45:42 2022 +0100 Avoid the use of Number wrapper constructors since they are deprecated for removal. The only remaining use is in `NilObject`, which will need more thought. --- .../sis/internal/converter/AngleConverterTest.java | 4 +- .../test/java/org/apache/sis/util/NumbersTest.java | 5 +- .../org/apache/sis/util/collection/CacheTest.java | 18 +++--- .../org/apache/sis/util/collection/IntObject.java | 65 ++++++++++++++++++++++ .../sis/util/collection/WeakHashSetTest.java | 14 ++--- .../sis/util/collection/WeakValueHashMapTest.java | 49 ++++++++-------- 6 files changed, 108 insertions(+), 47 deletions(-) diff --git a/core/sis-utility/src/test/java/org/apache/sis/internal/converter/AngleConverterTest.java b/core/sis-utility/src/test/java/org/apache/sis/internal/converter/AngleConverterTest.java index 4878194..04313e6 100644 --- a/core/sis-utility/src/test/java/org/apache/sis/internal/converter/AngleConverterTest.java +++ b/core/sis-utility/src/test/java/org/apache/sis/internal/converter/AngleConverterTest.java @@ -28,7 +28,7 @@ import static org.apache.sis.test.Assert.*; * Tests the various {@link AngleConverter} implementations. * * @author Martin Desruisseaux (Geomatys) - * @version 0.3 + * @version 1.2 * @since 0.3 * @module */ @@ -42,7 +42,7 @@ public final strictfp class AngleConverterTest extends TestCase { final ObjectConverter<Angle,Double> c1 = AngleConverter.INSTANCE; final ObjectConverter<Double,Angle> c2 = AngleConverter.Inverse.INSTANCE; final Angle v1 = new Angle (30.25); - final Double v2 = new Double(30.25); + final Double v2 = 30.25; assertEquals(v2, c1.apply(v1)); assertEquals(v1, c2.apply(v2)); assertSame(c2, c1.inverse()); diff --git a/core/sis-utility/src/test/java/org/apache/sis/util/NumbersTest.java b/core/sis-utility/src/test/java/org/apache/sis/util/NumbersTest.java index 6bba3aa..f78ec8c 100644 --- a/core/sis-utility/src/test/java/org/apache/sis/util/NumbersTest.java +++ b/core/sis-utility/src/test/java/org/apache/sis/util/NumbersTest.java @@ -30,7 +30,7 @@ import static org.apache.sis.util.Numbers.*; * Tests the {@link Numbers} static methods. * * @author Martin Desruisseaux (Geomatys) - * @version 1.1 + * @version 1.2 * @since 0.3 * @module */ @@ -214,8 +214,7 @@ public final strictfp class NumbersTest extends TestCase { */ @Test public void testCast() { - @SuppressWarnings({"deprecation", "UnnecessaryBoxing"}) - final Integer value = new Integer(10); // Intentionally a new instance. + final Integer value = 10; assertEquals(Byte .valueOf((byte) 10), cast(value, Byte .class)); assertEquals(Short .valueOf((short) 10), cast(value, Short .class)); assertSame (value, cast(value, Integer.class)); diff --git a/core/sis-utility/src/test/java/org/apache/sis/util/collection/CacheTest.java b/core/sis-utility/src/test/java/org/apache/sis/util/collection/CacheTest.java index 6eb8458..37b21b3 100644 --- a/core/sis-utility/src/test/java/org/apache/sis/util/collection/CacheTest.java +++ b/core/sis-utility/src/test/java/org/apache/sis/util/collection/CacheTest.java @@ -44,7 +44,7 @@ import static org.apache.sis.test.Assert.*; * Tests the {@link Cache} with simple tests and a {@linkplain #stress() stress} test. * * @author Martin Desruisseaux (Geomatys) - * @version 0.3 + * @version 1.2 * @since 0.3 * @module */ @@ -187,12 +187,12 @@ public final strictfp class CacheTest extends TestCase { * @param cache the cache to validate. * @return Statistics on the key values of the given map. */ - private static Statistics validateStressEntries(final String name, final Map<Integer,Integer> cache) { + private static Statistics validateStressEntries(final String name, final Map<Integer,IntObject> cache) { final Statistics statistics = new Statistics(name); - for (final Map.Entry<Integer,Integer> entry : cache.entrySet()) { + for (final Map.Entry<Integer,IntObject> entry : cache.entrySet()) { final int key = entry.getKey(); - final int value = entry.getValue(); - assertEquals(key*key, value); + final IntObject value = entry.getValue(); + assertEquals(key*key, value.value); statistics.accept(key); } return statistics; @@ -209,7 +209,7 @@ public final strictfp class CacheTest extends TestCase { @DependsOnMethod("testThreadBlocking") public void stress() throws InterruptedException { final int count = 5000; - final Cache<Integer,Integer> cache = new Cache<>(); + final Cache<Integer,IntObject> cache = new Cache<>(); final AtomicReference<Throwable> failures = new AtomicReference<>(); final class WriterThread extends Thread { /** @@ -232,9 +232,9 @@ public final strictfp class CacheTest extends TestCase { @SuppressWarnings({"UnnecessaryBoxing", "CallToThreadYield", "NumberEquality"}) @Override public void run() { for (int i=0; i<count; i++) { - final Integer key = i; - final Integer expected = new Integer(i * i); // We really want new instance. - final Integer value; + final Integer key = i; + final IntObject expected = new IntObject(i * i); // We really want new instance. + final IntObject value; try { value = cache.getOrCreate(key, () -> expected); assertEquals(expected, value); diff --git a/core/sis-utility/src/test/java/org/apache/sis/util/collection/IntObject.java b/core/sis-utility/src/test/java/org/apache/sis/util/collection/IntObject.java new file mode 100644 index 0000000..355d708 --- /dev/null +++ b/core/sis-utility/src/test/java/org/apache/sis/util/collection/IntObject.java @@ -0,0 +1,65 @@ +/* + * 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.util.collection; + + +/** + * A wrapper for {@code int} value as a Java object (not a value class) and without caching. + * This is used for tests that use {@link java.lang.ref.WeakReference}. + * + * @author Martin Desruisseaux (Geomatys) + * @version 1.2 + * @since 1.2 + * @module + */ +final class IntObject { + /** + * The value. + */ + final int value; + + /** + * Creates a new wrapper for the given value. + */ + IntObject(final int value) { + this.value = value; + } + + /** + * Returns a hash code based on the value. + */ + @Override + public int hashCode() { + return value; + } + + /** + * Compares the given object with this object for equality. + */ + @Override + public boolean equals(final Object other) { + return (other instanceof IntObject) && ((IntObject) other).value == value; + } + + /** + * Returns a string representation of the integer value. + */ + @Override + public String toString() { + return Integer.toString(value); + } +} diff --git a/core/sis-utility/src/test/java/org/apache/sis/util/collection/WeakHashSetTest.java b/core/sis-utility/src/test/java/org/apache/sis/util/collection/WeakHashSetTest.java index 92132ca..c77defa 100644 --- a/core/sis-utility/src/test/java/org/apache/sis/util/collection/WeakHashSetTest.java +++ b/core/sis-utility/src/test/java/org/apache/sis/util/collection/WeakHashSetTest.java @@ -33,7 +33,7 @@ import static org.apache.sis.test.TestUtilities.waitForGarbageCollection; * A standard {@link HashSet} object is used for comparison purpose. * * @author Martin Desruisseaux (IRD, Geomatys) - * @version 0.3 + * @version 1.2 * @since 0.3 * @module */ @@ -58,10 +58,10 @@ public final strictfp class WeakHashSetTest extends TestCase { public void testStrongReferences() { final Random random = new Random(); for (int pass=0; pass<NUM_RETRY; pass++) { - final WeakHashSet<Integer> weakSet = new WeakHashSet<>(Integer.class); - final HashSet<Integer> strongSet = new HashSet<>(); + final WeakHashSet<IntObject> weakSet = new WeakHashSet<>(IntObject.class); + final HashSet<IntObject> strongSet = new HashSet<>(); for (int i=0; i<SAMPLE_SIZE; i++) { - final Integer value = random.nextInt(SAMPLE_SIZE); + final IntObject value = new IntObject(random.nextInt(SAMPLE_SIZE)); if (random.nextBoolean()) { /* * Tests addition. @@ -101,11 +101,11 @@ public final strictfp class WeakHashSetTest extends TestCase { public void testWeakReferences() throws InterruptedException { final Random random = new Random(); for (int pass=0; pass<NUM_RETRY; pass++) { - final WeakHashSet<Integer> weakSet = new WeakHashSet<>(Integer.class); - final HashSet<Integer> strongSet = new HashSet<>(); + final WeakHashSet<IntObject> weakSet = new WeakHashSet<>(IntObject.class); + final HashSet<IntObject> strongSet = new HashSet<>(); for (int i=0; i<SAMPLE_SIZE; i++) { @SuppressWarnings("UnnecessaryBoxing") - final Integer value = new Integer(random.nextInt(SAMPLE_SIZE)); // Really need new instances + final IntObject value = new IntObject(random.nextInt(SAMPLE_SIZE)); // Really need new instances. if (random.nextBoolean()) { /* * Tests addition. diff --git a/core/sis-utility/src/test/java/org/apache/sis/util/collection/WeakValueHashMapTest.java b/core/sis-utility/src/test/java/org/apache/sis/util/collection/WeakValueHashMapTest.java index 92160d8..2053aad 100644 --- a/core/sis-utility/src/test/java/org/apache/sis/util/collection/WeakValueHashMapTest.java +++ b/core/sis-utility/src/test/java/org/apache/sis/util/collection/WeakValueHashMapTest.java @@ -34,7 +34,7 @@ import static org.apache.sis.test.TestUtilities.waitForGarbageCollection; * A standard {@link HashMap} object is used for comparison purpose. * * @author Martin Desruisseaux (IRD, Geomatys) - * @version 0.4 + * @version 1.2 * @since 0.3 * @module */ @@ -66,14 +66,14 @@ public final strictfp class WeakValueHashMapTest extends TestCase { * * @param weakMap the map implementation to test. */ - static void testStrongReferences(final Map<Integer,Integer> weakMap) { + static void testStrongReferences(final Map<Integer,IntObject> weakMap) { final Random random = new Random(); for (int pass=0; pass<NUM_RETRY; pass++) { weakMap.clear(); - final HashMap<Integer,Integer> strongMap = new HashMap<>(); + final HashMap<Integer,IntObject> strongMap = new HashMap<>(); for (int i=0; i<SAMPLE_SIZE; i++) { - final Integer key = random.nextInt(SAMPLE_SIZE); - final Integer value = random.nextInt(SAMPLE_SIZE); + final Integer key = random.nextInt(SAMPLE_SIZE); + final IntObject value = new IntObject(random.nextInt(SAMPLE_SIZE)); assertEquals("containsKey:", strongMap.containsKey(key), weakMap.containsKey(key)); assertEquals("containsValue:", strongMap.containsValue(value), weakMap.containsValue(value)); assertSame ("get:", strongMap.get(key), weakMap.get(key)); @@ -109,23 +109,20 @@ public final strictfp class WeakValueHashMapTest extends TestCase { * @param weakMap the map implementation to test. */ @SuppressWarnings("UnnecessaryBoxing") - static void testWeakReferences(final Map<Integer,Integer> weakMap) throws InterruptedException { + static void testWeakReferences(final Map<Integer,IntObject> weakMap) throws InterruptedException { final Random random = new Random(); for (int pass=0; pass<NUM_RETRY; pass++) { weakMap.clear(); - final HashMap<Integer,Integer> strongMap = new HashMap<>(); + final HashMap<Integer,IntObject> strongMap = new HashMap<>(); for (int i=0; i<SAMPLE_SIZE; i++) { - /* - * We really want new instances here. - */ - final Integer key = new Integer(random.nextInt(SAMPLE_SIZE)); - final Integer value = new Integer(random.nextInt(SAMPLE_SIZE)); + final Integer key = random.nextInt(SAMPLE_SIZE); + final IntObject value = new IntObject(random.nextInt(SAMPLE_SIZE)); // Really need new instances. if (random.nextBoolean()) { /* * Tests addition. */ - final Integer weakPrevious = weakMap .put(key, value); - final Integer strongPrevious = strongMap.put(key, value); + final IntObject weakPrevious = weakMap .put(key, value); + final IntObject strongPrevious = strongMap.put(key, value); if (weakPrevious == null) { /* * The element was not in the WeakValueHashMap, possibly GC collected it. @@ -148,8 +145,8 @@ public final strictfp class WeakValueHashMapTest extends TestCase { /* * Tests remove. */ - final Integer weakPrevious = weakMap.get(key); - final Integer strongPrevious = strongMap.remove(key); + final IntObject weakPrevious = weakMap.get(key); + final IntObject strongPrevious = strongMap.remove(key); if (strongPrevious != null) { assertSame("remove:", strongPrevious, weakPrevious); } @@ -180,11 +177,11 @@ public final strictfp class WeakValueHashMapTest extends TestCase { @Test @DependsOnMethod("testStrongReferences") public void testWithArrayKeys() { - final WeakValueHashMap<int[],Integer> weakMap = new WeakValueHashMap<>(int[].class); + final WeakValueHashMap<int[],IntObject> weakMap = new WeakValueHashMap<>(int[].class); final int[] k1 = new int[] {2, 5, 3}; final int[] k2 = new int[] {2, 5, 4}; - final Integer v1 = 1; - final Integer v2 = 2; + final IntObject v1 = new IntObject(1); + final IntObject v2 = new IntObject(2); assertNull ( weakMap.put(k1, v1)); assertSame (v1, weakMap.put(k1, v1)); assertSame (v1, weakMap.put(k1.clone(), v1)); @@ -204,13 +201,13 @@ public final strictfp class WeakValueHashMapTest extends TestCase { @DependsOnMethod("testStrongReferences") @SuppressWarnings("UnnecessaryBoxing") public void testIdentityComparisons() { - final WeakValueHashMap<Integer,Integer> weakMap = new WeakValueHashMap<>(Integer.class, true); - final Integer k1 = 10; - final Integer k2 = 20; - final Integer k3 = new Integer(10); // Really want a new instance. - final Integer v1 = 1; - final Integer v2 = 2; - final Integer v3 = 3; + final WeakValueHashMap<IntObject,IntObject> weakMap = new WeakValueHashMap<>(IntObject.class, true); + final IntObject k1 = new IntObject(10); + final IntObject k2 = new IntObject(20); + final IntObject k3 = new IntObject(10); // Really want a new instance. + final IntObject v1 = new IntObject(1); + final IntObject v2 = new IntObject(2); + final IntObject v3 = new IntObject(3); assertEquals(k1, k3); // Necessary condition for the test to be valid. assertNull(weakMap.put(k1, v1)); assertSame(v1, weakMap.put(k1, v1)); assertNull(weakMap.put(k2, v2)); assertSame(v2, weakMap.put(k2, v2));