-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#512 MutableYamlSequenceBuilder implemented and tested
- Loading branch information
1 parent
785642b
commit 533f585
Showing
7 changed files
with
230 additions
and
12 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
src/main/java/com/amihaiemil/eoyaml/MutableYamlSequenceBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Copyright (c) 2016-2022, 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 java.util.LinkedList; | ||
import java.util.List; | ||
|
||
/** | ||
* YamlSequenceBuilder mutable implementation, for better memory cosumption. | ||
* This class is <b>mutable and NOT thread-safe</b>. | ||
* @author Mihai Andronache ([email protected]) | ||
* @version $Id$ | ||
* @since 6.1.0 | ||
*/ | ||
final class MutableYamlSequenceBuilder implements YamlSequenceBuilder { | ||
/** | ||
* Added nodes. | ||
*/ | ||
private final List<YamlNode> nodes; | ||
|
||
/** | ||
* Default ctor. | ||
*/ | ||
MutableYamlSequenceBuilder() { | ||
this(new LinkedList<>()); | ||
} | ||
|
||
/** | ||
* Constructor. | ||
* @param nodes Nodes used in building the YamlSequence | ||
*/ | ||
MutableYamlSequenceBuilder(final List<YamlNode> nodes) { | ||
this.nodes = nodes; | ||
} | ||
|
||
@Override | ||
public YamlSequenceBuilder add(final String value) { | ||
return this.add(new PlainStringScalar(value)); | ||
} | ||
|
||
@Override | ||
public YamlSequenceBuilder add(final YamlNode node) { | ||
this.nodes.add(node); | ||
return this; | ||
} | ||
|
||
@Override | ||
public YamlSequence build(final String comment) { | ||
YamlSequence sequence = new RtYamlSequence(this.nodes, comment); | ||
if (this.nodes.isEmpty()) { | ||
sequence = new EmptyYamlSequence(sequence); | ||
} | ||
return sequence; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ | |
import java.util.Collection; | ||
|
||
/** | ||
* Builder of YamlMapping. Implementations should be immutable and thread-safe. | ||
* Builder of YamlMapping. | ||
* @author Mihai Andronache ([email protected]) | ||
* @version $Id$ | ||
* @since 1.0.0 | ||
|
@@ -40,31 +40,31 @@ public interface YamlMappingBuilder { | |
* Add a pair to the mapping. | ||
* @param key String | ||
* @param value String | ||
* @return This builder | ||
* @return Builder | ||
*/ | ||
YamlMappingBuilder add(final String key, final String value); | ||
|
||
/** | ||
* Add a pair to the mapping. | ||
* @param key YamlNode (sequence or mapping) | ||
* @param value String | ||
* @return This builder | ||
* @return Builder | ||
*/ | ||
YamlMappingBuilder add(final YamlNode key, final String value); | ||
|
||
/** | ||
* Add a pair to the mapping. | ||
* @param key YamlNode (sequence or mapping) | ||
* @param value YamlNode (sequence or mapping) | ||
* @return This builder | ||
* @return Builder | ||
*/ | ||
YamlMappingBuilder add(final YamlNode key, final YamlNode value); | ||
|
||
/** | ||
* Add a pair to the mapping. | ||
* @param key String | ||
* @param value YamlNode (sequence or mapping) | ||
* @return This builder | ||
* @return Builder | ||
*/ | ||
YamlMappingBuilder add(final String key, final YamlNode value); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ | |
import java.util.Collection; | ||
|
||
/** | ||
* Builder of YamlSequence. Implementations should be immutable and thread-safe. | ||
* Builder of YamlSequence. | ||
* @author Salavat.Yalalov ([email protected]) | ||
* @version $Id$ | ||
* @since 1.0.0 | ||
|
@@ -40,14 +40,14 @@ public interface YamlSequenceBuilder { | |
/** | ||
* Add a value to the sequence. | ||
* @param value String | ||
* @return This builder | ||
* @return Builder | ||
*/ | ||
YamlSequenceBuilder add(final String value); | ||
|
||
/** | ||
* Add a value to the sequence. | ||
* @param node YamlNode | ||
* @return This builder | ||
* @return Builder | ||
*/ | ||
YamlSequenceBuilder add(final YamlNode node); | ||
|
||
|
112 changes: 112 additions & 0 deletions
112
src/test/java/com/amihaiemil/eoyaml/MutableYamlSequenceBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/** | ||
* Copyright (c) 2016-2022, 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.Test; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
/** | ||
* Unit tests for {@link MutableYamlSequenceBuilder}. | ||
* @author Mihai Andronache ([email protected]) | ||
* @version $Id$ | ||
* @since 6.1.0 | ||
*/ | ||
public final class MutableYamlSequenceBuilderTest { | ||
/** | ||
* MutableYamlSequenceBuilder can add a String. | ||
*/ | ||
@Test | ||
public void addsString() { | ||
YamlSequenceBuilder sequenceBuilder = new MutableYamlSequenceBuilder(); | ||
YamlSequenceBuilder withAdded = sequenceBuilder.add("value"); | ||
MatcherAssert.assertThat(withAdded, Matchers.notNullValue()); | ||
MatcherAssert.assertThat( | ||
sequenceBuilder, Matchers.is(withAdded) | ||
); | ||
} | ||
|
||
/** | ||
* MutableYamlSequenceBuilder can add a YamlNode. | ||
*/ | ||
@Test | ||
public void addsYamlNode() { | ||
YamlSequenceBuilder sequenceBuilder = new MutableYamlSequenceBuilder(); | ||
YamlSequenceBuilder withAdded = | ||
sequenceBuilder.add(new PlainStringScalar("value")); | ||
MatcherAssert.assertThat(withAdded, Matchers.notNullValue()); | ||
MatcherAssert.assertThat( | ||
sequenceBuilder, Matchers.is(withAdded) | ||
); | ||
} | ||
|
||
/** | ||
* MutableYamlSequenceBuilder can build a YamlSequence. | ||
*/ | ||
@Test | ||
public void buildsYamlSequence() { | ||
YamlSequenceBuilder sequenceBuilder = new MutableYamlSequenceBuilder(); | ||
List<YamlNode> devs = new LinkedList<>(); | ||
devs.add(new PlainStringScalar("amihaiemil")); | ||
devs.add(new PlainStringScalar("salikjan")); | ||
YamlSequence sequence = sequenceBuilder | ||
.add("amihaiemil") | ||
.add(new RtYamlSequence(devs)) | ||
.build(); | ||
|
||
MatcherAssert.assertThat(sequence, Matchers.notNullValue()); | ||
MatcherAssert.assertThat( | ||
sequence.string(0), Matchers.equalTo("amihaiemil") | ||
); | ||
|
||
MatcherAssert.assertThat( | ||
sequence.yamlSequence(1).values().size(), | ||
Matchers.equalTo(2) | ||
); | ||
} | ||
/** | ||
* MutableYamlSequenceBuilder can build a YamlSequence with a comment | ||
* referring to it. | ||
*/ | ||
@Test | ||
public void buildsYamlSequenceWithComment() { | ||
final YamlSequence seq = new MutableYamlSequenceBuilder() | ||
.add("element 1") | ||
.add("element 2") | ||
.build("some test sequence"); | ||
final Comment com = seq.comment(); | ||
MatcherAssert.assertThat(com.yamlNode(), Matchers.is(seq)); | ||
MatcherAssert.assertThat( | ||
com.value(), | ||
Matchers.equalTo("some test sequence") | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters