2525
2626import java .nio .charset .Charset ;
2727import java .util .Arrays ;
28+ import java .nio .charset .StandardCharsets ;
2829import java .util .ArrayList ;
2930import java .util .HashMap ;
3031import java .util .List ;
3940public class StringUtils extends org .apache .commons .lang3 .StringUtils {
4041 private static final char [] hexChar = {'0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F' };
4142
42- private static Charset preferredACSCharset ;
43+ private static final Charset preferredACSCharset ;
4344 private static final String UTF8 = "UTF-8" ;
4445
4546 static {
4647 if (isUtf8Supported ()) {
47- preferredACSCharset = Charset . forName ( UTF8 ) ;
48+ preferredACSCharset = StandardCharsets . UTF_8 ;
4849 } else {
4950 preferredACSCharset = Charset .defaultCharset ();
5051 }
@@ -66,8 +67,8 @@ public static String cleanupTags(String tags) {
6667 if (tags != null ) {
6768 final String [] tokens = tags .split ("," );
6869 final StringBuilder t = new StringBuilder ();
69- for (int i = 0 ; i < tokens . length ; i ++ ) {
70- t .append (tokens [ i ] .trim ()).append ("," );
70+ for (String token : tokens ) {
71+ t .append (token .trim ()).append ("," );
7172 }
7273 t .delete (t .length () - 1 , t .length ());
7374 tags = t .toString ();
@@ -77,16 +78,16 @@ public static String cleanupTags(String tags) {
7778 }
7879
7980 /**
80- * @param tags
81+ * @param tags a {code}String{code} containing a list of comma separated tags
8182 * @return List of tags
8283 */
8384 public static List <String > csvTagsToList (final String tags ) {
84- final List <String > tagsList = new ArrayList <String >();
85+ final List <String > tagsList = new ArrayList <>();
8586
8687 if (tags != null ) {
8788 final String [] tokens = tags .split ("," );
88- for (int i = 0 ; i < tokens . length ; i ++ ) {
89- tagsList .add (tokens [ i ] .trim ());
89+ for (String token : tokens ) {
90+ tagsList .add (token .trim ());
9091 }
9192 }
9293
@@ -95,13 +96,13 @@ public static List<String> csvTagsToList(final String tags) {
9596
9697 /**
9798 * Converts a List of tags to a comma separated list
98- * @param tagsList
99+ * @param tagsList List of tags to convert to a comma separated list in a {code}String{code}
99100 * @return String containing a comma separated list of tags
100101 */
101102
102103 public static String listToCsvTags (final List <String > tagsList ) {
103104 final StringBuilder tags = new StringBuilder ();
104- if (tagsList .size () > 0 ) {
105+ if (! tagsList .isEmpty () ) {
105106 for (int i = 0 ; i < tagsList .size (); i ++) {
106107 tags .append (tagsList .get (i ));
107108 if (i != tagsList .size () - 1 ) {
@@ -113,22 +114,6 @@ public static String listToCsvTags(final List<String> tagsList) {
113114 return tags .toString ();
114115 }
115116
116- public static String getExceptionStackInfo (final Throwable e ) {
117- final StringBuffer sb = new StringBuffer ();
118-
119- sb .append (e .toString ()).append ("\n " );
120- final StackTraceElement [] elemnents = e .getStackTrace ();
121- for (final StackTraceElement element : elemnents ) {
122- sb .append (element .getClassName ()).append ("." );
123- sb .append (element .getMethodName ()).append ("(" );
124- sb .append (element .getFileName ()).append (":" );
125- sb .append (element .getLineNumber ()).append (")" );
126- sb .append ("\n " );
127- }
128-
129- return sb .toString ();
130- }
131-
132117 public static String unicodeEscape (final String s ) {
133118 final StringBuilder sb = new StringBuilder ();
134119 for (int i = 0 ; i < s .length (); i ++) {
@@ -151,22 +136,17 @@ public static String getMaskedPasswordForDisplay(final String password) {
151136 return "*" ;
152137 }
153138
154- final StringBuffer sb = new StringBuffer ();
155- sb .append (password .charAt (0 ));
156- for (int i = 1 ; i < password .length (); i ++) {
157- sb .append ("*" );
158- }
159-
160- return sb .toString ();
139+ return password .charAt (0 ) +
140+ "*" .repeat (password .length () - 1 );
161141 }
162142
163143 // removes a password request param and it's value, also considering password is in query parameter value which has been url encoded
164- private static final Pattern REGEX_PASSWORD_QUERYSTRING = Pattern .compile ("(&|%26)?[^(&|%26)]*((p|P )assword|accesskey|secretkey)(=|%3D).*?(?=(%26|[&'\" ]|$))" );
144+ private static final Pattern REGEX_PASSWORD_QUERYSTRING = Pattern .compile ("(&|%26)?[^(&|%26)]*(([pP] )assword|accesskey|secretkey)(=|%3D).*?(?=(%26|[&'\" ]|$))" );
165145
166146 // removes a password/accesskey/ property from a response json object
167- private static final Pattern REGEX_PASSWORD_JSON = Pattern .compile ("\" ((p|P )assword|privatekey|accesskey|secretkey)\" :\\ s?\" .*?\" ,?" );
147+ private static final Pattern REGEX_PASSWORD_JSON = Pattern .compile ("\" (([pP] )assword|privatekey|accesskey|secretkey)\" :\\ s?\" .*?\" ,?" );
168148
169- private static final Pattern REGEX_PASSWORD_DETAILS = Pattern .compile ("(&|%26)?details(\\ [|%5B)\\ d*(\\ ]|%5D)\\ .key(=|%3D)((p|P )assword|accesskey|secretkey)(?=(%26|[&'\" ]))" );
149+ private static final Pattern REGEX_PASSWORD_DETAILS = Pattern .compile ("(&|%26)?details(\\ [|%5B)\\ d*(\\ ]|%5D)\\ .key(=|%3D)(([pP] )assword|accesskey|secretkey)(?=(%26|[&'\" ]))" );
170150
171151 private static final Pattern REGEX_PASSWORD_DETAILS_INDEX = Pattern .compile ("details(\\ [|%5B)\\ d*(\\ ]|%5D)" );
172152
@@ -180,7 +160,7 @@ public static String cleanString(final String stringToClean) {
180160 if (stringToClean != null ) {
181161 cleanResult = REGEX_PASSWORD_QUERYSTRING .matcher (stringToClean ).replaceAll ("" );
182162 cleanResult = REGEX_PASSWORD_JSON .matcher (cleanResult ).replaceAll ("" );
183- cleanResult = REGEX_SESSION_KEY .matcher (cleanResult ).replaceFirst ("" );
163+ cleanResult = REGEX_SESSION_KEY .matcher (cleanResult ).replaceAll ("" );
184164 final Matcher detailsMatcher = REGEX_PASSWORD_DETAILS .matcher (cleanResult );
185165 while (detailsMatcher .find ()) {
186166 final Matcher detailsIndexMatcher = REGEX_PASSWORD_DETAILS_INDEX .matcher (detailsMatcher .group ());
@@ -208,24 +188,20 @@ public static boolean areTagsEqual(final String tags1, final String tags2) {
208188 return true ;
209189 }
210190
211- if (tags1 != null && tags2 == null ) {
212- return false ;
213- }
214-
215- if (tags1 == null && tags2 != null ) {
191+ if (tags1 == null ^ tags2 == null ) {
216192 return false ;
217193 }
218194
219195 final String delimiter = "," ;
220196
221- final List <String > lstTags1 = new ArrayList <String >();
197+ final List <String > lstTags1 = new ArrayList <>();
222198 final String [] aTags1 = tags1 .split (delimiter );
223199
224200 for (final String tag1 : aTags1 ) {
225201 lstTags1 .add (tag1 .toLowerCase ());
226202 }
227203
228- final List <String > lstTags2 = new ArrayList <String >();
204+ final List <String > lstTags2 = new ArrayList <>();
229205 final String [] aTags2 = tags2 .split (delimiter );
230206
231207 for (final String tag2 : aTags2 ) {
@@ -236,7 +212,7 @@ public static boolean areTagsEqual(final String tags1, final String tags2) {
236212 }
237213
238214 public static Map <String , String > stringToMap (final String s ) {
239- final Map <String , String > map = new HashMap <String , String >();
215+ final Map <String , String > map = new HashMap <>();
240216 final String [] elements = s .split (";" );
241217 for (final String parts : elements ) {
242218 final String [] keyValue = parts .split (":" );
@@ -246,14 +222,14 @@ public static Map<String, String> stringToMap(final String s) {
246222 }
247223
248224 public static String mapToString (final Map <String , String > map ) {
249- String s = "" ;
225+ StringBuilder s = new StringBuilder () ;
250226 for (final Map .Entry <String , String > entry : map .entrySet ()) {
251- s += entry .getKey () + ":" + entry .getValue () + ";" ;
227+ s . append ( entry .getKey ()). append ( ":" ). append ( entry .getValue ()). append ( ";" ) ;
252228 }
253229 if (s .length () > 0 ) {
254- s = s .substring (0 , s .length () - 1 );
230+ s = new StringBuilder ( s .substring (0 , s .length () - 1 ) );
255231 }
256- return s ;
232+ return s . toString () ;
257233 }
258234
259235 public static <T > List <T > applyPagination (final List <T > originalList , final Long startIndex , final Long pageSizeVal ) {
@@ -274,7 +250,7 @@ public static <T> List<T> applyPagination(final List<T> originalList, final Long
274250 }
275251
276252 private static <T > List <List <T >> partitionList (final List <T > originalList , final int chunkSize ) {
277- final List <List <T >> listOfChunks = new ArrayList <List < T > >();
253+ final List <List <T >> listOfChunks = new ArrayList <>();
278254 for (int i = 0 ; i < originalList .size () / chunkSize ; i ++) {
279255 listOfChunks .add (originalList .subList (i * chunkSize , i * chunkSize + chunkSize ));
280256 }
@@ -302,9 +278,7 @@ public static Map<String, String> parseJsonToMap(String jsonString) {
302278 if (org .apache .commons .lang3 .StringUtils .isNotBlank (jsonString )) {
303279 try {
304280 JsonNode jsonNode = objectMapper .readTree (jsonString );
305- jsonNode .fields ().forEachRemaining (entry -> {
306- mapResult .put (entry .getKey (), entry .getValue ().asText ());
307- });
281+ jsonNode .fields ().forEachRemaining (entry -> mapResult .put (entry .getKey (), entry .getValue ().asText ()));
308282 } catch (Exception e ) {
309283 throw new CloudRuntimeException ("Error while parsing json to convert it to map " + e .getMessage ());
310284 }
0 commit comments