Must override toString() for Java 17

This commit is contained in:
Gary Gregory 2023-07-13 09:46:50 -04:00
parent 21c8c53418
commit f59447dd4f
2 changed files with 25 additions and 1 deletions

View File

@ -19,6 +19,7 @@ package org.apache.commons.collections4.properties;
import java.util.AbstractMap.SimpleEntry;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
@ -145,4 +146,28 @@ public class OrderedProperties extends Properties {
}
return remove;
}
@Override
public synchronized String toString() {
// Must override for Java 17 to maintain order since the implementation is based on a map
final int max = size() - 1;
if (max == -1) {
return "{}";
}
final StringBuilder sb = new StringBuilder();
final Iterator<Map.Entry<Object, Object>> it = entrySet().iterator();
sb.append('{');
for (int i = 0;; i++) {
final Map.Entry<Object, Object> e = it.next();
final Object key = e.getKey();
final Object value = e.getValue();
sb.append(key == this ? "(this Map)" : key.toString());
sb.append('=');
sb.append(value == this ? "(this Map)" : value.toString());
if (i == max) {
return sb.append('}').toString();
}
sb.append(", ");
}
}
}

View File

@ -288,7 +288,6 @@ public class OrderedPropertiesTest {
assertFalse(Collections.list(props.propertyNames()).contains(k));
}
@Disabled("Fails on GHA?")
@Test
public void testToString() {
final OrderedProperties orderedProperties = new OrderedProperties();