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 29d4dde + 7d63c75 commit b9f53f1
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
89 changes: 89 additions & 0 deletions src/main/java/com/amihaiemil/camel/CachedYamlLine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* 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;

/**
* Decorator class to cache values of trimmed() and indentation() method for
* a YamlLine.
* @author Sherif Waly (sherifwaly95@gmail.com)
* @version $Id$
* @since 1.0.0
*
*/
final class CachedYamlLine implements YamlLine {

/**
* Content line.
*/
private YamlLine line;

/**
* Cached trimmed line.
*/
private String trimmed;

/**
* Cached indentation.
*/
private int indentation = -1;

/**
* Ctor.
* @param line YamlLine
*/
CachedYamlLine(final YamlLine line) {
this.line = line;
}

@Override
public int compareTo(final YamlLine other) {
return this.line.compareTo(other);
}

@Override
public String trimmed() {
if(this.trimmed == null) {
this.trimmed = this.line.trimmed();
}
return this.trimmed;
}

@Override
public int number() {
return this.line.number();
}

@Override
public int indentation() {
if(this.indentation == -1) {
this.indentation = this.line.indentation();
}
return this.indentation;
}

}
79 changes: 79 additions & 0 deletions src/test/java/com/amihaiemil/camel/CachedYamlLineTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* 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 org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Unit Tests for {@link CachedYamlLine}.
* @author Sherif Waly (sherifwaly95@gmail.com)
* @version $Id$
* @since 1.0.0
*
*/
public final class CachedYamlLineTest {

/**
* CachedIndentedLine knows its number.
*/
@Test
public void knowsNumber() {
YamlLine line = new CachedYamlLine(
new RtYamlLine("this line", 12)
);
MatcherAssert.assertThat(line.number(), Matchers.is(12));
}

/**
* CachedIndentedLine caches trimmed String and returns same object.
*/
@Test
public void cachesTrimmedValue() {
YamlLine line = new CachedYamlLine(
new RtYamlLine(" this line ", 12)
);
String cached = line.trimmed();
MatcherAssert.assertThat(cached, Matchers.is("this line"));

//Same String object is returned.
MatcherAssert.assertThat(cached == line.trimmed(), Matchers.is(true));
}

/**
* CachedIndentedLine caches indentation value and doesn't recalculate it.
*/
@Test
public void cachesIndentationValue() {
YamlLine line = new CachedYamlLine(
new RtYamlLine(" this line", 12)
);
MatcherAssert.assertThat(line.indentation(), Matchers.is(12));
}
}

0 comments on commit b9f53f1

Please sign in to comment.