Skip to content

Commit 19119be

Browse files
committed
[COLLECTIONS-877] OrderedProperties.stringPropertyNames() returns an
unordered set
1 parent bed78c1 commit 19119be

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<action type="fix" dev="pkarwasz" due-to="Piotr P. Karwasz, Joerg Budischewski" issue="COLLECTIONS-838">Calling SetUtils.union on multiple instances of SetView causes JVM to hang</action>
3636
<action type="fix" dev="pkarwasz" due-to="Piotr P. Karwasz, Joerg Budischewski" issue="COLLECTIONS-722">Improve IteratorUtils.chainedIterator() performance.</action>
3737
<action type="fix" dev="ggregory" due-to="Gary Gregory, PMD">Fix UselessOverridingMethod in org.apache.commons.collections4.properties.PropertiesFactory.</action>
38+
<action type="fix" dev="ggregory" due-to="Adam Rauch, Gary Gregory" issue="COLLECTIONS-693">OrderedProperties.stringPropertyNames() returns an unordered set.</action>
3839
<!-- ADD -->
3940
<action type="add" dev="ggregory" due-to="Gary Gregory">Add generics to UnmodifiableIterator for the wrapped type.</action>
4041
<!-- UPDATE -->

src/main/java/org/apache/commons/collections4/properties/OrderedProperties.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ public Set<Map.Entry<Object, Object>> entrySet() {
8484
return orderedKeys.stream().map(k -> new SimpleEntry<>(k, get(k))).collect(Collectors.toCollection(LinkedHashSet::new));
8585
}
8686

87+
/**
88+
* Enumerates all key/value pairs in the specified LinkedHashSet and omits the property if the key or value is not a string.
89+
*
90+
* @param result The result set to populate.
91+
* @return The given set.
92+
*/
93+
private synchronized LinkedHashSet<String> enumerateStringProperties(final LinkedHashSet<String> result) {
94+
if (defaults != null) {
95+
result.addAll(defaults.stringPropertyNames());
96+
}
97+
for (final Enumeration<?> e = keys(); e.hasMoreElements();) {
98+
final Object k = e.nextElement();
99+
final Object v = get(k);
100+
if (k instanceof String && v instanceof String) {
101+
result.add((String) k);
102+
}
103+
}
104+
return result;
105+
}
106+
87107
@Override
88108
public synchronized void forEach(final BiConsumer<? super Object, ? super Object> action) {
89109
Objects.requireNonNull(action);
@@ -154,6 +174,11 @@ public synchronized boolean remove(final Object key, final Object value) {
154174
return remove;
155175
}
156176

177+
@Override
178+
public Set<String> stringPropertyNames() {
179+
return enumerateStringProperties(new LinkedHashSet<>());
180+
}
181+
157182
@Override
158183
public synchronized String toString() {
159184
// Must override for Java 17 to maintain order since the implementation is based on a map

src/test/java/org/apache/commons/collections4/properties/OrderedPropertiesTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
2122

2223
import java.io.FileNotFoundException;
2324
import java.io.FileReader;
@@ -27,6 +28,7 @@
2728
import java.util.Iterator;
2829
import java.util.Map;
2930
import java.util.Map.Entry;
31+
import java.util.Set;
3032
import java.util.concurrent.atomic.AtomicInteger;
3133

3234
import org.junit.jupiter.api.Test;
@@ -57,6 +59,9 @@ private void assertAscendingOrder(final OrderedProperties orderedProperties) {
5759
for (int i = first; i <= last; i++) {
5860
assertEquals("key" + i, propertyNames.nextElement());
5961
}
62+
final Set<String> propertyNameSet = orderedProperties.stringPropertyNames();
63+
final AtomicInteger i = new AtomicInteger(first);
64+
propertyNameSet.forEach(e -> assertEquals("key" + i.getAndIncrement(), e));
6065
}
6166

6267
private OrderedProperties assertDescendingOrder(final OrderedProperties orderedProperties) {
@@ -80,6 +85,9 @@ private OrderedProperties assertDescendingOrder(final OrderedProperties orderedP
8085
for (int i = first; i <= last; i--) {
8186
assertEquals("key" + i, propertyNames.nextElement());
8287
}
88+
final Set<String> propertyNameSet = orderedProperties.stringPropertyNames();
89+
final AtomicInteger i = new AtomicInteger(first);
90+
propertyNameSet.forEach(e -> assertEquals("key" + i.getAndDecrement(), e));
8391
return orderedProperties;
8492
}
8593

@@ -287,6 +295,12 @@ void testRemoveKeyValue() throws FileNotFoundException, IOException {
287295
assertFalse(Collections.list(props.propertyNames()).contains(k));
288296
}
289297

298+
@Test
299+
void testStringPropertyName() {
300+
final OrderedProperties orderedProperties = new OrderedProperties();
301+
assertTrue(orderedProperties.stringPropertyNames().isEmpty());
302+
}
303+
290304
@Test
291305
void testToString() {
292306
final OrderedProperties orderedProperties = new OrderedProperties();

0 commit comments

Comments
 (0)