Skip to content

Commit

Permalink
#512 MutableYamlSequenceBuilder implemented and tested
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Aug 27, 2022
1 parent 785642b commit 533f585
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 12 deletions.
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;
}
}
10 changes: 9 additions & 1 deletion src/main/java/com/amihaiemil/eoyaml/Yaml.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,21 @@ public static YamlMappingBuilder createMutableYamlMappingBuilder() {
}

/**
* Create a {@link YamlSequenceBuilder}.
* Create an immutable, thread-safe, {@link YamlSequenceBuilder}.
* @return Builder of YamlMapping.
*/
public static YamlSequenceBuilder createYamlSequenceBuilder() {
return new RtYamlSequenceBuilder();
}

/**
* Create a mutable, NOT thread-safe, {@link YamlSequenceBuilder}.
* @return Builder of YamlMapping.
*/
public static YamlSequenceBuilder createMutableYamlSequenceBuilder() {
return new MutableYamlSequenceBuilder();
}

/**
* Create a {@link YamlScalarBuilder}.
* @return Builder of Yaml Scalars.
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/amihaiemil/eoyaml/YamlMappingBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/amihaiemil/eoyaml/YamlSequenceBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down
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")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void buildsYamlSequence() {
* referring to it.
*/
@Test
public void buildsYamlMappingWithComment() {
public void buildsYamlSequenceWithComment() {
final YamlSequence seq = new RtYamlSequenceBuilder()
.add("element 1")
.add("element 2")
Expand Down
22 changes: 20 additions & 2 deletions src/test/java/com/amihaiemil/eoyaml/YamlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,30 @@ public void createsMutableYamlMappingBuilder() {
}

/**
* Yaml can create a YamlSequenceBuilder.
* Yaml can create an immutable YamlSequenceBuilder.
*/
@Test
public void createsYamlSequenceBuilder() {
MatcherAssert.assertThat(
Yaml.createYamlSequenceBuilder(), Matchers.notNullValue()
Yaml.createYamlSequenceBuilder(),
Matchers.allOf(
Matchers.notNullValue(),
Matchers.instanceOf(RtYamlSequenceBuilder.class)
)
);
}

/**
* Yaml can create a mutable YamlSequenceBuilder.
*/
@Test
public void createsMutablYamlSequenceBuilder() {
MatcherAssert.assertThat(
Yaml.createMutableYamlSequenceBuilder(),
Matchers.allOf(
Matchers.notNullValue(),
Matchers.instanceOf(MutableYamlSequenceBuilder.class)
)
);
}

Expand Down

0 comments on commit 533f585

Please sign in to comment.