mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-10-21 23:43:50 +00:00
added append function
This commit is contained in:
@@ -1,9 +1,31 @@
|
||||
package strman;
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
/**
|
||||
* A String manipulation library without any dependencies
|
||||
*/
|
||||
public interface Strman {
|
||||
|
||||
|
||||
/**
|
||||
* Appends Strings to value
|
||||
*
|
||||
* @param value initial String
|
||||
* @param appends -- an array of strings to append
|
||||
* @return full string
|
||||
*/
|
||||
static String append(final String value, final String... appends) throws IllegalArgumentException {
|
||||
if (value == null) {
|
||||
throw new IllegalArgumentException("'value' should be not null.");
|
||||
}
|
||||
if (appends == null || appends.length == 0) {
|
||||
return value;
|
||||
}
|
||||
StringJoiner joiner = new StringJoiner("");
|
||||
for (String append : appends) {
|
||||
joiner.add(append);
|
||||
}
|
||||
return value + joiner.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,21 @@ package strman;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static strman.Strman.append;
|
||||
|
||||
public class StrmanTest {
|
||||
|
||||
@Test
|
||||
public void shouldAppendStringsToEndOfValue() throws Exception {
|
||||
assertThat(append("f", "o", "o", "b", "a", "r"), equalTo("foobar"));
|
||||
assertThat(append("foobar"), equalTo("foobar"));
|
||||
assertThat(append("", "foobar"), equalTo("foobar"));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void appendShouldThrowIllegalArgumentExceptionWhenValueIsNull() throws Exception {
|
||||
append(null);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user