Skip to content

Commit af089de

Browse files
Richard Domanderctrueden
authored andcommitted
Add String utils
1 parent 4711e59 commit af089de

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed

src/main/java/org/scijava/util/StringUtils.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,12 @@
6969
* @author Curtis Rueden
7070
* @author Chris Allan
7171
* @author Melissa Linkert
72+
* @author Richard Domander (Royal Veterinary College, London)
7273
*/
7374
public final class StringUtils {
7475

76+
public static final char DEFAULT_PAD_CHAR = ' ';
77+
7578
private StringUtils() {
7679
// NB: prevent instantiation of utility class.
7780
}
@@ -130,4 +133,74 @@ public static String sanitize(final String s) {
130133
return buf.toString();
131134
}
132135

136+
public static boolean isNullOrEmpty(final String s) {
137+
return s == null || s.isEmpty();
138+
}
139+
140+
/**
141+
* Calls {@link #padEnd(String, int, char)} with the {@link #DEFAULT_PAD_CHAR}
142+
*/
143+
public static String padEnd(final String s, final int length) {
144+
return padEnd(s, length, DEFAULT_PAD_CHAR);
145+
}
146+
147+
/**
148+
* Adds characters to the end of the {@link String} to make it the given
149+
* length
150+
*
151+
* @param s the original string
152+
* @param length the length of the string with padding
153+
* @param padChar the character added to the end
154+
* @return the end padded {@link String}. Null if s is null, s if no padding
155+
* is not necessary
156+
*/
157+
public static String padEnd(final String s, final int length,
158+
final char padChar)
159+
{
160+
if (s == null) {
161+
return null;
162+
}
163+
164+
final StringBuilder builder = new StringBuilder(s);
165+
final int padding = length - s.length();
166+
for (int i = 0; i < padding; i++) {
167+
builder.append(padChar);
168+
}
169+
170+
return builder.toString();
171+
}
172+
173+
/**
174+
* Calls {@link #padStart(String, int, char)} with the
175+
* {@link #DEFAULT_PAD_CHAR}
176+
*/
177+
public static String padStart(final String s, final int length) {
178+
return padStart(s, length, DEFAULT_PAD_CHAR);
179+
}
180+
181+
/**
182+
* Adds characters to the start of the {@link String} to make it the given
183+
* length
184+
*
185+
* @param s the original string
186+
* @param length the length of the string with padding
187+
* @param padChar the character added to the start
188+
* @return the start padded {@link String}. Null if s is null, s if no padding
189+
* is not necessary
190+
*/
191+
public static String padStart(final String s, final int length,
192+
final char padChar)
193+
{
194+
if (s == null) {
195+
return null;
196+
}
197+
198+
final StringBuilder builder = new StringBuilder();
199+
final int padding = length - s.length();
200+
for (int i = 0; i < padding; i++) {
201+
builder.append(padChar);
202+
}
203+
204+
return builder.append(s).toString();
205+
}
133206
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
2+
package org.scijava.util;
3+
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertFalse;
6+
import static org.junit.Assert.assertNotNull;
7+
import static org.junit.Assert.assertNull;
8+
import static org.junit.Assert.assertTrue;
9+
10+
import org.junit.Test;
11+
12+
/**
13+
* Tests for the utility methods in {@link StringUtils}
14+
*
15+
* @author Richard Domander (Royal Veterinary College, London)
16+
*/
17+
public class StringUtilsTest {
18+
19+
@Test
20+
public void isNullOrEmptyFalseIfString() throws Exception {
21+
assertFalse(StringUtils.isNullOrEmpty("Fresh out of Red Leicester"));
22+
}
23+
24+
@Test
25+
public void isNullOrEmpty() throws Exception {
26+
assertTrue(StringUtils.isNullOrEmpty(null));
27+
assertTrue(StringUtils.isNullOrEmpty(""));
28+
}
29+
30+
@Test
31+
public void padEndNullStringReturnsNull() throws Exception {
32+
assertNull(StringUtils.padEnd(null, 5, '*'));
33+
}
34+
35+
@Test
36+
public void padEndEmptyString() throws Exception {
37+
final int length = 5;
38+
final char padChar = '*';
39+
40+
final String padded = StringUtils.padEnd("", length, padChar);
41+
assertNotNull(padded);
42+
assertFalse(padded.isEmpty());
43+
assertEquals(length, padded.length());
44+
assertTrue(padded.chars().allMatch(c -> c == padChar));
45+
}
46+
47+
@Test
48+
public void padEndLengthSmaller() throws Exception {
49+
final String s = "Eric the fruit bat";
50+
51+
final String padded = StringUtils.padEnd(s, 3, '*');
52+
53+
assertEquals(s, padded);
54+
}
55+
56+
@Test
57+
public void padEndLengthEqual() throws Exception {
58+
final String s = "Eric the cat";
59+
60+
final String padded = StringUtils.padEnd(s, s.length(), '*');
61+
62+
assertEquals(s, padded);
63+
}
64+
65+
@Test
66+
public void padEndLengthNegative() throws Exception {
67+
final String s = "Eric the dog";
68+
69+
final String padded = StringUtils.padEnd(s, -1, '~');
70+
71+
assertEquals(s, padded);
72+
}
73+
74+
@Test
75+
public void padEnd() throws Exception {
76+
final String s = "Eric the halibut";
77+
final int newLength = s.length() + 5;
78+
79+
final String padded = StringUtils.padEnd(s, newLength);
80+
81+
assertEquals(newLength, padded.length());
82+
final String padding = padded.substring(padded.length() - 5);
83+
assertTrue(padding.chars().allMatch(c -> c == StringUtils.DEFAULT_PAD_CHAR));
84+
assertEquals(s, padded.substring(0, padded.length() - 5));
85+
}
86+
87+
@Test
88+
public void padStartNullStringReturnsNull() throws Exception {
89+
assertNull(StringUtils.padStart(null, 5, '*'));
90+
}
91+
92+
@Test
93+
public void padStartEmptyString() throws Exception {
94+
final int length = 5;
95+
final char padChar = '*';
96+
97+
final String padded = StringUtils.padStart("", length, padChar);
98+
assertNotNull(padded);
99+
assertFalse(padded.isEmpty());
100+
assertEquals(length, padded.length());
101+
assertTrue(padded.chars().allMatch(c -> c == padChar));
102+
}
103+
104+
@Test
105+
public void padStartLengthSmaller() throws Exception {
106+
final String s = "Eric the dog";
107+
108+
final String padded = StringUtils.padStart(s, 3, '*');
109+
110+
assertEquals(s, padded);
111+
}
112+
113+
@Test
114+
public void padStartLengthEqual() throws Exception {
115+
final String s = "Simon the prawn";
116+
117+
final String padded = StringUtils.padStart(s, s.length(), '*');
118+
119+
assertEquals(s, padded);
120+
}
121+
122+
@Test
123+
public void padStartLengthNegative() throws Exception {
124+
final String s = "Norman the pike";
125+
126+
final String padded = StringUtils.padStart(s, -1, '~');
127+
128+
assertEquals(s, padded);
129+
}
130+
131+
@Test
132+
public void padStart() throws Exception {
133+
final String s = "Norman the pike";
134+
final int newLength = s.length() + 5;
135+
136+
final String padded = StringUtils.padStart(s, newLength);
137+
138+
assertEquals(newLength, padded.length());
139+
final String padding = padded.substring(0, 5);
140+
assertTrue(padding.chars().allMatch(c -> c == StringUtils.DEFAULT_PAD_CHAR));
141+
assertEquals(s, padded.substring(5, padded.length()));
142+
}
143+
}

0 commit comments

Comments
 (0)