Added at function

This commit is contained in:
Shekhar Gulati
2016-05-03 20:05:19 +05:30
parent d7910e724e
commit e6d4baa0b8
3 changed files with 45 additions and 4 deletions
+11 -1
View File
@@ -1,7 +1,7 @@
strman-java strman-java
------ ------
A Java string manipulation library without any dependencies. It is inspired by [dleitee/strman](https://github.com/dleitee/strman). A Java 8 string manipulation library without any dependencies. It is inspired by [dleitee/strman](https://github.com/dleitee/strman).
## Available Functions ## Available Functions
@@ -32,3 +32,13 @@ String title = "s";
String result = appendArray(title, new String[]{"tr","m","an"}); String result = appendArray(title, new String[]{"tr","m","an"});
// result = "strman" // result = "strman"
``` ```
## at(value, index)
Gets the character at index.
```java
import static strman.Strman.at;
String result = at("foobar", 0);
// result = "f"
```
+22 -1
View File
@@ -1,5 +1,6 @@
package strman; package strman;
import java.util.Optional;
import java.util.StringJoiner; import java.util.StringJoiner;
/** /**
@@ -26,7 +27,7 @@ public interface Strman {
* @param appends an array of strings to append * @param appends an array of strings to append
* @return full String * @return full String
*/ */
static String appendArray(String value, String[] appends) { static String appendArray(final String value, final String[] appends) {
if (value == null) { if (value == null) {
throw new IllegalArgumentException("'value' should be not null."); throw new IllegalArgumentException("'value' should be not null.");
} }
@@ -39,4 +40,24 @@ public interface Strman {
} }
return value + joiner.toString(); return value + joiner.toString();
} }
/**
* Get the character at index. This method will take care of negative indexes.
* The valid value of index is between -(length-1) to (length-1).
* For values which don't fall under this range Optional.empty will be returned.
*
* @param value input value
* @param index location
* @return an Optional String if found else empty
*/
static Optional<String> at(final String value, int index) {
if (value == null || value.isEmpty()) {
return Optional.empty();
}
int length = value.length();
if (index < 0) {
index = length + index;
}
return (index < length && index >= 0) ? Optional.of(String.valueOf(value.charAt(index))) : Optional.empty();
}
} }
+12 -2
View File
@@ -2,10 +2,13 @@ package strman;
import org.junit.Test; import org.junit.Test;
import java.util.Optional;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static strman.Strman.append; import static strman.Strman.append;
import static strman.Strman.appendArray; import static strman.Strman.appendArray;
import static strman.Strman.at;
public class StrmanTest { public class StrmanTest {
@@ -28,11 +31,18 @@ public class StrmanTest {
assertThat(appendArray("", new String[]{"foobar"}), equalTo("foobar")); assertThat(appendArray("", new String[]{"foobar"}), equalTo("foobar"));
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void appendArrayShouldThrowIllegalArgumentExceptionWhenValueIsNull() throws Exception { public void appendArrayShouldThrowIllegalArgumentExceptionWhenValueIsNull() throws Exception {
appendArray(null, new String[]{}); appendArray(null, new String[]{});
} }
@Test
public void shouldFindCharacterAtIndex() throws Exception {
assertThat(at("foobar", 0), equalTo(Optional.of("f")));
assertThat(at("foobar", 1), equalTo(Optional.of("o")));
assertThat(at("foobar", -1), equalTo(Optional.of("r")));
assertThat(at("foobar", -2), equalTo(Optional.of("a")));
assertThat(at("foobar", 10), equalTo(Optional.empty()));
assertThat(at("foobar", -10), equalTo(Optional.empty()));
}
} }