SortedProperties should sort entries in same way as keys (#256)
This commit is contained in:
parent
87a028621f
commit
1cba0bcef2
|
@ -17,10 +17,13 @@
|
||||||
|
|
||||||
package org.apache.commons.collections4.properties;
|
package org.apache.commons.collections4.properties;
|
||||||
|
|
||||||
|
import java.util.AbstractMap;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
@ -48,4 +51,15 @@ public class SortedProperties extends Properties {
|
||||||
Collections.sort(keys);
|
Collections.sort(keys);
|
||||||
return new IteratorEnumeration<>(keys.iterator());
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,11 @@
|
||||||
package org.apache.commons.collections4.properties;
|
package org.apache.commons.collections4.properties;
|
||||||
|
|
||||||
import java.util.Enumeration;
|
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.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@ -35,4 +39,18 @@ public class SortedPropertiesTest {
|
||||||
Assert.assertEquals(String.valueOf(ch), keys.nextElement());
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue