Skip to content

Commit 6a70054

Browse files
committed
1 parent a695dee commit 6a70054

File tree

3 files changed

+111
-18
lines changed

3 files changed

+111
-18
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
/target/
1+
/target/
2+
/nbproject/

src/main/java/difflib/text/StringUtils.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.util.LinkedList;
2323
import java.util.List;
24+
import static java.util.stream.Collectors.toList;
2425

2526
final class StringUtils {
2627

@@ -39,19 +40,15 @@ public static String normalize(String str) {
3940
}
4041

4142
public static List<String> normalize(List<String> list) {
42-
List<String> result = new LinkedList<>();
43-
for (String line : list) {
44-
result.add(normalize(line));
45-
}
46-
return result;
43+
return list.stream()
44+
.map(StringUtils::normalize)
45+
.collect(toList());
4746
}
4847

4948
public static List<String> wrapText(List<String> list, int columnWidth) {
50-
List<String> result = new LinkedList<>();
51-
for (String line : list) {
52-
result.add(wrapText(line, columnWidth));
53-
}
54-
return result;
49+
return list.stream()
50+
.map(line -> wrapText(line, columnWidth))
51+
.collect(toList());
5552
}
5653

5754
/**
@@ -62,19 +59,23 @@ public static List<String> wrapText(List<String> list, int columnWidth) {
6259
* @return the wrapped text
6360
*/
6461
public static String wrapText(String line, int columnWidth) {
65-
int lenght = line.length();
66-
int delimiter = "<br>".length();
62+
if (columnWidth <= 0) {
63+
throw new IllegalArgumentException("columnWidth may not be less or equal 0");
64+
}
65+
int length = line.length();
66+
int delimiter = "<br/>".length();
6767
int widthIndex = columnWidth;
6868

69-
for (int count = 0; lenght > widthIndex; count++) {
70-
line = line.subSequence(0, widthIndex + delimiter * count) + "<br>"
71-
+ line.substring(widthIndex + delimiter * count);
69+
StringBuilder b = new StringBuilder(line);
70+
71+
for (int count = 0; length > widthIndex; count++) {
72+
b.insert(widthIndex + delimiter * count, "<br/>");
7273
widthIndex += columnWidth;
7374
}
7475

75-
return line;
76+
return b.toString();
7677
}
77-
78+
7879
private StringUtils() {
7980
}
8081
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2017 java-diff-utils.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package difflib.text;
17+
18+
import java.util.Collections;
19+
import java.util.List;
20+
import org.junit.After;
21+
import org.junit.AfterClass;
22+
import org.junit.Before;
23+
import org.junit.BeforeClass;
24+
import org.junit.Test;
25+
import static org.junit.Assert.*;
26+
27+
/**
28+
*
29+
* @author tw
30+
*/
31+
public class StringUtilsTest {
32+
33+
public StringUtilsTest() {
34+
}
35+
36+
@BeforeClass
37+
public static void setUpClass() {
38+
}
39+
40+
@AfterClass
41+
public static void tearDownClass() {
42+
}
43+
44+
@Before
45+
public void setUp() {
46+
}
47+
48+
@After
49+
public void tearDown() {
50+
}
51+
52+
/**
53+
* Test of htmlEntites method, of class StringUtils.
54+
*/
55+
@Test
56+
public void testHtmlEntites() {
57+
assertEquals("&lt;test&gt;", StringUtils.htmlEntites("<test>"));
58+
}
59+
60+
/**
61+
* Test of normalize method, of class StringUtils.
62+
*/
63+
@Test
64+
public void testNormalize_String() {
65+
assertEquals(" test",StringUtils.normalize("\ttest"));
66+
}
67+
68+
/**
69+
* Test of normalize method, of class StringUtils.
70+
*/
71+
@Test
72+
public void testNormalize_List() {
73+
assertEquals(Collections.singletonList(" test"),StringUtils.normalize(Collections.singletonList("\ttest")));
74+
}
75+
76+
/**
77+
* Test of wrapText method, of class StringUtils.
78+
*/
79+
@Test
80+
public void testWrapText_String_int() {
81+
assertEquals("te<br/>st", StringUtils.wrapText("test", 2));
82+
assertEquals("tes<br/>t", StringUtils.wrapText("test", 3));
83+
assertEquals("test", StringUtils.wrapText("test", 10));
84+
}
85+
86+
@Test(expected = IllegalArgumentException.class)
87+
public void testWrapText_String_int_zero() {
88+
assertEquals("test", StringUtils.wrapText("test", 0));
89+
}
90+
91+
}

0 commit comments

Comments
 (0)