SortedProperties should sort entries in same way as keys (#256)

This commit is contained in:
Michael Berry 2021-09-28 14:10:21 +01:00 committed by GitHub
parent 87a028621f
commit 1cba0bcef2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

@ -17,10 +17,13 @@
package org.apache.commons.collections4.properties;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
@ -48,4 +51,15 @@ public class SortedProperties extends Properties {
Collections.sort(keys);
return new IteratorEnumeration<>(keys.iterator());
}
@Override
public Set<Map.Entry<Object, Object>> entrySet() {
Enumeration<Object> keys = keys();
Set<Map.Entry<Object, Object>> entrySet = new LinkedHashSet<>();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
entrySet.add(new AbstractMap.SimpleEntry<>(key, getProperty((String) key)));
}
return entrySet;
}
}

View File

@ -18,7 +18,11 @@
package org.apache.commons.collections4.properties;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.commons.collections4.MultiSet;
import org.junit.Assert;
import org.junit.Test;
@ -35,4 +39,18 @@ public class SortedPropertiesTest {
Assert.assertEquals(String.valueOf(ch), keys.nextElement());
}
}
@Test
public void testEntrySet() {
final SortedProperties sortedProperties = new SortedProperties();
for (char ch = 'Z'; ch >= 'A'; ch--) {
sortedProperties.put(String.valueOf(ch), "Value" + ch);
}
final Iterator<Map.Entry<Object, Object>> entries = sortedProperties.entrySet().iterator();
for (char ch = 'A'; ch <= 'Z'; ch++) {
Map.Entry<Object, Object> entry = entries.next();
Assert.assertEquals(String.valueOf(ch), entry.getKey());
Assert.assertEquals("Value" + ch, entry.getValue());
}
}
}