mirror of
https://github.com/java-diff-utils/java-diff-utils.git
synced 2026-03-13 10:11:17 +08:00
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
/target/
|
||||
/target/
|
||||
/nbproject/
|
||||
@@ -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<String> normalize(List<String> list) {
|
||||
List<String> result = new LinkedList<>();
|
||||
for (String line : list) {
|
||||
result.add(normalize(line));
|
||||
}
|
||||
return result;
|
||||
return list.stream()
|
||||
.map(StringUtils::normalize)
|
||||
.collect(toList());
|
||||
}
|
||||
|
||||
public static List<String> wrapText(List<String> list, int columnWidth) {
|
||||
List<String> 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 = "<br>".length();
|
||||
if (columnWidth <= 0) {
|
||||
throw new IllegalArgumentException("columnWidth may not be less or equal 0");
|
||||
}
|
||||
int length = line.length();
|
||||
int delimiter = "<br/>".length();
|
||||
int widthIndex = columnWidth;
|
||||
|
||||
for (int count = 0; lenght > widthIndex; count++) {
|
||||
line = line.subSequence(0, widthIndex + delimiter * count) + "<br>"
|
||||
+ line.substring(widthIndex + delimiter * count);
|
||||
StringBuilder b = new StringBuilder(line);
|
||||
|
||||
for (int count = 0; length > widthIndex; count++) {
|
||||
b.insert(widthIndex + delimiter * count, "<br/>");
|
||||
widthIndex += columnWidth;
|
||||
}
|
||||
|
||||
return line;
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
|
||||
private StringUtils() {
|
||||
}
|
||||
}
|
||||
|
||||
91
src/test/java/difflib/text/StringUtilsTest.java
Normal file
91
src/test/java/difflib/text/StringUtilsTest.java
Normal file
@@ -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>"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<br/>st", StringUtils.wrapText("test", 2));
|
||||
assertEquals("tes<br/>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));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user