Repository: decorators-squad/eo-yaml
Pull request #171 (2020-03-09 22:00:46)

[WIP] Implement Ordering in ReadYamlMapping
PR for #166 

- Made ReadYamlMapping.keys() and RtYamlMapping.keys() return ordered sets.


Patch:
diff --git a/src/main/java/com/amihaiemil/eoyaml/ComparableYamlMapping.java b/src/main/java/com/amihaiemil/eoyaml/ComparableYamlMapping.java
index dc24c98..894550c 100644
--- a/src/main/java/com/amihaiemil/eoyaml/ComparableYamlMapping.java
+++ b/src/main/java/com/amihaiemil/eoyaml/ComparableYamlMapping.java
@@ -79,7 +79,7 @@ abstract class ComparableYamlMapping implements YamlMapping {
      *
      * A Mapping is always considered greater than a Scalar or a Sequence.<br>
      *
-     * If o is a Mapping, their integer lengths are compared - the one with
+     * If other is a Mapping, their integer lengths are compared - the one with
      * the greater length is considered greater. If the lengths are equal,
      * then the 2 Mappings are equal if all elements are equal (K==K and V==V).
      * If the elements are not identical, the comparison of the first unequal
@@ -89,9 +89,9 @@ abstract class ComparableYamlMapping implements YamlMapping {
      * @checkstyle NestedIfDepth (100 lines)
      * @checkstyle ExecutableStatementCount (100 lines)
      * @return
-     *   a value &lt; 0 if this &lt; o <br>
-     *   0 if this == o or <br>
-     *   a value &gt; 0 if this &gt; o
+     *   a value &lt; 0 if this &lt; other <br>
+     *   0 if this == other or <br>
+     *   a value &gt; 0 if this &gt; other
      */
     @Override
     public int compareTo(final YamlNode other) {
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadYamlMapping.java b/src/main/java/com/amihaiemil/eoyaml/ReadYamlMapping.java
index 67c7586..72fe9f3 100644
--- a/src/main/java/com/amihaiemil/eoyaml/ReadYamlMapping.java
+++ b/src/main/java/com/amihaiemil/eoyaml/ReadYamlMapping.java
@@ -27,11 +27,7 @@
  */
 package com.amihaiemil.eoyaml;
 
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
 
 /**
  * YamlMapping read from somewhere.
@@ -208,7 +204,7 @@ final class ReadYamlMapping extends ComparableYamlMapping {
 
     @Override
     public Set<YamlNode> keys() {
-        final Set<YamlNode> keys = new HashSet<>();
+        final Set<YamlNode> keys = new TreeSet<>();
         for (final YamlLine line : this.lines) {
             final String trimmed = line.trimmed();
             if(":".equals(trimmed)) {
diff --git a/src/main/java/com/amihaiemil/eoyaml/RtYamlMapping.java b/src/main/java/com/amihaiemil/eoyaml/RtYamlMapping.java
index 17ffc63..8023381 100644
--- a/src/main/java/com/amihaiemil/eoyaml/RtYamlMapping.java
+++ b/src/main/java/com/amihaiemil/eoyaml/RtYamlMapping.java
@@ -27,11 +27,7 @@
  */
 package com.amihaiemil.eoyaml;
 
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-import java.util.TreeMap;
+import java.util.*;
 
 /**
  * YAML mapping implementation (rt means runtime).
@@ -158,7 +154,7 @@ final class RtYamlMapping extends ComparableYamlMapping {
 
     @Override
     public Set<YamlNode> keys() {
-        final Set<YamlNode> keys = new HashSet<>();
+        final Set<YamlNode> keys = new TreeSet<>();
         keys.addAll(this.mappings.keySet());
         return keys;
     }
diff --git a/src/main/java/com/amihaiemil/eoyaml/YamlMapping.java b/src/main/java/com/amihaiemil/eoyaml/YamlMapping.java
index 79e55de..3e2033a 100644
--- a/src/main/java/com/amihaiemil/eoyaml/YamlMapping.java
+++ b/src/main/java/com/amihaiemil/eoyaml/YamlMapping.java
@@ -86,7 +86,8 @@ public interface YamlMapping extends YamlNode {
     String string(final YamlNode key);
 
     /**
-     * Return the keys' set of this mapping.
+     * Return the keys' set of this mapping.<br><br>
+     * <b>Pay attention: </b> the keys are ordered.
      * @return Set of YamlNode keys.
      */
     Set<YamlNode> keys();


Test patch:
diff --git a/src/test/java/com/amihaiemil/eoyaml/ReadYamlMappingTest.java b/src/test/java/com/amihaiemil/eoyaml/ReadYamlMappingTest.java
index 6cb1e5f..69591f9 100644
--- a/src/test/java/com/amihaiemil/eoyaml/ReadYamlMappingTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/ReadYamlMappingTest.java
@@ -28,6 +28,7 @@
 package com.amihaiemil.eoyaml;
 
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
@@ -69,33 +70,35 @@ public final class ReadYamlMappingTest {
     }
 
     /**
-     * ReadYamlMapping can return its keys.
+     * ReadYamlMapping can return its keys. The keys should
+     * be ordered.
      */
     @Test
-    public void returnsKeys(){
+    public void returnsOrderedKeys(){
         final List<YamlLine> lines = new ArrayList<>();
-        lines.add(new RtYamlLine("first: somethingElse", 0));
-        lines.add(new RtYamlLine("second: ", 1));
+        lines.add(new RtYamlLine("zkey: somethingElse", 0));
+        lines.add(new RtYamlLine("bkey: ", 1));
         lines.add(new RtYamlLine("  fourth: some", 2));
         lines.add(new RtYamlLine("  fifth: values", 3));
-        lines.add(new RtYamlLine("third: something", 4));
+        lines.add(new RtYamlLine("akey: something", 4));
         final YamlMapping map = new ReadYamlMapping(new AllYamlLines(lines));
         final Set<YamlNode> keys = map.keys();
         MatcherAssert.assertThat(
             keys, Matchers.not(Matchers.emptyIterable())
         );
         MatcherAssert.assertThat(keys.size(), Matchers.equalTo(3));
+        final Iterator<YamlNode> iterator = keys.iterator();
         MatcherAssert.assertThat(
-            keys.contains(new Scalar("first")),
-            Matchers.is(Boolean.TRUE)
+            iterator.next(),
+            Matchers.equalTo(new Scalar("akey"))
         );
         MatcherAssert.assertThat(
-            keys.contains(new Scalar("second")),
-            Matchers.is(Boolean.TRUE)
+            iterator.next(),
+            Matchers.equalTo(new Scalar("bkey"))
         );
         MatcherAssert.assertThat(
-            keys.contains(new Scalar("third")),
-            Matchers.is(Boolean.TRUE)
+            iterator.next(),
+            Matchers.equalTo(new Scalar("zkey"))
         );
     }