From 0d00395b9149f87e7c6fd1205c0606a75fade08a Mon Sep 17 00:00:00 2001 From: amihaiemil Date: Sun, 5 May 2024 17:32:57 +0300 Subject: [PATCH] convenience type casting for booleans --- .../com/amihaiemil/eoyaml/YamlMapping.java | 38 +++++++++++++++++++ .../com/amihaiemil/eoyaml/YamlSequence.java | 15 ++++++++ .../YamlMappingTypeCastingTestCase.java | 25 +++++++++++- .../YamlSequenceTypeCastingTestCase.java | 19 ++++++++++ 4 files changed, 96 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/amihaiemil/eoyaml/YamlMapping.java b/src/main/java/com/amihaiemil/eoyaml/YamlMapping.java index 1cd2e28c..2e429bf6 100644 --- a/src/main/java/com/amihaiemil/eoyaml/YamlMapping.java +++ b/src/main/java/com/amihaiemil/eoyaml/YamlMapping.java @@ -463,6 +463,44 @@ default LocalDateTime dateTime(final YamlNode key) { return null; } + /** + * Convenience method to directly read a boolean value + * from this map. Returns true if the value is not null and is equal, + * ignoring case, to the string "true". + * It is equivalent to: + *
+     *     YamlMapping map = ...;
+     *     boolean value = Boolean.valueOf(map.string(...));
+     * 
+ * @param key The key of the value. + * @return Boolean. + */ + default boolean bool(final String key) { + return this.bool( + Yaml.createYamlScalarBuilder().addLine(key).buildPlainScalar() + ); + } + + /** + * Convenience method to directly read a boolean value + * from this map. Returns true if the value is not null and is equal, + * ignoring case, to the string "true". + * It is equivalent to: + *
+     *     YamlMapping map = ...;
+     *     boolean value = Boolean.valueOf(map.string(...));
+     * 
+ * @param key The key of the value. + * @return Boolean. + */ + default boolean bool(final YamlNode key) { + final YamlNode value = this.value(key); + if(value instanceof Scalar) { + return Boolean.valueOf(((Scalar) value).value()); + } + return false; + } + @Override default List children() { final List children = new ArrayList<>(); diff --git a/src/main/java/com/amihaiemil/eoyaml/YamlSequence.java b/src/main/java/com/amihaiemil/eoyaml/YamlSequence.java index 30c6b4ca..615d0074 100644 --- a/src/main/java/com/amihaiemil/eoyaml/YamlSequence.java +++ b/src/main/java/com/amihaiemil/eoyaml/YamlSequence.java @@ -276,6 +276,21 @@ default LocalDateTime dateTime(final int index) { return datetime; } + /** + * Convenience method to directly read a boolean value + * from this sequence. Returns true if the value is not null and is equal, + * ignoring case, to the string "true".It is equivalent to: + *
+     *     YamlSequence sequence = ...;
+     *     boolean value = Boolean.valueOf(sequence.string(...));
+     * 
+ * @param index The index of the value. + * @return Boolean. + */ + default boolean bool(final int index) { + return Boolean.valueOf(this.string(index)); + } + /** * Turn this YamlSequence to a JsonArray. * @return JsonArray. diff --git a/src/test/java/com/amihaiemil/eoyaml/YamlMappingTypeCastingTestCase.java b/src/test/java/com/amihaiemil/eoyaml/YamlMappingTypeCastingTestCase.java index e925ab13..d2021b60 100644 --- a/src/test/java/com/amihaiemil/eoyaml/YamlMappingTypeCastingTestCase.java +++ b/src/test/java/com/amihaiemil/eoyaml/YamlMappingTypeCastingTestCase.java @@ -121,7 +121,28 @@ public void returnsDateTime() { Matchers.nullValue() ); } - + + @Test + public void returnsBoolean() { + final YamlMapping mapping = this.mapping(); + MatcherAssert.assertThat( + mapping.bool("trueValue"), + Matchers.is(true) + ); + MatcherAssert.assertThat( + mapping.bool("falseValue"), + Matchers.is(false) + ); + MatcherAssert.assertThat( + mapping.bool("integer"), + Matchers.is(false) + ); + MatcherAssert.assertThat( + mapping.bool("missing"), + Matchers.is(false) + ); + } + /** * Get a YamlMapping for test. * @return YamlMapping. @@ -134,6 +155,8 @@ private YamlMapping mapping() { .add("long", "32165498") .add("localDate", "2007-12-03") .add("localDateTime", "2007-12-03T10:15:30") + .add("trueValue", "true") + .add("falseValue", "false") .build(); return mapping; } diff --git a/src/test/java/com/amihaiemil/eoyaml/YamlSequenceTypeCastingTestCase.java b/src/test/java/com/amihaiemil/eoyaml/YamlSequenceTypeCastingTestCase.java index edcd4940..9e0d3e2c 100644 --- a/src/test/java/com/amihaiemil/eoyaml/YamlSequenceTypeCastingTestCase.java +++ b/src/test/java/com/amihaiemil/eoyaml/YamlSequenceTypeCastingTestCase.java @@ -91,6 +91,23 @@ public void returnsDateTime() { Matchers.is(LocalDateTime.parse("2007-12-03T10:15:30")) ); } + + @Test + public void returnsBoolean() { + final YamlSequence sequence = this.sequence(); + MatcherAssert.assertThat( + sequence.bool(5), + Matchers.is(false) + ); + MatcherAssert.assertThat( + sequence.bool(6), + Matchers.is(true) + ); + MatcherAssert.assertThat( + sequence.bool(7), + Matchers.is(false) + ); + } /** * Get a YamlSequence for test. @@ -104,6 +121,8 @@ private YamlSequence sequence() { .add("32165498") .add("2007-12-03") .add("2007-12-03T10:15:30") + .add("true") + .add("false") .build(); return sequence; }