mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-09-27 13:33:58 +00:00
Fixed #29. Added support for shuffle function
This commit is contained in:
@@ -96,17 +96,8 @@ public abstract class Strman {
|
||||
* @return character array
|
||||
*/
|
||||
public static String[] chars(final String value) {
|
||||
/**
|
||||
* The other implementation of this could be using String's split method
|
||||
* String[] chars = value.split("")
|
||||
*/
|
||||
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
|
||||
int length = value.length();
|
||||
String[] result = new String[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
result[i] = at(value, i).get();
|
||||
}
|
||||
return result;
|
||||
return value.split("");
|
||||
}
|
||||
|
||||
|
||||
@@ -908,6 +899,25 @@ public abstract class Strman {
|
||||
return html.chars().mapToObj(c -> "\\u" + String.format("%04x", c).toUpperCase()).map(e -> HtmlEntities.encodedEntities.get(e)).collect(joining());
|
||||
}
|
||||
|
||||
/**
|
||||
* It returns a string with its characters in random order.
|
||||
*
|
||||
* @param value The input String
|
||||
* @return The shuffled String
|
||||
*/
|
||||
public static String shuffle(final String value) {
|
||||
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
|
||||
String[] chars = chars(value);
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
int r = random.nextInt(chars.length);
|
||||
String tmp = chars[i];
|
||||
chars[i] = chars[r];
|
||||
chars[r] = tmp;
|
||||
}
|
||||
return Arrays.stream(chars).collect(joining());
|
||||
}
|
||||
|
||||
public static String decode(final String value, final int digits, final int radix) {
|
||||
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
|
||||
return Arrays
|
||||
|
||||
@@ -6,12 +6,12 @@ import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.hasItems;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining;
|
||||
import static org.hamcrest.collection.IsArrayWithSize.emptyArray;
|
||||
import static org.junit.Assert.*;
|
||||
import static strman.Strman.*;
|
||||
import static strman.Strman.endsWith;
|
||||
import static strman.Strman.format;
|
||||
|
||||
public class StrmanTest {
|
||||
@@ -663,4 +663,13 @@ public class StrmanTest {
|
||||
assertThat(htmlEncode("Ж"), equalTo("Ж"));
|
||||
assertThat(htmlEncode("┐"), equalTo("┐"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shuffle_shouldShuffleAString() throws Exception {
|
||||
assertThat(shuffle("shekhar"), not(equalTo("shekhar")));
|
||||
assertThat(shuffle("strman"), not(equalTo("strman")));
|
||||
assertThat(shuffle(""), equalTo(""));
|
||||
assertThat(shuffle("s"), equalTo("s"));
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user