You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/09/28 13:10:29 UTC

[commons-collections] branch master updated: SortedProperties should sort entries in same way as keys (#256)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git


The following commit(s) were added to refs/heads/master by this push:
     new 1cba0bc  SortedProperties should sort entries in same way as keys (#256)
1cba0bc is described below

commit 1cba0bcef28b3be54362648bea1dfe0721f12a2e
Author: Michael Berry <be...@gmail.com>
AuthorDate: Tue Sep 28 14:10:21 2021 +0100

    SortedProperties should sort entries in same way as keys (#256)
---
 .../collections4/properties/SortedProperties.java      | 14 ++++++++++++++
 .../collections4/properties/SortedPropertiesTest.java  | 18 ++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/src/main/java/org/apache/commons/collections4/properties/SortedProperties.java b/src/main/java/org/apache/commons/collections4/properties/SortedProperties.java
index 3ab41be..7a36d80 100644
--- a/src/main/java/org/apache/commons/collections4/properties/SortedProperties.java
+++ b/src/main/java/org/apache/commons/collections4/properties/SortedProperties.java
@@ -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;
+    }
 }
diff --git a/src/test/java/org/apache/commons/collections4/properties/SortedPropertiesTest.java b/src/test/java/org/apache/commons/collections4/properties/SortedPropertiesTest.java
index ce294bf..1da63cb 100644
--- a/src/test/java/org/apache/commons/collections4/properties/SortedPropertiesTest.java
+++ b/src/test/java/org/apache/commons/collections4/properties/SortedPropertiesTest.java
@@ -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());
+        }
+    }
 }