Repository: decorators-squad/eo-yaml
Pull request #289 (2020-04-05 17:46:05)

Method comment() Implemented for Built Scalars
PR for #288 
Method ``comment()`` implemented in all types of scalar used for building YAML.


Patch:
diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md
deleted file mode 100644
index 82d5b02..0000000
--- a/CODE_OF_CONDUCT.md
+++ /dev/null
@@ -1,4 +0,0 @@
-1. This repo should be an intelectual warfare zone, particularly focused on Object-Oriented Programming.
-2. Be nice to each other. Don't take rejected PRs personally.
-3. Try not to spam Issues or PRs with useless comments.
-3. Enjoy yourself.
diff --git a/src/main/java/com/amihaiemil/eoyaml/BaseScalar.java b/src/main/java/com/amihaiemil/eoyaml/BaseScalar.java
index afc35f8..db9d1ca 100644
--- a/src/main/java/com/amihaiemil/eoyaml/BaseScalar.java
+++ b/src/main/java/com/amihaiemil/eoyaml/BaseScalar.java
@@ -129,4 +129,12 @@ abstract class BaseScalar extends BaseYamlNode implements Scalar {
         }
         return printed.append(this.value()).toString();
     }
+
+    /**
+     * Get the Comment referring to this YamlNode.
+     * @return Comment.
+     */
+    public Comment comment() {
+        return null;
+    }
 }
