This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git
The following commit(s) were added to refs/heads/master by this push:
new 751a9868c remove stale key from OrderedProperties on compute/merge to
null (#702)
751a9868c is described below
commit 751a9868cb995423355ffe2d76c90109df20a7bf
Author: Dexter.k <[email protected]>
AuthorDate: Sat Jul 4 18:29:54 2026 +0000
remove stale key from OrderedProperties on compute/merge to null (#702)
---
.../collections4/properties/OrderedProperties.java | 11 +++++++--
.../properties/OrderedPropertiesTest.java | 27 ++++++++++++++++++++++
2 files changed, 36 insertions(+), 2 deletions(-)
diff --git
a/src/main/java/org/apache/commons/collections4/properties/OrderedProperties.java
b/src/main/java/org/apache/commons/collections4/properties/OrderedProperties.java
index 3252b61f7..4bd21c188 100644
---
a/src/main/java/org/apache/commons/collections4/properties/OrderedProperties.java
+++
b/src/main/java/org/apache/commons/collections4/properties/OrderedProperties.java
@@ -66,6 +66,8 @@ public class OrderedProperties extends Properties {
final Object compute = super.compute(key, remappingFunction);
if (compute != null) {
orderedKeys.add(key);
+ } else {
+ orderedKeys.remove(key);
}
return compute;
}
@@ -123,8 +125,13 @@ public class OrderedProperties extends Properties {
@Override
public synchronized Object merge(final Object key, final Object value,
final BiFunction<? super Object, ? super Object, ? extends Object>
remappingFunction) {
- orderedKeys.add(key);
- return super.merge(key, value, remappingFunction);
+ final Object merge = super.merge(key, value, remappingFunction);
+ if (merge != null) {
+ orderedKeys.add(key);
+ } else {
+ orderedKeys.remove(key);
+ }
+ return merge;
}
@Override
diff --git
a/src/test/java/org/apache/commons/collections4/properties/OrderedPropertiesTest.java
b/src/test/java/org/apache/commons/collections4/properties/OrderedPropertiesTest.java
index 9d44952a4..816387625 100644
---
a/src/test/java/org/apache/commons/collections4/properties/OrderedPropertiesTest.java
+++
b/src/test/java/org/apache/commons/collections4/properties/OrderedPropertiesTest.java
@@ -119,6 +119,20 @@ class OrderedPropertiesTest {
assertDescendingOrder(orderedProperties);
}
+ @Test
+ void testComputeToNullRemovesKey() {
+ final OrderedProperties orderedProperties = new OrderedProperties();
+ orderedProperties.put("a", "1");
+ orderedProperties.put("b", "2");
+ // A remapping to null removes the mapping; the ordered key view must
follow.
+ orderedProperties.compute("a", (k, v) -> null);
+ assertFalse(orderedProperties.containsKey("a"));
+ assertFalse(orderedProperties.keySet().contains("a"));
+ assertEquals(1, orderedProperties.size());
+ assertEquals("[b]", orderedProperties.keySet().toString());
+ assertEquals("{b=2}", orderedProperties.toString());
+ }
+
@Test
void testComputeIfAbsent() {
final OrderedProperties orderedProperties = new OrderedProperties();
@@ -217,6 +231,19 @@ class OrderedPropertiesTest {
assertDescendingOrder(orderedProperties);
}
+ @Test
+ void testMergeToNullRemovesKey() {
+ final OrderedProperties orderedProperties = new OrderedProperties();
+ orderedProperties.put("a", "1");
+ orderedProperties.put("b", "2");
+ // A remapping to null removes the mapping; the ordered key view must
follow.
+ orderedProperties.merge("a", "x", (oldVal, newVal) -> null);
+ assertFalse(orderedProperties.containsKey("a"));
+ assertFalse(orderedProperties.keySet().contains("a"));
+ assertEquals(1, orderedProperties.size());
+ assertEquals("{b=2}", orderedProperties.toString());
+ }
+
@Test
void testPut() {
final OrderedProperties orderedProperties = new OrderedProperties();