@@ -18,6 +18,7 @@ public class StringUtils {
1818 * @return {@link boolean}
1919 */
2020 public static boolean isEmpty (final CharSequence cs ) {
21+ /* String is empty is it is null or length is 0 */
2122 return cs == null || cs .length () == 0 ;
2223 }
2324
@@ -28,9 +29,11 @@ public static boolean isEmpty(final CharSequence cs) {
2829 * @return {@link boolean}
2930 */
3031 public static boolean isAnyEmpty (final CharSequence ... css ) {
32+ /* Input has to be array */
3133 if (ArrayUtils .isEmpty (css )) {
3234 return false ;
3335 }
36+ /* Check for each character */
3437 for (CharSequence cs : css ) {
3538 if (isEmpty (cs )) {
3639 return true ;
@@ -48,9 +51,11 @@ public static boolean isAnyEmpty(final CharSequence... css) {
4851 */
4952 public static boolean isBlank (final CharSequence cs ) {
5053 int strLen ;
54+ /* If string is null or length is 0 */
5155 if (cs == null || (strLen = cs .length ()) == 0 ) {
5256 return true ;
5357 }
58+ /* There should not be any white space character */
5459 for (int i = 0 ; i < strLen ; i ++) {
5560 if (Character .isWhitespace (cs .charAt (i )) == false ) {
5661 return false ;
@@ -66,9 +71,11 @@ public static boolean isBlank(final CharSequence cs) {
6671 * @return {@link boolean}
6772 */
6873 public static boolean isAnyBlank (final CharSequence ... css ) {
74+ /* Input has to be array */
6975 if (ArrayUtils .isEmpty (css )) {
7076 return true ;
7177 }
78+ /* Check for each character, if it is blank */
7279 for (CharSequence cs : css ) {
7380 if (isBlank (cs )) {
7481 return true ;
@@ -84,6 +91,7 @@ public static boolean isAnyBlank(final CharSequence... css) {
8491 * @return {@link String}
8592 */
8693 public static String trim (final String str ) {
94+ /* If string is null, return null else trim */
8795 return str == null ? null : str .trim ();
8896 }
8997
@@ -95,15 +103,80 @@ public static String trim(final String str) {
95103 * @return {@link String}
96104 */
97105 public static String truncate (final String str , final int maxWidth ) {
98- return str ;
106+ return truncate ( str , 0 , maxWidth ) ;
99107 }
100108
109+ /**
110+ * Method to truncate the string when max width
111+ * and offset is given
112+ *
113+ * @param str
114+ * @param offset
115+ * @param maxWidth
116+ * @return {@link String}
117+ */
118+ public static String truncate (final String str , final int offset , final int maxWidth ) {
119+ /* Offset and max width can't be less then 0 */
120+ if (offset < 0 ) {
121+ throw new IllegalArgumentException ("offset cannot be negative" );
122+ }
123+ if (maxWidth < 0 ) {
124+ throw new IllegalArgumentException ("maxWidth cannot be negative" );
125+ }
126+ /* If string is null, return null */
127+ if (str == null ) {
128+ return null ;
129+ }
130+ /* If offset is greater then list length, return empty string */
131+ if (offset > str .length ()) {
132+ return "" ;
133+ }
134+ /* If valid scenario, find where we have to stop */
135+ if (str .length () > maxWidth ) {
136+ final int ix = offset + maxWidth > str .length () ? str .length () : offset + maxWidth ;
137+ return str .substring (offset , ix );
138+ }
139+ return str .substring (offset );
140+ }
141+
142+ /**
143+ * Method to check if two strings are equal
144+ *
145+ * @param cs1
146+ * @param cs2
147+ * @return {@link boolean}
148+ */
101149 public static boolean equals (final CharSequence cs1 , final CharSequence cs2 ) {
150+ /* If there reference is same, they are equal */
151+ if (cs1 == cs2 ) {
152+ return true ;
153+ }
154+ /* If any one of them is null, they can't be equal */
155+ if (cs1 == null || cs2 == null ) {
156+ return false ;
157+ }
158+ /* If there lengths are different, they can't be equal */
159+ if (cs1 .length () != cs2 .length ()) {
160+ return false ;
161+ }
162+ /* Now, both of them has to be string and equal in value */
163+ if (cs1 instanceof String && cs2 instanceof String ) {
164+ return cs1 .equals (cs2 );
165+ }
102166 return false ;
103167 }
104168
169+ /**
170+ * Method to check if two strings are equal when cases are ignored
171+ *
172+ * @param str1
173+ * @param str2
174+ * @return {@link boolean}
175+ */
105176 public static boolean equalsIgnoreCase (final CharSequence str1 , final CharSequence str2 ) {
106- return false ;
177+ String updatedStr1 = String .valueOf (str1 ).toLowerCase ();
178+ String updatedStr2 = String .valueOf (str2 ).toLowerCase ();
179+ return equals (updatedStr1 , updatedStr2 );
107180 }
108181
109182 public static int compare (final String str1 , final String str2 ) {
0 commit comments