Skip to content

Commit

Permalink
Added areAnagram, isPalindrome and reverse methods
Browse files Browse the repository at this point in the history
  • Loading branch information
giorgosart committed Jun 12, 2019
1 parent 49e6e47 commit 885a4c0
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 78 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ A simple Java string utility library
```
import static com.github.giorgosart.StringUtils.defaultString;
import static com.github.giorgosart.StringUtils.truncate;
import static com.github.giorgosart.Validator.isBlank;
import static com.github.giorgosart.Validator.isEmpty;
import static com.github.giorgosart.StringUtils.isBlank;
import static com.github.giorgosart.StringUtils.isEmpty;
import com.github.giorgosart.TruncateAt;
public class Main {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>com.github.giorgosart</groupId>
<artifactId>java-string-utils</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<name>java-string-utils</name>
Expand Down
88 changes: 77 additions & 11 deletions src/main/java/com/github/giorgosart/StringUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.giorgosart;

import java.util.Arrays;

/**
* All String operations are null safe
*
Expand All @@ -11,48 +13,112 @@ public class StringUtils {

private StringUtils() { /* cannot be instantiated */ }

/**
* Checks if two words are an anagram of each other
*
* @param firstWord the first word to be examined
* @param secondWord the second word to be examined
* @return true or false based weather the two words are an anagram of each other
* @since 1.1.0
*/
public static boolean areAnagram(String firstWord, String secondWord) {
if (isEmpty(firstWord) || isEmpty(secondWord)) {
return false;
}
return Arrays.equals(firstWord.replaceAll(" ", "").codePoints().sorted().toArray(),
secondWord.replaceAll(" ", "").codePoints().sorted().toArray());
}

/**
* returns the default string passed in if the string to be examined is blank
*
* @param str the string to be examined
* @param defaultString the default string to be used if the str is blank
* @return the defaultString if the str is blank
* @since 1.0.0
*/
public static String defaultString(String str, String defaultString) {
return Validator.isBlank(str) ? defaultString : str;
return isBlank(str) ? defaultString : str;
}

/**
* Checks if the string is null, its length is 0 or a whitespace.
*
* @param str the string to be examined, can be null
* @return true if str is null, its length is 0 or a whitespace
* @since 1.0.0
*/
public static boolean isBlank(String str) {
return (str == null || str.isEmpty() || str.matches("^\\s*$"));
}

/**
* Checks if the string is null or its length is 0.
*
* @param str the string to be examined, can be null
* @return true if str is null or its length is 0
* @since 1.0.0
*/
public static boolean isEmpty(String str) {
return (str == null || str.isEmpty());
}

/**
* Checks whether the passed in string is a palindrome
*
* @param str the string to be checked
* @return true or false based on whether the string is a palindrome
* @since 1.1.0
*/
public static boolean isPalindrome(String str) {
return !isBlank(str) &&
str.replaceAll(" ", "").equalsIgnoreCase(reverse(str.replaceAll(" ", "")));
}

/**
* Reverse the string passed in
*
* @param str the string to be reversed
* @return a reversed version of the string passed in
* @since 1.1.0
*/
public static String reverse(String str) {
return isBlank(str) ? str : new StringBuilder().append(str).reverse().toString();
}

/**
* Returns a truncated string based on the parameters passed in
*
* @param str the string to be examined, can be null
* @param width the expected width of the string
* @param maxWidth the expected maxWidth of the string
* @param trimPosition where should the string be truncated at, either beginning or end
* @return the original string if the length of the string is smaller than width
* @return the original string if the length of the string is smaller than maxWidth
* @since 1.0.0
*/
public static String truncate(String str, int width, String trimPosition) {
return truncate(str, width, trimPosition, false);
public static String truncate(String str, int maxWidth, String trimPosition) {
return truncate(str, maxWidth, trimPosition, false);
}

/**
* Returns a truncated string based on the parameters passed in
*
* @param str the string to be examined, can be null
* @param width the expected width of the string
* @param maxWidth the expected maxWidth of the string
* @param trimPosition where should the string be truncated at, either beginning or end
* @param includeEllipsis whether the ellipsis characters should be included
* @return the original string if the length of the string is smaller than width, otherwise the
* @return the original string if the length of the string is smaller than maxWidth, otherwise the
* truncated string
* @since 1.0.0
*/
public static String truncate(String str, int width, String trimPosition,
public static String truncate(String str, int maxWidth, String trimPosition,
boolean includeEllipsis) {
if (Validator.isBlank(str) || str.length() < width) {
if (isBlank(str) || str.length() < maxWidth) {
return str;
}
if (trimPosition.equals(TruncateAt.START.toString())) {
return includeEllipsis ? str.substring(0, width) + "..." : str.substring(0, width);
return includeEllipsis ? str.substring(0, maxWidth) + "..." : str.substring(0, maxWidth);
} else if (trimPosition.equals(TruncateAt.END.toString())) {
return includeEllipsis ? "..." + str.substring(width) : str.substring(width);
return includeEllipsis ? "..." + str.substring(maxWidth) : str.substring(maxWidth);
}
return str;
}
Expand Down
34 changes: 0 additions & 34 deletions src/main/java/com/github/giorgosart/Validator.java

This file was deleted.

44 changes: 42 additions & 2 deletions src/test/java/com/github/giorgosart/StringUtilsTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.github.giorgosart;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.github.giorgosart.StringUtils;
import com.github.giorgosart.TruncateAt;
import org.junit.jupiter.api.Test;

class StringUtilsTest {
Expand All @@ -29,4 +29,44 @@ void truncate() {

assertNull(StringUtils.truncate(null, 2, TruncateAt.END.name(), true));
}

@Test
void areAnagram() {
assertTrue(StringUtils.areAnagram("nag a ram", "anagram"));
assertFalse(StringUtils.areAnagram(null, null));
}

@Test
void reverse() {
assertEquals(StringUtils.reverse("aabbccdd"), "ddccbbaa");
assertEquals(StringUtils.reverse(""), "");
assertNull(StringUtils.reverse(null));
}

@Test
void isPalindrome() {
assertFalse(StringUtils.isPalindrome(null));
assertTrue(StringUtils.isPalindrome("aabbsbbaa"));
assertTrue(StringUtils.isPalindrome("star buttons not tub rats"));
}

@Test
void iBlankTest() {
assertEquals(StringUtils.isBlank(null), Boolean.TRUE);
assertEquals(StringUtils.isBlank(""), Boolean.TRUE);
assertEquals(StringUtils.isBlank(" "), Boolean.TRUE);
assertEquals(StringUtils.isBlank("some string"), Boolean.FALSE);
assertEquals(StringUtils.isBlank(" "), Boolean.TRUE);
assertEquals(StringUtils.isBlank(" some string "), Boolean.FALSE);
}

@Test
void isEmpty() {
assertEquals(StringUtils.isEmpty(null), Boolean.TRUE);
assertEquals(StringUtils.isEmpty(""), Boolean.TRUE);
assertEquals(StringUtils.isEmpty(" "), Boolean.FALSE);
assertEquals(StringUtils.isEmpty("some string"), Boolean.FALSE);
assertEquals(StringUtils.isEmpty(" "), Boolean.FALSE);
assertEquals(StringUtils.isEmpty(" some string "), Boolean.FALSE);
}
}
28 changes: 0 additions & 28 deletions src/test/java/com/github/giorgosart/ValidatorTest.java

This file was deleted.

0 comments on commit 885a4c0

Please sign in to comment.