diff --git a/.gitignore b/.gitignore index a6f89c2..0643ce7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/target/ \ No newline at end of file +/target/ +/nbproject/ \ No newline at end of file diff --git a/src/main/java/difflib/text/StringUtils.java b/src/main/java/difflib/text/StringUtils.java index 1a04dfb..317d700 100644 --- a/src/main/java/difflib/text/StringUtils.java +++ b/src/main/java/difflib/text/StringUtils.java @@ -21,6 +21,7 @@ package difflib.text; import java.util.LinkedList; import java.util.List; +import static java.util.stream.Collectors.toList; final class StringUtils { @@ -39,19 +40,15 @@ final class StringUtils { } public static List normalize(List list) { - List result = new LinkedList<>(); - for (String line : list) { - result.add(normalize(line)); - } - return result; + return list.stream() + .map(StringUtils::normalize) + .collect(toList()); } public static List wrapText(List list, int columnWidth) { - List result = new LinkedList<>(); - for (String line : list) { - result.add(wrapText(line, columnWidth)); - } - return result; + return list.stream() + .map(line -> wrapText(line, columnWidth)) + .collect(toList()); } /** @@ -62,19 +59,23 @@ final class StringUtils { * @return the wrapped text */ public static String wrapText(String line, int columnWidth) { - int lenght = line.length(); - int delimiter = "
".length(); + if (columnWidth <= 0) { + throw new IllegalArgumentException("columnWidth may not be less or equal 0"); + } + int length = line.length(); + int delimiter = "
".length(); int widthIndex = columnWidth; - for (int count = 0; lenght > widthIndex; count++) { - line = line.subSequence(0, widthIndex + delimiter * count) + "
" - + line.substring(widthIndex + delimiter * count); + StringBuilder b = new StringBuilder(line); + + for (int count = 0; length > widthIndex; count++) { + b.insert(widthIndex + delimiter * count, "
"); widthIndex += columnWidth; } - return line; + return b.toString(); } - + private StringUtils() { } } diff --git a/src/test/java/difflib/text/StringUtilsTest.java b/src/test/java/difflib/text/StringUtilsTest.java new file mode 100644 index 0000000..1823d11 --- /dev/null +++ b/src/test/java/difflib/text/StringUtilsTest.java @@ -0,0 +1,91 @@ +/* + * Copyright 2017 java-diff-utils. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package difflib.text; + +import java.util.Collections; +import java.util.List; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author tw + */ +public class StringUtilsTest { + + public StringUtilsTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of htmlEntites method, of class StringUtils. + */ + @Test + public void testHtmlEntites() { + assertEquals("<test>", StringUtils.htmlEntites("")); + } + + /** + * Test of normalize method, of class StringUtils. + */ + @Test + public void testNormalize_String() { + assertEquals(" test",StringUtils.normalize("\ttest")); + } + + /** + * Test of normalize method, of class StringUtils. + */ + @Test + public void testNormalize_List() { + assertEquals(Collections.singletonList(" test"),StringUtils.normalize(Collections.singletonList("\ttest"))); + } + + /** + * Test of wrapText method, of class StringUtils. + */ + @Test + public void testWrapText_String_int() { + assertEquals("te
st", StringUtils.wrapText("test", 2)); + assertEquals("tes
t", StringUtils.wrapText("test", 3)); + assertEquals("test", StringUtils.wrapText("test", 10)); + } + + @Test(expected = IllegalArgumentException.class) + public void testWrapText_String_int_zero() { + assertEquals("test", StringUtils.wrapText("test", 0)); + } + +}