diff --git a/src/main/java/com/amihaiemil/eoyaml/BaseYamlNode.java b/src/main/java/com/amihaiemil/eoyaml/BaseYamlNode.java
index 9987704..f7425c9 100644
--- a/src/main/java/com/amihaiemil/eoyaml/BaseYamlNode.java
+++ b/src/main/java/com/amihaiemil/eoyaml/BaseYamlNode.java
@@ -30,6 +30,11 @@ package com.amihaiemil.eoyaml;
 /**
  * Base YAML Node. This is the first class in the hierarchy
  * for any kind of YAML node.
+ *
+ * So far, the purpose of this base class is to hide methods that should
+ * be applicable to all types of YamlNode and which we do not want
+ * to make public on the YamlNode interface.
+ *
  * @author Mihai Andronache (amihaiemil@gmail.com)
  * @version $Id$
  * @since 4.0.0
diff --git a/src/main/java/com/amihaiemil/eoyaml/PlainStringScalar.java b/src/main/java/com/amihaiemil/eoyaml/PlainStringScalar.java
index 3ee9851..0ff5498 100644
--- a/src/main/java/com/amihaiemil/eoyaml/PlainStringScalar.java
+++ b/src/main/java/com/amihaiemil/eoyaml/PlainStringScalar.java
@@ -32,7 +32,7 @@ package com.amihaiemil.eoyaml;
  * built YAML or in the unit tests.
  *
  * DO NOT use it when READING yaml. For reading use
- * {@link ReadPlainScalarValue}!
+ * {@link ReadPlainScalar}!
  *
  * @author Mihai Andronache (amihaiemil@gmail.com)
  * @version $Id$
@@ -41,17 +41,32 @@ package com.amihaiemil.eoyaml;
  */
 final class PlainStringScalar extends BaseScalar {
 
+    /**
+     * Comments referring to this scalar.
+     */
+    private final Comment comment;
+
     /**
      * This scalar's value.
      */
-    private String value;
+    private final String value;
 
     /**
      * Ctor.
      * @param value Given value for this scalar.
      */
     PlainStringScalar(final String value) {
+        this(value, "");
+    }
+
+    /**
+     * Ctor.
+     * @param comment Comment referring to this Scalar.
+     * @param value Given value for this scalar.
+     */
+    PlainStringScalar(final String value, final String comment) {
         this.value = value;
+        this.comment = new BuiltComment(this, comment);
     }
 
     /**
@@ -63,6 +78,11 @@ final class PlainStringScalar extends BaseScalar {
         return this.value;
     }
 
+    @Override
+    public Comment comment() {
+        return this.comment;
+    }
+
     /**
      * When printing a plain scalar, we have to wrap it
      * inside proper YAML elements, otherwise it won't make
diff --git a/src/main/java/com/amihaiemil/eoyaml/RtYamlScalarBuilder.java b/src/main/java/com/amihaiemil/eoyaml/RtYamlScalarBuilder.java
index 71fa865..f06cdbc 100644
--- a/src/main/java/com/amihaiemil/eoyaml/RtYamlScalarBuilder.java
+++ b/src/main/java/com/amihaiemil/eoyaml/RtYamlScalarBuilder.java
@@ -94,6 +94,11 @@ final class RtYamlScalarBuilder implements YamlScalarBuilder {
      */
     static class BuiltFoldedBlockScalar extends BaseScalar {
 
+        /**
+         * Comments referring to this scalar.
+         */
+        private final Comment comment;
+
         /**
          * Lines of this scalar.
          */
@@ -104,7 +109,19 @@ final class RtYamlScalarBuilder implements YamlScalarBuilder {
          * @param lines Given string lines.
          */
         BuiltFoldedBlockScalar(final List<String> lines) {
+            this(lines, "");
+        }
+
+        /**
+         * Ctor.
+         * @param comment Comment referring to this scalar.
+         * @param lines Given string lines.
+         */
+        BuiltFoldedBlockScalar(
+            final List<String> lines, final String comment
+        ) {
             this.lines = lines;
+            this.comment = new BuiltComment(this, comment);
         }
 
         /**
@@ -123,6 +140,11 @@ final class RtYamlScalarBuilder implements YamlScalarBuilder {
             ).collect(Collectors.joining(" "));
         }
 
+        @Override
+        public Comment comment() {
+            return this.comment;
+        }
+
         /**
          * Indent this block scalar. When indenting/printing, we're going to
          * separate the lines.
@@ -198,6 +220,11 @@ final class RtYamlScalarBuilder implements YamlScalarBuilder {
      */
     static class BuiltLiteralBlockScalar extends BaseScalar {
 
+        /**
+         * Comments referring to this scalar.
+         */
+        private final Comment comment;
+
         /**
          * Lines of this scalar.
          */
@@ -208,9 +235,22 @@ final class RtYamlScalarBuilder implements YamlScalarBuilder {
          * @param lines Given string lines.
          */
         BuiltLiteralBlockScalar(final List<String> lines) {
+            this(lines, "");
+        }
+
+        /**
+         * Ctor.
+         * @param lines Given string lines.
+         * @param comment Comment referring to this scalar.
+         */
+        BuiltLiteralBlockScalar(
+            final List<String> lines, final String comment
+        ) {
             this.lines = lines;
+            this.comment = new BuiltComment(this, comment);
         }
 
+
         /**
          * Return the value of this literal scalar.
          * @return String value.
@@ -222,6 +262,11 @@ final class RtYamlScalarBuilder implements YamlScalarBuilder {
             );
         }
 
+        @Override
+        public Comment comment() {
+            return this.comment;
+        }
+
         /**
          * Indent this block scalar. When indenting/printing, we're going to
          * separate the lines.
diff --git a/src/main/java/com/amihaiemil/eoyaml/Scalar.java b/src/main/java/com/amihaiemil/eoyaml/Scalar.java
index a3fc0cf..248383e 100644
--- a/src/main/java/com/amihaiemil/eoyaml/Scalar.java
+++ b/src/main/java/com/amihaiemil/eoyaml/Scalar.java
@@ -46,4 +46,10 @@ public interface Scalar extends YamlNode {
      *  supposed to be.
      */
     String value();
+
+    /**
+     * Comment referring to this Scalar.
+     * @return Comment.
+     */
+    Comment comment();
 }


Test patch:
diff --git a/src/test/java/com/amihaiemil/eoyaml/BuiltFoldedBlockScalarTest.java b/src/test/java/com/amihaiemil/eoyaml/BuiltFoldedBlockScalarTest.java
index b454bf9..a8f98e1 100644
--- a/src/test/java/com/amihaiemil/eoyaml/BuiltFoldedBlockScalarTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/BuiltFoldedBlockScalarTest.java
@@ -128,4 +128,22 @@ public final class BuiltFoldedBlockScalarTest {
         );
     }
 
+    /**
+     * BuiltFoldedBlockScalar can return its comment.
+     */
+    @Test
+    public void returnsComment() {
+        final Scalar scl = new RtYamlScalarBuilder.BuiltFoldedBlockScalar(
+            new ArrayList<>(), "comment"
+        );
+        MatcherAssert.assertThat(
+            scl.comment().value(),
+            Matchers.equalTo("comment")
+        );
+        MatcherAssert.assertThat(
+            scl.comment().yamlNode(),
+            Matchers.is(scl)
+        );
+    }
+
 }
diff --git a/src/test/java/com/amihaiemil/eoyaml/BuiltLiteralBlockScalarTest.java b/src/test/java/com/amihaiemil/eoyaml/BuiltLiteralBlockScalarTest.java
index 7694bf4..54fa1f7 100644
--- a/src/test/java/com/amihaiemil/eoyaml/BuiltLiteralBlockScalarTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/BuiltLiteralBlockScalarTest.java
@@ -127,4 +127,22 @@ public final class BuiltLiteralBlockScalarTest {
         );
     }
 
+    /**
+     * BuiltLiteralBlockScalar can return its comment.
+     */
+    @Test
+    public void returnsComment() {
+        final Scalar scl = new RtYamlScalarBuilder.BuiltLiteralBlockScalar(
+            new ArrayList<>(), "comment"
+        );
+        MatcherAssert.assertThat(
+            scl.comment().value(),
+            Matchers.equalTo("comment")
+        );
+        MatcherAssert.assertThat(
+            scl.comment().yamlNode(),
+            Matchers.is(scl)
+        );
+    }
+
 }
diff --git a/src/test/java/com/amihaiemil/eoyaml/PlainStringScalarTest.java b/src/test/java/com/amihaiemil/eoyaml/PlainStringScalarTest.java
index 23fadf1..248667c 100644
--- a/src/test/java/com/amihaiemil/eoyaml/PlainStringScalarTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/PlainStringScalarTest.java
@@ -55,6 +55,23 @@ public final class PlainStringScalarTest {
         MatcherAssert.assertThat(scl.value(), Matchers.equalTo(val));
     }
 
+    /**
+     * Scalar can return its comment.
+     */
+    @Test
+    public void returnsComment() {
+        final String val = "test scalar value";
+        final PlainStringScalar scl = new PlainStringScalar(val, "comment");
+        MatcherAssert.assertThat(
+            scl.comment().value(),
+            Matchers.equalTo("comment")
+        );
+        MatcherAssert.assertThat(
+            scl.comment().yamlNode(),
+            Matchers.is(scl)
+        );
+    }
+
     /**
      * Make sure that equals and hash code are reflexive
      * and symmetric.