From ac3a3a0e0905ccb7ffb2593bbf0af5d6c52b9193 Mon Sep 17 00:00:00 2001 From: amihaiemil Date: Wed, 8 May 2024 08:30:57 +0300 Subject: [PATCH] YamlMapping.add(..., ...) with convenient types --- .../amihaiemil/eoyaml/YamlMappingBuilder.java | 121 ++++++++++++++++++ .../YamlMappingTypeCastingTestCase.java | 16 +-- 2 files changed, 129 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/amihaiemil/eoyaml/YamlMappingBuilder.java b/src/main/java/com/amihaiemil/eoyaml/YamlMappingBuilder.java index 88992d1e..a4055b4d 100644 --- a/src/main/java/com/amihaiemil/eoyaml/YamlMappingBuilder.java +++ b/src/main/java/com/amihaiemil/eoyaml/YamlMappingBuilder.java @@ -30,6 +30,8 @@ import javax.json.JsonArray; import javax.json.JsonObject; import javax.json.JsonStructure; +import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.Collection; /** @@ -61,6 +63,125 @@ default YamlMappingBuilder add(final String key, final String value) { ); } + /** + * Add a pair to the mapping. + * @param key String + * @param value Char[] + * @return Builder + */ + default YamlMappingBuilder add(final String key, final char[] value) { + return this.add( + new PlainStringScalar(key), + new PlainStringScalar(String.valueOf(value)) + ); + } + + /** + * Add a pair to the mapping. + * @param key String + * @param value Char + * @return Builder + */ + default YamlMappingBuilder add(final String key, final char value) { + return this.add( + new PlainStringScalar(key), + new PlainStringScalar(String.valueOf(value)) + ); + } + + /** + * Add a pair to the mapping. + * @param key String + * @param value Int + * @return Builder + */ + default YamlMappingBuilder add(final String key, final int value) { + return this.add( + new PlainStringScalar(key), + new PlainStringScalar(String.valueOf(value)) + ); + } + + /** + * Add a pair to the mapping. + * @param key String + * @param value Double + * @return Builder + */ + default YamlMappingBuilder add(final String key, final double value) { + return this.add( + new PlainStringScalar(key), + new PlainStringScalar(String.valueOf(value)) + ); + } + + /** + * Add a pair to the mapping. + * @param key String + * @param value Float + * @return Builder + */ + default YamlMappingBuilder add(final String key, final float value) { + return this.add( + new PlainStringScalar(key), + new PlainStringScalar(String.valueOf(value)) + ); + } + + /** + * Add a pair to the mapping. + * @param key String + * @param value Long + * @return Builder + */ + default YamlMappingBuilder add(final String key, final long value) { + return this.add( + new PlainStringScalar(key), + new PlainStringScalar(String.valueOf(value)) + ); + } + + /** + * Add a pair to the mapping. + * @param key String + * @param value LocalDate + * @return Builder + */ + default YamlMappingBuilder add(final String key, final LocalDate value) { + return this.add( + new PlainStringScalar(key), + new PlainStringScalar(String.valueOf(value)) + ); + } + + /** + * Add a pair to the mapping. + * @param key String + * @param value LocalDateTime + * @return Builder + */ + default YamlMappingBuilder add( + final String key, final LocalDateTime value + ) { + return this.add( + new PlainStringScalar(key), + new PlainStringScalar(String.valueOf(value)) + ); + } + + /** + * Add a pair to the mapping. + * @param key String + * @param value Boolean + * @return Builder + */ + default YamlMappingBuilder add(final String key, final boolean value) { + return this.add( + new PlainStringScalar(key), + new PlainStringScalar(String.valueOf(value)) + ); + } + /** * Add a pair to the mapping. * @param key String diff --git a/src/test/java/com/amihaiemil/eoyaml/YamlMappingTypeCastingTestCase.java b/src/test/java/com/amihaiemil/eoyaml/YamlMappingTypeCastingTestCase.java index d2021b60..780cb1c9 100644 --- a/src/test/java/com/amihaiemil/eoyaml/YamlMappingTypeCastingTestCase.java +++ b/src/test/java/com/amihaiemil/eoyaml/YamlMappingTypeCastingTestCase.java @@ -149,14 +149,14 @@ public void returnsBoolean() { */ private YamlMapping mapping() { YamlMapping mapping = Yaml.createYamlMappingBuilder() - .add("integer", "123") - .add("float", "3.54") - .add("double", "2.05") - .add("long", "32165498") - .add("localDate", "2007-12-03") - .add("localDateTime", "2007-12-03T10:15:30") - .add("trueValue", "true") - .add("falseValue", "false") + .add("integer", 123) + .add("float", 3.54) + .add("double", 2.05) + .add("long", 32165498L) + .add("localDate", LocalDate.of(2007, 12, 3)) + .add("localDateTime", LocalDateTime.of(2007, 12, 3, 10, 15, 30)) + .add("trueValue", true) + .add("falseValue", false) .build(); return mapping; }