This is an automated email from the ASF dual-hosted git repository.

andy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/jena.git


The following commit(s) were added to refs/heads/main by this push:
     new 20d9c7ed0a GH-2802: Remove only if same key
20d9c7ed0a is described below

commit 20d9c7ed0a1e153d5ce6db77a681c560d6ce2576
Author: Andy Seaborne <[email protected]>
AuthorDate: Sun Oct 27 17:37:09 2024 +0000

    GH-2802: Remove only if same key
---
 .../apache/jena/atlas/lib/cache/CacheSimple.java   | 11 +++--------
 .../jena/atlas/lib/cache/TestCacheSimple.java      | 22 ++++++++++++++++++++--
 2 files changed, 23 insertions(+), 10 deletions(-)

diff --git 
a/jena-base/src/main/java/org/apache/jena/atlas/lib/cache/CacheSimple.java 
b/jena-base/src/main/java/org/apache/jena/atlas/lib/cache/CacheSimple.java
index 30a8059dc2..c12997fffe 100644
--- a/jena-base/src/main/java/org/apache/jena/atlas/lib/cache/CacheSimple.java
+++ b/jena-base/src/main/java/org/apache/jena/atlas/lib/cache/CacheSimple.java
@@ -115,19 +115,14 @@ public class CacheSimple<K, V> implements Cache<K, V> {
         }
     }
 
-
     @Override
     public void put(K key, V thing) {
         Objects.requireNonNull(key);
-        final int idx = calcIndex(key);
-        if(thing == null) { //null value causes removal of entry
-            if (keys[idx] != null) {
-                keys[idx] = null;
-                values[idx] = null;
-                currentSize--;
-            }
+        if (thing == null) {
+            remove(key);
             return;
         }
+        final int idx = calcIndex(key);
         if(!thing.equals(values[idx])) {
             values[idx] = thing;
         }
diff --git 
a/jena-base/src/test/java/org/apache/jena/atlas/lib/cache/TestCacheSimple.java 
b/jena-base/src/test/java/org/apache/jena/atlas/lib/cache/TestCacheSimple.java
index 81c0c0f427..a3db7546e0 100644
--- 
a/jena-base/src/test/java/org/apache/jena/atlas/lib/cache/TestCacheSimple.java
+++ 
b/jena-base/src/test/java/org/apache/jena/atlas/lib/cache/TestCacheSimple.java
@@ -32,7 +32,7 @@ public class TestCacheSimple {
 
     /**
      * Simple test to ensure that {@link CacheSimple} evidences
-     * the fixed-size behavior we desire.
+     * the fixed-size behaviour we desire.
      */
     @Test
     public void testFixedSize() {
@@ -208,6 +208,24 @@ public class TestCacheSimple {
         }
     }
 
+    @Test
+    public void testRemoveSameHash() {
+        CompoundKey key1 = new CompoundKey(1, 1);
+        CompoundKey key2 = new CompoundKey(1, 2);
+        String value1 = "v1";
+        String value2 = "v2";
+
+        Cache<CompoundKey, String> cache = new CacheSimple<>(10);
+
+        cache.put(key1, value1);
+        // Delete - different key
+        cache.put(key2, null);
+        String s = cache.getIfPresent(key1);
+
+        assertEquals(1, cache.size());
+        assertEquals(value1, s);
+    }
+
     @Test
     public void testGet() {
         Cache<String, String> cache = new CacheSimple<>(10);
@@ -325,7 +343,7 @@ public class TestCacheSimple {
         assertEquals(16, cache.getAllocatedSize());
     }
 
-    // Compound key for tests. 
+    // Compound key for tests.
     private static final class CompoundKey {
         private final int a;
         private final int b;

Reply via email to