Repository: decorators-squad/eo-yaml
Pull request #269 (2020-03-31 16:17:20)

#242 - removes ReadPlainScalarKey class.
#242 This PR removes the ReadPlainScalarKey class.


Patch:
diff --git a/src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java b/src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java
index e0f9803..8f3ee58 100644
--- a/src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java
+++ b/src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java
@@ -129,7 +129,7 @@ final class AllYamlLines implements YamlLines {
         } else if (first.trimmed().contains(":")){
             node = new ReadYamlMapping(this);
         } else if(this.lines().size() == 1) {
-            node = new ReadPlainScalarValue(first);
+            node = new ReadPlainScalar(first);
         } else {
             throw new YamlReadingException(
                 "Could not parse YAML starting at line " + (first.number() + 1)
diff --git a/src/main/java/com/amihaiemil/eoyaml/BaseYamlMapping.java b/src/main/java/com/amihaiemil/eoyaml/BaseYamlMapping.java
index 650c04c..dc7af02 100644
--- a/src/main/java/com/amihaiemil/eoyaml/BaseYamlMapping.java
+++ b/src/main/java/com/amihaiemil/eoyaml/BaseYamlMapping.java
@@ -222,7 +222,7 @@ public abstract class BaseYamlMapping
     ) {
         final BaseScalar indentable = (BaseScalar) scalar;
         if (indentable instanceof PlainStringScalar
-            || indentable instanceof ReadPlainScalarValue
+            || indentable instanceof ReadPlainScalar
         ) {
             print.append(indentable.indent(0)).append(System.lineSeparator());
         } else if (indentable instanceof RtYamlScalarBuilder.BuiltFoldedBlockScalar
diff --git a/src/main/java/com/amihaiemil/eoyaml/BaseYamlSequence.java b/src/main/java/com/amihaiemil/eoyaml/BaseYamlSequence.java
index 25116ba..8ea783e 100644
--- a/src/main/java/com/amihaiemil/eoyaml/BaseYamlSequence.java
+++ b/src/main/java/com/amihaiemil/eoyaml/BaseYamlSequence.java
@@ -186,7 +186,7 @@ public abstract class BaseYamlSequence
     ) {
         final BaseScalar indentable = (BaseScalar) scalar;
         if (indentable instanceof PlainStringScalar
-            || indentable instanceof ReadPlainScalarValue
+            || indentable instanceof ReadPlainScalar
         ) {
             print.append(indentable.indent(0)).append(System.lineSeparator());
         } else if (indentable instanceof RtYamlScalarBuilder.BuiltFoldedBlockScalar
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadPlainScalarValue.java b/src/main/java/com/amihaiemil/eoyaml/ReadPlainScalar.java
similarity index 97%
rename from src/main/java/com/amihaiemil/eoyaml/ReadPlainScalarValue.java
rename to src/main/java/com/amihaiemil/eoyaml/ReadPlainScalar.java
index 7d9d861..60726cd 100644
--- a/src/main/java/com/amihaiemil/eoyaml/ReadPlainScalarValue.java
+++ b/src/main/java/com/amihaiemil/eoyaml/ReadPlainScalar.java
@@ -33,7 +33,7 @@ package com.amihaiemil.eoyaml;
  * @version $Id$
  * @since 3.1.3
  */
-final class ReadPlainScalarValue extends BaseScalar {
+final class ReadPlainScalar extends BaseScalar {
 
     /**
      * Line where the plain scalar value is supposed to be found.
@@ -48,7 +48,7 @@ final class ReadPlainScalarValue extends BaseScalar {
      * Constructor.
      * @param line Read YamlLine.
      */
-    ReadPlainScalarValue(final YamlLine line) {
+    ReadPlainScalar(final YamlLine line) {
         this.line = line;
     }
 
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadPlainScalarKey.java b/src/main/java/com/amihaiemil/eoyaml/ReadPlainScalarKey.java
deleted file mode 100644
index 825eeea..0000000
--- a/src/main/java/com/amihaiemil/eoyaml/ReadPlainScalarKey.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/**
- * Copyright (c) 2016-2020, Mihai Emil Andronache
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- *  modification, are permitted provided that the following conditions are met:
- * Redistributions of source code must retain the above copyright notice, this
- *  list of conditions and the following disclaimer.
- *  Redistributions in binary form must reproduce the above copyright notice,
- *  this list of conditions and the following disclaimer in the documentation
- *  and/or other materials provided with the distribution.
- * Neither the name of the copyright holder nor the names of its
- *  contributors may be used to endorse or promote products derived from
- *  this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-package com.amihaiemil.eoyaml;
-
-/**
- * A Scalar key from a ReadYamlMapping.
- * @author Mihai Andronache (amihaiemil@gmail.com)
- * @version $Id$
- * @since 3.1.3
- */
-final class ReadPlainScalarKey extends BaseScalar {
-
-    /**
-     * Line where the plain scalar key is supposed to be found.
-     * This line should start with "key:".
-     */
-    private YamlLine line;
-
-    /**
-     * Constructor.
-     * @param line Read YamlLine.
-     */
-    ReadPlainScalarKey(final YamlLine line) {
-        this.line = line;
-    }
-
-    @Override
-    public String value() {
-        final String trimmed = this.line.trimmed();
-        if(trimmed.contains(":")) {
-            final String keyPart = trimmed.substring(
-                0, trimmed.indexOf(":")
-            ).trim();
-            if (!keyPart.isEmpty()) {
-                return keyPart;
-            }
-        }
-        throw new IllegalStateException(
-            "Expected scalar key on line " + (this.line.number() + 1) + "."
-                + " The line should have the format 'key: value' or 'key:'. "
-                + "Instead, the line is: [" + this.line.trimmed() + "]."
-        );
-    }
-
-    @Override
-    public String toString() {
-        return this.indent(0);
-    }
-}
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadYamlMapping.java b/src/main/java/com/amihaiemil/eoyaml/ReadYamlMapping.java
index 01572ea..6dc07cc 100644
--- a/src/main/java/com/amihaiemil/eoyaml/ReadYamlMapping.java
+++ b/src/main/java/com/amihaiemil/eoyaml/ReadYamlMapping.java
@@ -28,7 +28,6 @@
 package com.amihaiemil.eoyaml;
 
 import com.amihaiemil.eoyaml.exceptions.YamlReadingException;
-
 import java.util.*;
 
 /**
@@ -71,7 +70,21 @@ final class ReadYamlMapping extends BaseYamlMapping {
             } else if ("?".equals(trimmed)) {
                 keys.add(this.lines.nested(line.number()).toYamlNode(line));
             } else {
-                keys.add(new ReadPlainScalarKey(line));
+                if(!trimmed.contains(":")) {
+                    throw new YamlReadingException(
+                        "Expected scalar key on line " 
+                        + (line.number() + 1) + "."
+                        + " The line should have the format " 
+                        + "'key: value' or 'key:'. "
+                        + "Instead, the line is: "
+                        + "[" + line.trimmed() + "]."
+                    );
+                }
+                final String key = trimmed.substring(
+                        0, trimmed.indexOf(":")).trim();
+                if(!key.isEmpty()) {
+                    keys.add(new PlainStringScalar(key));
+                }
             }
         }
         return keys;
@@ -125,8 +138,8 @@ final class ReadYamlMapping extends BaseYamlMapping {
     public String string(final YamlNode key) {
         final String found;
         final YamlNode value = this.value(key);
-        if(value instanceof ReadPlainScalarValue) {
-            found = ((ReadPlainScalarValue) value).value();
+        if(value instanceof ReadPlainScalar) {
+            found = ((ReadPlainScalar) value).value();
         } else {
             found = null;
         }
@@ -178,7 +191,7 @@ final class ReadYamlMapping extends BaseYamlMapping {
             } else if(trimmed.startsWith(key + ":")
                 && trimmed.length() > 1
             ) {
-                value = new ReadPlainScalarValue(line);
+                value = new ReadPlainScalar(line);
             }
         }
         return value;
@@ -212,7 +225,7 @@ final class ReadYamlMapping extends BaseYamlMapping {
                     } else if(colonLine.trimmed().startsWith(":")
                         && (colonLine.trimmed().length() > 1)
                     ){
-                        value = new ReadPlainScalarValue(colonLine);
+                        value = new ReadPlainScalar(colonLine);
                     } else {
                         throw new YamlReadingException(
                             "No value found for existing complex key: "
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java b/src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java
index a231071..ca03b57 100644
--- a/src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java
+++ b/src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java
@@ -67,7 +67,7 @@ final class ReadYamlSequence extends BaseYamlSequence {
             ) {
                 kids.add(this.lines.nested(line.number()).toYamlNode(line));
             } else {
-                kids.add(new ReadPlainScalarValue(line));
+                kids.add(new ReadPlainScalar(line));
             }
         }
         return kids;
@@ -104,7 +104,7 @@ final class ReadYamlSequence extends BaseYamlSequence {
         String value = null;
         int count = 0;
         for (final YamlNode node : this.values()) {
-            if(count == index && (node instanceof ReadPlainScalarValue)) {
+            if(count == index && (node instanceof ReadPlainScalar)) {
                 value = ((Scalar) node).value();
                 break;
             }
diff --git a/src/main/java/com/amihaiemil/eoyaml/RtYamlInput.java b/src/main/java/com/amihaiemil/eoyaml/RtYamlInput.java
index 6d4dced..f767c80 100644
--- a/src/main/java/com/amihaiemil/eoyaml/RtYamlInput.java
+++ b/src/main/java/com/amihaiemil/eoyaml/RtYamlInput.java
@@ -73,14 +73,14 @@ final class RtYamlInput implements YamlInput {
 
     @Override
     public Scalar readPlainScalar() throws IOException {
-        final ReadPlainScalarValue read;
+        final ReadPlainScalar read;
         final Iterator<YamlLine> iterator = new NoDirectivesOrMarkers(
             this.readInput()
         ).iterator();
         if(!iterator.hasNext()) {
-            read = new ReadPlainScalarValue(new YamlLine.NullYamlLine());
+            read = new ReadPlainScalar(new YamlLine.NullYamlLine());
         } else {
-            read = new ReadPlainScalarValue(iterator.next());
+            read = new ReadPlainScalar(iterator.next());
         }
         return read;
     }


Test patch:
diff --git a/src/test/java/com/amihaiemil/eoyaml/AllYamlLinesTest.java b/src/test/java/com/amihaiemil/eoyaml/AllYamlLinesTest.java
index b33f73b..17954cf 100644
--- a/src/test/java/com/amihaiemil/eoyaml/AllYamlLinesTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/AllYamlLinesTest.java
@@ -247,7 +247,7 @@ public final class AllYamlLinesTest {
         final YamlLines yamlLines = new AllYamlLines(lines);
         MatcherAssert.assertThat(
             yamlLines.toYamlNode(new RtYamlLine("---", 0)),
-            Matchers.instanceOf(ReadPlainScalarValue.class)
+            Matchers.instanceOf(ReadPlainScalar.class)
         );
     }
 
diff --git a/src/test/java/com/amihaiemil/eoyaml/ReadPlainScalarKeyTest.java b/src/test/java/com/amihaiemil/eoyaml/ReadPlainScalarKeyTest.java
deleted file mode 100644
index ba438e8..0000000
--- a/src/test/java/com/amihaiemil/eoyaml/ReadPlainScalarKeyTest.java
+++ /dev/null
@@ -1,125 +0,0 @@
-/**
- * Copyright (c) 2016-2020, Mihai Emil Andronache
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- *  modification, are permitted provided that the following conditions are met:
- * Redistributions of source code must retain the above copyright notice, this
- *  list of conditions and the following disclaimer.
- *  Redistributions in binary form must reproduce the above copyright notice,
- *  this list of conditions and the following disclaimer in the documentation
- *  and/or other materials provided with the distribution.
- * Neither the name of the copyright holder nor the names of its
- *  contributors may be used to endorse or promote products derived from
- *  this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-package com.amihaiemil.eoyaml;
-
-import org.hamcrest.MatcherAssert;
-import org.hamcrest.Matchers;
-import org.junit.Assert;
-import org.junit.Test;
-
-/**
- * Unit tests for {@link ReadPlainScalarKey}.
- * @author Mihai Andronache (amihaiemil@gmail.com)
- * @version $Id$
- * @since 3.1.3
- */
-public final class ReadPlainScalarKeyTest {
-
-    /**
-     * ReadPlainScalarKey can return the key from a YamlLine that
-     * represents a simple key: value mapping.
-     */
-    @Test
-    public void returnsKeyWithScalarValue() {
-        final Scalar scalar = new ReadPlainScalarKey(
-            new RtYamlLine("key: value", 0)
-        );
-        MatcherAssert.assertThat(scalar.value(), Matchers.equalTo("key"));
-    }
-
-    /**
-     * ReadPlainScalarKey can return the key from a YamlLine that
-     * represents a mapping between a simple key and a YamlNode value.
-     */
-    @Test
-    public void returnsKeyWithYamlNodeValue() {
-        final Scalar scalar = new ReadPlainScalarKey(
-            new RtYamlLine("key: ", 0)
-        );
-        MatcherAssert.assertThat(scalar.value(), Matchers.equalTo("key"));
-    }
-
-    /**
-     * ReadPlainScalarKey will complain if the given YamlLine contains
-     * the start of a complex key rather than a Scalar key.
-     */
-    @Test(expected = IllegalStateException.class)
-    public void complainsOnStartOfComplexKey() {
-        final YamlLine line = new RtYamlLine("?", 0);
-        final Scalar scalar = new ReadPlainScalarKey(line);
-        scalar.value();
-        Assert.fail("IllegalStateException was expected.");
-
-    }
-
-    /**
-     * ReadPlainScalarKey will complain if the given YamlLine contains
-     * the end of a complex key rather than a Scalar key.
-     */
-    @Test(expected = IllegalStateException.class)
-    public void complainsOnEndOfComplexKey() {
-        final YamlLine line = new RtYamlLine(":", 0);
-        final Scalar scalar = new ReadPlainScalarKey(line);
-        scalar.value();
-        Assert.fail("IllegalStateException was expected.");
-    }
-
-    /**
-     * ReadPlainScalarKey will complain if the given YamlLine is actually
-     * part of a YamlSequence.
-     */
-    @Test(expected = IllegalStateException.class)
-    public void complainsOnLineFromSequence() {
-        final YamlLine line = new RtYamlLine("- someValue", 0);
-        final Scalar scalar = new ReadPlainScalarKey(line);
-        scalar.value();
-        Assert.fail("IllegalStateException was expected.");
-    }
-
-    /**
-     * ReadPlainScalarKey will complain if the given YamlLine is empty.
-     */
-    @Test(expected = IllegalStateException.class)
-    public void complainsOnEmptyLine() {
-        final YamlLine line = new RtYamlLine("", 0);
-        final Scalar scalar = new ReadPlainScalarKey(line);
-        scalar.value();
-        Assert.fail("IllegalStateException was expected.");
-    }
-
-    /**
-     * Unit test for toString.
-     */
-    @Test
-    public void toStringWorks() {
-        final Scalar scalar = new ReadPlainScalarKey(
-            new RtYamlLine("key: value", 0)
-        );
-        MatcherAssert.assertThat(scalar.toString(), Matchers.equalTo("key"));
-    }
-}
diff --git a/src/test/java/com/amihaiemil/eoyaml/ReadPlainScalarValueTest.java b/src/test/java/com/amihaiemil/eoyaml/ReadPlainScalarValueTest.java
index 82567ce..f5d9198 100644
--- a/src/test/java/com/amihaiemil/eoyaml/ReadPlainScalarValueTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/ReadPlainScalarValueTest.java
@@ -40,46 +40,46 @@ import org.junit.Test;
 public final class ReadPlainScalarValueTest {
 
     /**
-     * ReadPlainScalarValue can return the scalar's value from a mapping line.
+     * ReadPlainScalar can return the scalar's value from a mapping line.
      */
     @Test
     public void returnsValueFromMappingLine() {
-        final Scalar scalar = new ReadPlainScalarValue(
+        final Scalar scalar = new ReadPlainScalar(
             new RtYamlLine("key: value", 0)
         );
         MatcherAssert.assertThat(scalar.value(), Matchers.equalTo("value"));
     }
 
     /**
-     * ReadPlainScalarValue can return the scalar's value from a sequence line.
+     * ReadPlainScalar can return the scalar's value from a sequence line.
      */
     @Test
     public void returnsValueFromSequenceLine() {
-        final Scalar scalar = new ReadPlainScalarValue(
+        final Scalar scalar = new ReadPlainScalar(
             new RtYamlLine("- value", 0)
         );
         MatcherAssert.assertThat(scalar.value(), Matchers.equalTo("value"));
     }
 
     /**
-     * ReadPlainScalarValue will return the plain scalar.
+     * ReadPlainScalar will return the plain scalar.
      */
     @Test
     public void returnsPlainScalar() {
         final YamlLine line = new RtYamlLine("value", 0);
-        final Scalar scalar = new ReadPlainScalarValue(line);
+        final Scalar scalar = new ReadPlainScalar(line);
         MatcherAssert.assertThat(scalar.value(), Matchers.equalTo("value"));
     }
 
     /**
-     * ReadPlainScalarValue will return empty string if the line is empty.
+     * ReadPlainScalar will return empty string if the line is empty.
      * Should never be the case in real practice, however, because empty
      * lines are omitted when read.
      */
     @Test
     public void returnsEmptyScalar() {
         final YamlLine line = new RtYamlLine("", 0);
-        final Scalar scalar = new ReadPlainScalarValue(line);
+        final Scalar scalar = new ReadPlainScalar(line);
         MatcherAssert.assertThat(scalar.value(), Matchers.isEmptyString());
     }
 
@@ -88,7 +88,7 @@ public final class ReadPlainScalarValueTest {
      */
     @Test
     public void toStringWorks() {
-        final Scalar scalar = new ReadPlainScalarValue(
+        final Scalar scalar = new ReadPlainScalar(
             new RtYamlLine("key: value", 0)
         );
         MatcherAssert.assertThat(