Skip to content

Commit

Permalink
Must override toString() for Java 17
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Jul 13, 2023
1 parent 21c8c53 commit f59447d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand Down Expand Up @@ -145,4 +146,28 @@ public synchronized boolean remove(final Object key, final Object value) {
}
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(", ");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ public void testRemoveKeyValue() throws FileNotFoundException, IOException {
assertFalse(Collections.list(props.propertyNames()).contains(k));
}

@Disabled("Fails on GHA?")
@Test
public void testToString() {
final OrderedProperties orderedProperties = new OrderedProperties();
Expand Down

0 comments on commit f59447d

Please sign in to comment.