Skip to content

Commit

Permalink
Merge branch 'master' into 79
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Mar 7, 2017
2 parents b9f53f1 + ae074b7 commit 08386cf
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 13 deletions.
13 changes: 13 additions & 0 deletions src/main/java/com/amihaiemil/camel/CachedYamlLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ final class CachedYamlLine implements YamlLine {
*/
private int indentation = -1;

/**
* Cached value.
*/
private Boolean hasNestedNode;

/**
* Ctor.
* @param line YamlLine
Expand Down Expand Up @@ -86,4 +91,12 @@ public int indentation() {
return this.indentation;
}

@Override
public boolean hasNestedNode() {
if (this.hasNestedNode == null) {
this.hasNestedNode = this.line.hasNestedNode();
}
return this.hasNestedNode;
}

}
5 changes: 5 additions & 0 deletions src/main/java/com/amihaiemil/camel/EvenlyIndentedLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,9 @@ public String toString() {
public int compareTo(final YamlLine other) {
return this.line.compareTo(other);
}

@Override
public boolean hasNestedNode() {
return this.line.hasNestedNode();
}
}
8 changes: 5 additions & 3 deletions src/main/java/com/amihaiemil/camel/RtYamlInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ private AbstractYamlLines readInput() throws IOException {
while ((line = reader.readLine()) != null) {
final YamlLine current = new RtYamlLine(line, number);
lines.add(
new WellIndentedLine(
previous,
new EvenlyIndentedLine(current)
new CachedYamlLine(
new WellIndentedLine(
previous,
new EvenlyIndentedLine(current)
)
)
);
number++;
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/com/amihaiemil/camel/RtYamlLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 1.0.0
* @todo #52:30min Right now, at every call of trimmed() and indentation()
* the values are calculated. This isn't efficient, so we need a decorator
* to cache these values. Let's name it CachedYamlLine. It should be used
* like this: YamlLine line = new CachedYamlLine(new RtYamlLine(...));
*/
final class RtYamlLine implements YamlLine {

Expand Down Expand Up @@ -95,4 +91,18 @@ public int compareTo(final YamlLine other) {
}
return result;
}

@Override
public boolean hasNestedNode() {
final boolean result;
final String specialCharacters = ":>|-";
final CharSequence prevLineLastChar =
this.trimmed().substring(this.trimmed().length()-1);
if(specialCharacters.contains(prevLineLastChar)) {
result = true;
} else {
result = false;
}
return result;
}
}
11 changes: 6 additions & 5 deletions src/main/java/com/amihaiemil/camel/WellIndentedLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ public int number() {
public int indentation() {
final int lineIndent = this.line.indentation();
final int previousLineIndent = this.previousLine.indentation();
final String specialCharacters = ":>|-";
final CharSequence prevLineLastChar =
this.previousLine.trimmed()
.substring(this.previousLine.trimmed().length()-1);
if(specialCharacters.contains(prevLineLastChar)) {
if(this.previousLine.hasNestedNode()) {
if(lineIndent != previousLineIndent+2) {
throw new IllegalStateException(
"Indentation of line " + this.line.number() + " isn't ok. "
Expand All @@ -102,4 +98,9 @@ public String toString() {
public int compareTo(final YamlLine other) {
return this.line.compareTo(other);
}

@Override
public boolean hasNestedNode() {
return this.line.hasNestedNode();
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/amihaiemil/camel/YamlLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ interface YamlLine extends Comparable<YamlLine> {
*/
int indentation();

/**
* Does this line precede a nested node?
* @return True or false
*/
boolean hasNestedNode();
/**
* YamlLine null object.
*/
Expand All @@ -79,6 +84,11 @@ public int indentation() {
public int compareTo(final YamlLine other) {
return -1;
}

@Override
public boolean hasNestedNode() {
return false;
}

}
}
25 changes: 24 additions & 1 deletion src/test/java/com/amihaiemil/camel/RtYamlLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* @since 1.0.0
*/
public final class RtYamlLineTest {

/**
* RtYamlLine knows its number.
*/
Expand All @@ -48,6 +48,29 @@ public void knowsNumber() {
MatcherAssert.assertThat(line.number(), Matchers.is(12));
}

/**
* RtYamlLine knows if it has a nested YamlNode after it.
*/
@Test
public void precedesYamlNode() {
MatcherAssert.assertThat(
new RtYamlLine("this:", 12).hasNestedNode(),
Matchers.is(true)
);
MatcherAssert.assertThat(
new RtYamlLine("this: |> ", 12).hasNestedNode(),
Matchers.is(true)
);
MatcherAssert.assertThat(
new RtYamlLine("this: |- ", 12).hasNestedNode(),
Matchers.is(true)
);
MatcherAssert.assertThat(
new RtYamlLine("this: value", 12).hasNestedNode(),
Matchers.is(false)
);
}

/**
* RtYamlLine can trim itself.
*/
Expand Down

0 comments on commit 08386cf

Please sign in to comment.