Skip to content

Commit

Permalink
AbstractYamlLines.indent(int) scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Mar 7, 2017
1 parent 05ff0c7 commit 29d4dde
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/main/java/com/amihaiemil/camel/AbstractYamlLines.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ abstract class AbstractYamlLines implements Iterable<YamlLine> {
*/
abstract int count();

/**
* Indent these lines.
* @param indentation Spaces to precede each line.
* @return String with the pretty-printed, indented lines.
*/
abstract String indent (int indentation);

/**
* Turn these lines into a YamlNode.
* @return YamlNode
Expand All @@ -64,5 +71,5 @@ YamlNode toYamlNode() {
}
return node;
}

}
6 changes: 6 additions & 0 deletions src/main/java/com/amihaiemil/camel/OrderedYamlLines.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,10 @@ int count() {
return this.unordered.count();
}

@Override
String indent(int indentation) {
// TODO Auto-generated method stub
return null;
}

}
24 changes: 24 additions & 0 deletions src/main/java/com/amihaiemil/camel/RtYamlLines.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,29 @@ int count() {
return this.lines.size();
}

@Override
String indent(final int indentation) {
final StringBuilder indented = new StringBuilder();
final Iterator<YamlLine> linesIt = this.lines.iterator();
final YamlLine first = linesIt.next();
if (first.indentation() == indentation) {
indented.append(first.toString()).append("\n");
while (linesIt.hasNext()) {
indented.append(linesIt.next().toString()).append("\n");
}
} else {
final int offset = indentation - first.indentation();
for (YamlLine line : lines) {
int correct = line.indentation() + offset;
while (correct > 0) {
indented.append(" ");
correct--;
}
indented.append(line.trimmed()).append("\n");
}
}
return indented.toString();
}


}
15 changes: 15 additions & 0 deletions src/test/java/com/amihaiemil/camel/RtYamlLinesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ public void iteratesRight() {
MatcherAssert.assertThat(iterator.hasNext(), Matchers.is(false));
}

/**
* RtYamlLines can indent the lines properly.
*/
@Test
public void indentsRight() {
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(" second: something", 3));
lines.add(new RtYamlLine(" third: somethingElse", 4));
final AbstractYamlLines yaml = new RtYamlLines(lines);
System.out.println(yaml.indent(0));
}

/**
* RtYamlLines can return nested lines for a given line.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/com/amihaiemil/camel/ScalarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public void hasNoChildren() {
MatcherAssert.assertThat(
scl.children(), Matchers.emptyIterable()
);
for (int i=0; i<10;i++) {
for (int j=0; j<10;j++) {
System.out.print(i + " x " + j + " = " + i*j + " | ");
}
System.out.println();
}
}

/**
Expand Down

0 comments on commit 29d4dde

Please sign in to comment.