Skip to content

Commit

Permalink
OrderedYamlLines, fixes and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Feb 28, 2017
1 parent 391a70c commit 05ff0c7
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 31 deletions.
90 changes: 90 additions & 0 deletions src/main/java/com/amihaiemil/camel/OrderedYamlLines.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* Copyright (c) 2016-2017, 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.camel;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
* Ordered YamlLines.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 1.0.0
*/
final class OrderedYamlLines extends AbstractYamlLines {

/**
* Lines to order.
*/
private AbstractYamlLines unordered;

/**
* Ctor.
* @param unordered Decorated lines.
*/
OrderedYamlLines(final AbstractYamlLines unordered) {
this.unordered = unordered;
}

/**
* Iterates over the lines with the same indentation. The lines
* are ordered.
* @return Iterator over the ordered lines.
*/
@Override
public Iterator<YamlLine> iterator() {
final Iterator<YamlLine> lines = this.unordered.iterator();
final List<YamlLine> ordered = new LinkedList<>();
while (lines.hasNext()) {
ordered.add(lines.next());
}
Collections.sort(ordered);
return ordered.iterator();
}

/**
* Returns the lines which are nested after the given line.
* The lines are not necessarily ordered. If the resulting lines
* should be ordered (be iterated in order), then they have
* to be wrapped inside a new OrderedYamlLines.
* @return AbstractYamlLines
* @param after The number of the parent line
*/
@Override
AbstractYamlLines nested(final int after) {
return this.unordered.nested(after);
}

@Override
int count() {
return this.unordered.count();
}

}
5 changes: 3 additions & 2 deletions src/main/java/com/amihaiemil/camel/ReadYamlMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ final class ReadYamlMapping extends AbstractYamlMapping {
@Override
public Collection<YamlNode> children() {
final List<YamlNode> kids = new LinkedList<>();
for (final YamlLine line : this.lines) {
final OrderedYamlLines ordered = new OrderedYamlLines(this.lines);
for (final YamlLine line : ordered) {
final String trimmed = line.trimmed();
if("?".equals(trimmed) || ":".equals(trimmed)) {
continue;
} else {
if(trimmed.endsWith(":")) {
kids.add(this.lines.nested(line.number()).toYamlNode());
kids.add(ordered.nested(line.number()).toYamlNode());
} else {
final String[] parts = trimmed.split(":");
if(parts.length < 2) {
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/amihaiemil/camel/RtYamlLines.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -46,7 +45,6 @@ public Iterator<YamlLine> iterator() {
sameIndentation.add(current);
}
}
Collections.sort(sameIndentation);
iterator = sameIndentation.iterator();
}
return iterator;
Expand All @@ -56,7 +54,7 @@ public Iterator<YamlLine> iterator() {
AbstractYamlLines nested(final int after) {
final List<YamlLine> nestedLines = new ArrayList<YamlLine>();
YamlLine start = null;
for(final YamlLine line: this.lines) {
for(final YamlLine line : this.lines) {
if(line.number() == after) {
start = line;
}
Expand Down
123 changes: 123 additions & 0 deletions src/test/java/com/amihaiemil/camel/OrderedYamlLinesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**
* Copyright (c) 2016-2017, 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.camel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Unit tests for {@link OrderedYamlLines}.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 1.0.0
*/
public final class OrderedYamlLinesTest {

/**
* OrderedYamlLines can iterate over the ordered lines properly.
* It should iterate only over the lines which are at the
* same indentation level.
*/
@Test
public void iteratesRight() {
final List<YamlLine> lines = new ArrayList<>();
lines.add(new RtYamlLine("first: ", 0));
lines.add(new RtYamlLine(" - fourth", 1));
lines.add(new RtYamlLine(" - fifth", 2));
lines.add(new RtYamlLine("bay: something", 3));
lines.add(new RtYamlLine("alba: somethingElse", 4));
final Iterator<YamlLine> iterator = new OrderedYamlLines(
new RtYamlLines(lines)
).iterator();
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(4));
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(3));
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(0));
MatcherAssert.assertThat(iterator.hasNext(), Matchers.is(false));
}

/**
* OrderedYamlLines can return nested lines (in initial order) for a given
* line.
*/
@Test
public void returnsNestedLinesRight() {
final List<YamlLine> lines = new ArrayList<>();
lines.add(new RtYamlLine("first: ", 0));
lines.add(new RtYamlLine(" - bay", 1));
lines.add(new RtYamlLine(" - alba", 2));
lines.add(new RtYamlLine("second: something", 3));
lines.add(new RtYamlLine("third: somethingElse", 4));
lines.add(new RtYamlLine(" - sixth", 5));
AbstractYamlLines yamlLines = new RtYamlLines(lines);

Iterator<YamlLine> iterator = yamlLines.nested(0).iterator();
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(1));
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(2));
MatcherAssert.assertThat(iterator.hasNext(), Matchers.is(false));

iterator = yamlLines.nested(1).iterator();
MatcherAssert.assertThat(iterator.hasNext(), Matchers.is(false));

iterator = yamlLines.nested(3).iterator();
MatcherAssert.assertThat(iterator.hasNext(), Matchers.is(false));

iterator = yamlLines.nested(4).iterator();
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(5));
MatcherAssert.assertThat(iterator.hasNext(), Matchers.is(false));
}

/**
* Test to check that YamlLine.compareTo(...) works fine.
*/
@Test
public void yamlLinesAreOrdered() {
final List<YamlLine> lines = new ArrayList<>();
lines.add(new RtYamlLine("Einestien", 0));
lines.add(new RtYamlLine(" Ben", 1));
lines.add(new RtYamlLine("Charles", 2));
lines.add(new RtYamlLine(" Denise", 3));
lines.add(new RtYamlLine(" Albert", 4));
lines.add(new RtYamlLine("Sherif", 5));
Collections.sort(lines);

Iterator<YamlLine> iterator = lines.iterator();
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(4));
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(1));
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(2));
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(3));
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(0));
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(5));
MatcherAssert.assertThat(iterator.hasNext(), Matchers.is(false));
}
}
28 changes: 2 additions & 26 deletions src/test/java/com/amihaiemil/camel/RtYamlLinesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
package com.amihaiemil.camel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -79,8 +78,8 @@ public void returnsNestedLinesRight() {
AbstractYamlLines yamlLines = new RtYamlLines(lines);

Iterator<YamlLine> iterator = yamlLines.nested(0).iterator();
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(2));
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(1));
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(2));
MatcherAssert.assertThat(iterator.hasNext(), Matchers.is(false));

iterator = yamlLines.nested(1).iterator();
Expand All @@ -93,28 +92,5 @@ public void returnsNestedLinesRight() {
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(5));
MatcherAssert.assertThat(iterator.hasNext(), Matchers.is(false));
}

/**
* Test to check YamlLines are sorted correctly.
*/
@Test
public void yamlLinesAreOrdered() {
final List<YamlLine> lines = new ArrayList<>();
lines.add(new RtYamlLine("Einestien", 0));
lines.add(new RtYamlLine(" Ben", 1));
lines.add(new RtYamlLine("Charles", 2));
lines.add(new RtYamlLine(" Denise", 3));
lines.add(new RtYamlLine(" Albert", 4));
lines.add(new RtYamlLine("Sherif", 5));
Collections.sort(lines);

Iterator<YamlLine> iterator = lines.iterator();
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(4));
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(1));
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(2));
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(3));
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(0));
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(5));
MatcherAssert.assertThat(iterator.hasNext(), Matchers.is(false));
}

}

0 comments on commit 05ff0c7

Please sign in to comment.